Skip to content

Commit 5b0958d

Browse files
committed
Set space locked as a boolean
1 parent 77433c8 commit 5b0958d

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

body.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (body *Body) SetType(newType int) {
177177
if body.space == nil {
178178
return
179179
}
180-
assert(body.space.locked == 0, "Space is locked")
180+
assertSpaceUnlocked(body.space)
181181

182182
if oldType != BODY_STATIC {
183183
body.Activate()

debug.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ func assert(truth bool, msg ...interface{}) {
99
panic(fmt.Sprint("Assertion failed: ", msg))
1010
}
1111
}
12+
13+
func assertSpaceUnlocked(space *Space) {
14+
assert(!space.locked, "This operation cannot be done safely during a call to Space.Step() or during a query. Put these calls into a post-step callback.")
15+
}

release.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
package cp
44

55
func assert(_ bool, _ ...interface{}) {}
6+
7+
func assertSpaceUnlocked(_ *Space) {}

space.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type Space struct {
4141
cachedArbiters *HashSet[ShapePair, *Arbiter]
4242
pooledArbiters sync.Pool
4343

44-
locked int
44+
locked bool
4545

4646
usesWildcards bool
4747
collisionHandlers *HashSet[*CollisionHandler, *CollisionHandler]
@@ -68,7 +68,7 @@ func NewSpace() *Space {
6868
collisionSlop: 0.1,
6969
collisionBias: math.Pow(0.9, 60),
7070
collisionPersistence: 3,
71-
locked: 0,
71+
locked: false,
7272
stamp: 0,
7373
shapeIDCounter: 1,
7474
staticShapes: NewBBTree(ShapeGetBB, nil),
@@ -147,7 +147,7 @@ func (space *Space) SetStaticBody(body *Body) {
147147
func (space *Space) Activate(body *Body) {
148148
assert(body.GetType() == BODY_DYNAMIC, "Attempting to activate a non-dynamic body")
149149

150-
if space.locked != 0 {
150+
if space.locked {
151151
if !Contains(space.rousedBodies, body) {
152152
space.rousedBodies = append(space.rousedBodies, body)
153153
}
@@ -262,7 +262,7 @@ func (space *Space) AddShape(shape *Shape) *Shape {
262262

263263
assert(shape.space != space, "You have already added this shape to this space. You must not add it a second time.")
264264
assert(shape.space == nil, "You have already added this shape to another space. You cannot add it to a second.")
265-
assert(space.locked == 0, "This operation cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.")
265+
assertSpaceUnlocked(space)
266266

267267
isStatic := body.GetType() == BODY_STATIC
268268
if !isStatic {
@@ -299,7 +299,7 @@ func (space *Space) AddBody(body *Body) *Body {
299299
func (space *Space) AddConstraint(constraint *Constraint) *Constraint {
300300
assert(constraint.space != space, "Already added to this space")
301301
assert(constraint.space == nil, "Already added to another space")
302-
assert(space.locked == 0, "Space is locked")
302+
assertSpaceUnlocked(space)
303303

304304
a := constraint.a
305305
b := constraint.b
@@ -323,7 +323,7 @@ func (space *Space) AddConstraint(constraint *Constraint) *Constraint {
323323

324324
func (space *Space) RemoveConstraint(constraint *Constraint) {
325325
assert(space.ContainsConstraint(constraint), "Constraint not found")
326-
assert(space.locked == 0, "Space is locked")
326+
assertSpaceUnlocked(space)
327327

328328
constraint.a.Activate()
329329
constraint.b.Activate()
@@ -342,7 +342,7 @@ func (space *Space) RemoveConstraint(constraint *Constraint) {
342342
func (space *Space) RemoveShape(shape *Shape) {
343343
body := shape.body
344344
assert(space.ContainsShape(shape))
345-
assert(space.locked == 0)
345+
assertSpaceUnlocked(space)
346346

347347
isStatic := body.GetType() == BODY_STATIC
348348
if isStatic {
@@ -365,7 +365,7 @@ func (space *Space) RemoveShape(shape *Shape) {
365365
func (space *Space) RemoveBody(body *Body) {
366366
assert(body != space.StaticBody)
367367
assert(space.ContainsBody(body))
368-
assert(space.locked == 0)
368+
assertSpaceUnlocked(space)
369369

370370
body.Activate()
371371
if body.GetType() == BODY_STATIC {
@@ -782,17 +782,11 @@ func (space *Space) Step(dt float64) {
782782
}
783783

784784
func (space *Space) Lock() {
785-
space.locked++
785+
space.locked = true
786786
}
787787

788788
func (space *Space) Unlock(runPostStep bool) {
789-
space.locked--
790-
791-
assert(space.locked >= 0, "Space lock underflow")
792-
793-
if space.locked != 0 {
794-
return
795-
}
789+
space.locked = false
796790

797791
for i := 0; i < len(space.rousedBodies); i++ {
798792
space.Activate(space.rousedBodies[i])
@@ -820,6 +814,10 @@ func (space *Space) Unlock(runPostStep bool) {
820814
}
821815
}
822816

817+
func (space *Space) IsLocked() bool {
818+
return space.locked
819+
}
820+
823821
func (space *Space) UncacheArbiter(arb *Arbiter) {
824822
a := arb.a
825823
b := arb.b
@@ -1133,7 +1131,7 @@ func (space *Space) ShapeQuery(shape *Shape, callback func(shape *Shape, points
11331131
// ReindexShape re-computes the hash of the shape in both the dynamic and static list.
11341132
func (space *Space) ReindexShape(shape *Shape) {
11351133

1136-
assert(space.locked == 0, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.")
1134+
assert(!space.locked, "You cannot manually reindex objects while the space is locked. Wait until the current query or step is complete.")
11371135

11381136
shape.CacheBB()
11391137

0 commit comments

Comments
 (0)