Skip to content

Commit

Permalink
refactor: Make PhysicsComponent vars private, create access methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart-del committed Mar 26, 2024
1 parent 8d5c543 commit 6dd326a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,13 @@
* @author Almas Baimagambetov (AlmasB) ([email protected])
*/
public final class PhysicsComponent extends Component {

FixtureDef fixtureDef = new FixtureDef();
BodyDef bodyDef = new BodyDef();

Body body;

private List<Entity> groundedList = new ArrayList<>();

private ReadOnlyBooleanWrapper onGroundProperty = new ReadOnlyBooleanWrapper(false);

private FixtureDef fixtureDef = new FixtureDef();
private BodyDef bodyDef = new BodyDef();
private Body body;
private final List<Entity> groundedList = new ArrayList<>();
private final ReadOnlyBooleanWrapper onGroundProperty = new ReadOnlyBooleanWrapper(false);
private boolean raycastIgnored = false;

private Runnable onInitPhysics = EmptyRunnable.INSTANCE;

private PhysicsWorld physicsWorld;

void setWorld(PhysicsWorld world) {
Expand Down Expand Up @@ -88,6 +81,18 @@ public Body getBody() {
return body;
}

public void setBody(Body body) {
this.body = body;
}

public FixtureDef getFixtureDef() {
return fixtureDef;
}

public BodyDef getBodyDef() {
return bodyDef;
}

/**
* Set a callback to run when this entity has been added to physics world.
*
Expand All @@ -97,7 +102,7 @@ public void setOnPhysicsInitialized(Runnable code) {
onInitPhysics = code;
}

private Map<HitBox, SensorCollisionHandler> sensorHandlers = new HashMap<>();
private final Map<HitBox, SensorCollisionHandler> sensorHandlers = new HashMap<>();

public Map<HitBox, SensorCollisionHandler> getSensorHandlers() {
return sensorHandlers;
Expand Down
36 changes: 18 additions & 18 deletions fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void onPhysicsEntityAdded(Entity entity) {
}

ChangeListener<Number> scaleChangeListener = (observable, oldValue, newValue) -> {
Body b = entity.getComponent(PhysicsComponent.class).body;
Body b = entity.getComponent(PhysicsComponent.class).getBody();

if (b != null) {
List<Fixture> fixtures = List.copyOf(b.getFixtures());
Expand Down Expand Up @@ -290,14 +290,14 @@ private boolean needManualCheck(Entity e1, Entity e2) {
// if no physics -> check manually

BodyType type1 = e1.getComponentOptional(PhysicsComponent.class)
.map(p -> p.body.getType())
.map(p -> p.getBody().getType())
.orElse(null);

if (type1 == null)
return true;

BodyType type2 = e2.getComponentOptional(PhysicsComponent.class)
.map(p -> p.body.getType())
.map(p -> p.getBody().getType())
.orElse(null);

if (type2 == null)
Expand Down Expand Up @@ -543,38 +543,38 @@ private void createBody(Entity e) {
PhysicsComponent physics = e.getComponent(PhysicsComponent.class);
physics.setWorld(this);

final BodyDef bodyDef = physics.getBodyDef();

// if position is 0, 0 then probably not set, so set ourselves
if (physics.bodyDef.getPosition().x == 0 && physics.bodyDef.getPosition().y == 0) {
physics.bodyDef.getPosition().set(toPoint(e.getCenter()));
if (bodyDef.getPosition().x == 0 && bodyDef.getPosition().y == 0) {
bodyDef.getPosition().set(toPoint(e.getCenter()));
}

if (physics.bodyDef.getAngle() == 0) {
physics.bodyDef.setAngle((float) -Math.toRadians(e.getRotation()));
if (bodyDef.getAngle() == 0) {
bodyDef.setAngle((float) -Math.toRadians(e.getRotation()));
}

physics.body = jboxWorld.createBody(physics.bodyDef);

physics.setBody(jboxWorld.createBody(bodyDef));
createFixtures(e);

createSensors(e);

physics.body.setEntity(e);
physics.getBody().setEntity(e);
physics.onInitPhysics();
}

private void createFixtures(Entity e) {
BoundingBoxComponent bbox = e.getBoundingBoxComponent();
PhysicsComponent physics = e.getComponent(PhysicsComponent.class);

FixtureDef fd = physics.fixtureDef;
FixtureDef fd = physics.getFixtureDef();

for (HitBox box : bbox.hitBoxesProperty()) {
Shape b2Shape = createShape(box, e);

// we use definitions from user, but override shape
fd.setShape(b2Shape);

Fixture fixture = physics.body.createFixture(fd);
Fixture fixture = physics.getBody().createFixture(fd);

fixture.setHitBox(box);
}
Expand All @@ -595,13 +595,13 @@ private void createSensors(Entity e) {
.sensor(true)
.shape(polygonShape);

Fixture f = physics.body.createFixture(fd);
Fixture f = physics.getBody().createFixture(fd);
f.setHitBox(box);
});
}

private Shape createShape(HitBox box, Entity e) {
if (e.getComponent(PhysicsComponent.class).body.getType() != BodyType.STATIC
if (e.getComponent(PhysicsComponent.class).getBody().getType() != BodyType.STATIC
&& box.getShape() instanceof ChainShapeData) {
throw new IllegalArgumentException("BoundingShape.chain() can only be used with BodyType.STATIC");
}
Expand All @@ -623,7 +623,7 @@ void destroyFixture(Body body, HitBox box) {
* @param e physics entity
*/
private void destroyBody(Entity e) {
jboxWorld.destroyBody(e.getComponent(PhysicsComponent.class).body);
jboxWorld.destroyBody(e.getComponent(PhysicsComponent.class).getBody());
}

private EdgeCallback raycastCallback = new EdgeCallback();
Expand Down Expand Up @@ -747,8 +747,8 @@ public <T extends Joint> T addJoint(Entity e1, Entity e2, JointDef<T> def) {
var p1 = e1.getComponent(PhysicsComponent.class);
var p2 = e2.getComponent(PhysicsComponent.class);

def.setBodyA(p1.body);
def.setBodyB(p2.body);
def.setBodyA(p1.getBody());
def.setBodyB(p2.getBody());

return jboxWorld.createJoint(def);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class PhysicsComponentTest {
@Test
fun `Body`() {
val c = PhysicsComponent()
assertNull(c.body)
assertThrows<IllegalStateException> { c.body }

val world = PhysicsWorld(600, 50.0)

val e = Entity()

e.boundingBoxComponent.addHitBox(HitBox(BoundingShape.box(10.0, 10.0)))
e.addComponent(c)

Expand Down

0 comments on commit 6dd326a

Please sign in to comment.