Skip to content

Commit 2acffc4

Browse files
committed
refactor: replace bit flag with a boolean
1 parent 8197d51 commit 2acffc4

File tree

2 files changed

+57
-7
lines changed
  • fxgl-entity/src
    • main/java/com/almasb/fxgl/physics/box2d/dynamics
    • test/kotlin/com/almasb/fxgl/physics/box2d/dynamics

2 files changed

+57
-7
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/physics/box2d/dynamics/Body.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public final class Body {
3131
private static final int e_islandFlag = 0x0001;
3232

3333
private static final int e_awakeFlag = 0x0002;
34-
private static final int e_autoSleepFlag = 0x0004;
3534
private static final int e_bulletFlag = 0x0008;
3635
private static final int e_fixedRotationFlag = 0x0010;
3736
private static final int e_activeFlag = 0x0020;
@@ -46,6 +45,8 @@ public final class Body {
4645

4746
public int m_flags = 0;
4847

48+
private boolean isSleepingAllowed = false;
49+
4950
public int m_islandIndex;
5051

5152
/**
@@ -102,7 +103,7 @@ public final class Body {
102103
m_flags |= e_fixedRotationFlag;
103104
}
104105
if (bd.isAllowSleep()) {
105-
m_flags |= e_autoSleepFlag;
106+
isSleepingAllowed = true;
106107
}
107108
if (bd.isAwake()) {
108109
m_flags |= e_awakeFlag;
@@ -966,10 +967,9 @@ void setIslandFlag(boolean flag) {
966967
* @param flag sleep flag
967968
*/
968969
public void setSleepingAllowed(boolean flag) {
969-
if (flag) {
970-
m_flags |= e_autoSleepFlag;
971-
} else {
972-
m_flags &= ~e_autoSleepFlag;
970+
isSleepingAllowed = flag;
971+
972+
if (!isSleepingAllowed) {
973973
setAwake(true);
974974
}
975975
}
@@ -978,7 +978,7 @@ public void setSleepingAllowed(boolean flag) {
978978
* @return whether this body is allowed to sleep
979979
*/
980980
public boolean isSleepingAllowed() {
981-
return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
981+
return isSleepingAllowed;
982982
}
983983

984984
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB (almaslvl@gmail.com).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.physics.box2d.dynamics
8+
9+
import com.almasb.fxgl.core.math.Vec2
10+
import org.junit.jupiter.api.Assertions.*
11+
import org.junit.jupiter.api.Test
12+
13+
/**
14+
*
15+
* @author Almas Baimagambetov (almaslvl@gmail.com)
16+
*/
17+
class BodyTest {
18+
19+
@Test
20+
fun `Body def sets body flags`() {
21+
val def = BodyDef()
22+
def.isActive = true
23+
def.isAllowSleep = true
24+
def.isBullet = true
25+
def.isFixedRotation = true
26+
def.isAwake = true
27+
28+
val world = World(Vec2())
29+
30+
val body = Body(def, world)
31+
32+
assertTrue(body.isActive)
33+
assertTrue(body.isSleepingAllowed)
34+
assertTrue(body.isBullet)
35+
assertTrue(body.isFixedRotation)
36+
assertTrue(body.isAwake)
37+
38+
body.isActive = false
39+
body.isSleepingAllowed = false
40+
body.isBullet = false
41+
body.isFixedRotation = false
42+
body.isAwake = false
43+
44+
assertFalse(body.isActive)
45+
assertFalse(body.isSleepingAllowed)
46+
assertFalse(body.isBullet)
47+
assertFalse(body.isFixedRotation)
48+
assertFalse(body.isAwake)
49+
}
50+
}

0 commit comments

Comments
 (0)