Skip to content

Commit

Permalink
refactor: replace bit flag with a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Feb 18, 2024
1 parent 8197d51 commit 2acffc4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public final class Body {
private static final int e_islandFlag = 0x0001;

private static final int e_awakeFlag = 0x0002;
private static final int e_autoSleepFlag = 0x0004;
private static final int e_bulletFlag = 0x0008;
private static final int e_fixedRotationFlag = 0x0010;
private static final int e_activeFlag = 0x0020;
Expand All @@ -46,6 +45,8 @@ public final class Body {

public int m_flags = 0;

private boolean isSleepingAllowed = false;

public int m_islandIndex;

/**
Expand Down Expand Up @@ -102,7 +103,7 @@ public final class Body {
m_flags |= e_fixedRotationFlag;
}
if (bd.isAllowSleep()) {
m_flags |= e_autoSleepFlag;
isSleepingAllowed = true;
}
if (bd.isAwake()) {
m_flags |= e_awakeFlag;
Expand Down Expand Up @@ -966,10 +967,9 @@ void setIslandFlag(boolean flag) {
* @param flag sleep flag
*/
public void setSleepingAllowed(boolean flag) {
if (flag) {
m_flags |= e_autoSleepFlag;
} else {
m_flags &= ~e_autoSleepFlag;
isSleepingAllowed = flag;

if (!isSleepingAllowed) {
setAwake(true);
}
}
Expand All @@ -978,7 +978,7 @@ public void setSleepingAllowed(boolean flag) {
* @return whether this body is allowed to sleep
*/
public boolean isSleepingAllowed() {
return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
return isSleepingAllowed;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB ([email protected]).
* See LICENSE for details.
*/

package com.almasb.fxgl.physics.box2d.dynamics

import com.almasb.fxgl.core.math.Vec2
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

/**
*
* @author Almas Baimagambetov ([email protected])
*/
class BodyTest {

@Test
fun `Body def sets body flags`() {
val def = BodyDef()
def.isActive = true
def.isAllowSleep = true
def.isBullet = true
def.isFixedRotation = true
def.isAwake = true

val world = World(Vec2())

val body = Body(def, world)

assertTrue(body.isActive)
assertTrue(body.isSleepingAllowed)
assertTrue(body.isBullet)
assertTrue(body.isFixedRotation)
assertTrue(body.isAwake)

body.isActive = false
body.isSleepingAllowed = false
body.isBullet = false
body.isFixedRotation = false
body.isAwake = false

assertFalse(body.isActive)
assertFalse(body.isSleepingAllowed)
assertFalse(body.isBullet)
assertFalse(body.isFixedRotation)
assertFalse(body.isAwake)
}
}

0 comments on commit 2acffc4

Please sign in to comment.