From 6dd326a898a5257797ed21a70d2a81c2777c9bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Wysocki?= Date: Tue, 26 Mar 2024 21:45:37 +0100 Subject: [PATCH] refactor: Make PhysicsComponent vars private, create access methods --- .../almasb/fxgl/physics/PhysicsComponent.java | 31 +++++++++------- .../com/almasb/fxgl/physics/PhysicsWorld.java | 36 +++++++++---------- .../fxgl/physics/PhysicsComponentTest.kt | 4 +-- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsComponent.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsComponent.java index 6bfe22e616..f5e5f0b31e 100644 --- a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsComponent.java +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsComponent.java @@ -34,20 +34,13 @@ * @author Almas Baimagambetov (AlmasB) (almaslvl@gmail.com) */ public final class PhysicsComponent extends Component { - - FixtureDef fixtureDef = new FixtureDef(); - BodyDef bodyDef = new BodyDef(); - - Body body; - - private List 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 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) { @@ -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. * @@ -97,7 +102,7 @@ public void setOnPhysicsInitialized(Runnable code) { onInitPhysics = code; } - private Map sensorHandlers = new HashMap<>(); + private final Map sensorHandlers = new HashMap<>(); public Map getSensorHandlers() { return sensorHandlers; diff --git a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java index 2239b7e25f..047a45918d 100644 --- a/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java +++ b/fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java @@ -128,7 +128,7 @@ private void onPhysicsEntityAdded(Entity entity) { } ChangeListener scaleChangeListener = (observable, oldValue, newValue) -> { - Body b = entity.getComponent(PhysicsComponent.class).body; + Body b = entity.getComponent(PhysicsComponent.class).getBody(); if (b != null) { List fixtures = List.copyOf(b.getFixtures()); @@ -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) @@ -543,22 +543,22 @@ 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(); } @@ -566,7 +566,7 @@ 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); @@ -574,7 +574,7 @@ private void createFixtures(Entity 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); } @@ -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"); } @@ -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(); @@ -747,8 +747,8 @@ public T addJoint(Entity e1, Entity e2, JointDef 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); } diff --git a/fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/PhysicsComponentTest.kt b/fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/PhysicsComponentTest.kt index d74105363a..5452464a83 100644 --- a/fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/PhysicsComponentTest.kt +++ b/fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/PhysicsComponentTest.kt @@ -75,11 +75,11 @@ class PhysicsComponentTest { @Test fun `Body`() { val c = PhysicsComponent() - assertNull(c.body) + assertThrows { c.body } val world = PhysicsWorld(600, 50.0) - val e = Entity() + e.boundingBoxComponent.addHitBox(HitBox(BoundingShape.box(10.0, 10.0))) e.addComponent(c)