Skip to content

Commit 6dd326a

Browse files
committed
refactor: Make PhysicsComponent vars private, create access methods
1 parent 8d5c543 commit 6dd326a

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsComponent.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,13 @@
3434
* @author Almas Baimagambetov (AlmasB) ([email protected])
3535
*/
3636
public final class PhysicsComponent extends Component {
37-
38-
FixtureDef fixtureDef = new FixtureDef();
39-
BodyDef bodyDef = new BodyDef();
40-
41-
Body body;
42-
43-
private List<Entity> groundedList = new ArrayList<>();
44-
45-
private ReadOnlyBooleanWrapper onGroundProperty = new ReadOnlyBooleanWrapper(false);
46-
37+
private FixtureDef fixtureDef = new FixtureDef();
38+
private BodyDef bodyDef = new BodyDef();
39+
private Body body;
40+
private final List<Entity> groundedList = new ArrayList<>();
41+
private final ReadOnlyBooleanWrapper onGroundProperty = new ReadOnlyBooleanWrapper(false);
4742
private boolean raycastIgnored = false;
48-
4943
private Runnable onInitPhysics = EmptyRunnable.INSTANCE;
50-
5144
private PhysicsWorld physicsWorld;
5245

5346
void setWorld(PhysicsWorld world) {
@@ -88,6 +81,18 @@ public Body getBody() {
8881
return body;
8982
}
9083

84+
public void setBody(Body body) {
85+
this.body = body;
86+
}
87+
88+
public FixtureDef getFixtureDef() {
89+
return fixtureDef;
90+
}
91+
92+
public BodyDef getBodyDef() {
93+
return bodyDef;
94+
}
95+
9196
/**
9297
* Set a callback to run when this entity has been added to physics world.
9398
*
@@ -97,7 +102,7 @@ public void setOnPhysicsInitialized(Runnable code) {
97102
onInitPhysics = code;
98103
}
99104

100-
private Map<HitBox, SensorCollisionHandler> sensorHandlers = new HashMap<>();
105+
private final Map<HitBox, SensorCollisionHandler> sensorHandlers = new HashMap<>();
101106

102107
public Map<HitBox, SensorCollisionHandler> getSensorHandlers() {
103108
return sensorHandlers;

fxgl-entity/src/main/java/com/almasb/fxgl/physics/PhysicsWorld.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private void onPhysicsEntityAdded(Entity entity) {
128128
}
129129

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

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

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

296296
if (type1 == null)
297297
return true;
298298

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

303303
if (type2 == null)
@@ -543,38 +543,38 @@ private void createBody(Entity e) {
543543
PhysicsComponent physics = e.getComponent(PhysicsComponent.class);
544544
physics.setWorld(this);
545545

546+
final BodyDef bodyDef = physics.getBodyDef();
547+
546548
// if position is 0, 0 then probably not set, so set ourselves
547-
if (physics.bodyDef.getPosition().x == 0 && physics.bodyDef.getPosition().y == 0) {
548-
physics.bodyDef.getPosition().set(toPoint(e.getCenter()));
549+
if (bodyDef.getPosition().x == 0 && bodyDef.getPosition().y == 0) {
550+
bodyDef.getPosition().set(toPoint(e.getCenter()));
549551
}
550552

551-
if (physics.bodyDef.getAngle() == 0) {
552-
physics.bodyDef.setAngle((float) -Math.toRadians(e.getRotation()));
553+
if (bodyDef.getAngle() == 0) {
554+
bodyDef.setAngle((float) -Math.toRadians(e.getRotation()));
553555
}
554556

555-
physics.body = jboxWorld.createBody(physics.bodyDef);
556-
557+
physics.setBody(jboxWorld.createBody(bodyDef));
557558
createFixtures(e);
558-
559559
createSensors(e);
560560

561-
physics.body.setEntity(e);
561+
physics.getBody().setEntity(e);
562562
physics.onInitPhysics();
563563
}
564564

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

569-
FixtureDef fd = physics.fixtureDef;
569+
FixtureDef fd = physics.getFixtureDef();
570570

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

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

577-
Fixture fixture = physics.body.createFixture(fd);
577+
Fixture fixture = physics.getBody().createFixture(fd);
578578

579579
fixture.setHitBox(box);
580580
}
@@ -595,13 +595,13 @@ private void createSensors(Entity e) {
595595
.sensor(true)
596596
.shape(polygonShape);
597597

598-
Fixture f = physics.body.createFixture(fd);
598+
Fixture f = physics.getBody().createFixture(fd);
599599
f.setHitBox(box);
600600
});
601601
}
602602

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

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

750-
def.setBodyA(p1.body);
751-
def.setBodyB(p2.body);
750+
def.setBodyA(p1.getBody());
751+
def.setBodyB(p2.getBody());
752752

753753
return jboxWorld.createJoint(def);
754754
}

fxgl-entity/src/test/kotlin/com/almasb/fxgl/physics/PhysicsComponentTest.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ class PhysicsComponentTest {
7575
@Test
7676
fun `Body`() {
7777
val c = PhysicsComponent()
78-
assertNull(c.body)
78+
assertThrows<IllegalStateException> { c.body }
7979

8080
val world = PhysicsWorld(600, 50.0)
81-
8281
val e = Entity()
82+
8383
e.boundingBoxComponent.addHitBox(HitBox(BoundingShape.box(10.0, 10.0)))
8484
e.addComponent(c)
8585

0 commit comments

Comments
 (0)