From b81e7038e0bf06649a5392206109638f0d5cc7e2 Mon Sep 17 00:00:00 2001 From: Quillraven Date: Tue, 27 May 2025 20:37:23 +0200 Subject: [PATCH] convert records to normal classes --- .../com/quillraven/component/Graphic.java | 26 +++++++---- .../com/quillraven/component/Physic.java | 22 +++++++-- .../com/quillraven/component/Transform.java | 45 ++++++++++++++++--- .../com/quillraven/system/CleanupSystem.java | 2 +- .../com/quillraven/system/RenderSystem.java | 14 +++--- .../quillraven/tiled/TiledAshleySpawner.java | 4 +- 6 files changed, 84 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/io/github/com/quillraven/component/Graphic.java b/core/src/main/java/io/github/com/quillraven/component/Graphic.java index d73fbc3..f44baaa 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Graphic.java +++ b/core/src/main/java/io/github/com/quillraven/component/Graphic.java @@ -5,13 +5,23 @@ import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.TextureRegion; -/** - * Component that stores the visual representation of an entity. - * Contains a texture region and a color for tinting. - */ -public record Graphic( - TextureRegion region, - Color color -) implements Component { +public class Graphic implements Component { public static final ComponentMapper MAPPER = ComponentMapper.getFor(Graphic.class); + + private final TextureRegion region; + private final Color color; + + public Graphic(TextureRegion region, Color color) { + this.region = region; + this.color = color; + } + + public TextureRegion getRegion() { + return region; + } + + public Color getColor() { + return color; + } + } diff --git a/core/src/main/java/io/github/com/quillraven/component/Physic.java b/core/src/main/java/io/github/com/quillraven/component/Physic.java index 8e79d98..bcce4ae 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Physic.java +++ b/core/src/main/java/io/github/com/quillraven/component/Physic.java @@ -5,9 +5,23 @@ import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.Body; -public record Physic( - Body body, - Vector2 prevPosition -) implements Component { +public class Physic implements Component { public static final ComponentMapper MAPPER = ComponentMapper.getFor(Physic.class); + + private final Body body; + private final Vector2 prevPosition; + + public Physic(Body body, Vector2 prevPosition) { + this.body = body; + this.prevPosition = prevPosition; + } + + public Body getBody() { + return body; + } + + public Vector2 getPrevPosition() { + return prevPosition; + } + } diff --git a/core/src/main/java/io/github/com/quillraven/component/Transform.java b/core/src/main/java/io/github/com/quillraven/component/Transform.java index ee739ef..cad9bd9 100644 --- a/core/src/main/java/io/github/com/quillraven/component/Transform.java +++ b/core/src/main/java/io/github/com/quillraven/component/Transform.java @@ -4,15 +4,29 @@ import com.badlogic.ashley.core.Component; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.gdx.math.Vector2; -public record Transform( - Vector2 position, - int z, - Vector2 size, - Vector2 scaling, - float rotationDeg -) implements Component, Comparable { +public class Transform implements Component, Comparable { public static final ComponentMapper MAPPER = ComponentMapper.getFor(Transform.class); + private final Vector2 position; + private final int z; + private final Vector2 size; + private final Vector2 scaling; + private final float rotationDeg; + + public Transform( + Vector2 position, + int z, + Vector2 size, + Vector2 scaling, + float rotationDeg + ) { + this.position = position; + this.z = z; + this.size = size; + this.scaling = scaling; + this.rotationDeg = rotationDeg; + } + @Override public int compareTo(Transform other) { if (this.z != other.z) { @@ -23,4 +37,21 @@ public record Transform( } return Float.compare(this.position.x, other.position.x); } + + public Vector2 getPosition() { + return position; + } + + public Vector2 getSize() { + return size; + } + + public Vector2 getScaling() { + return scaling; + } + + public float getRotationDeg() { + return rotationDeg; + } + } diff --git a/core/src/main/java/io/github/com/quillraven/system/CleanupSystem.java b/core/src/main/java/io/github/com/quillraven/system/CleanupSystem.java index b5f680e..8cae5e1 100644 --- a/core/src/main/java/io/github/com/quillraven/system/CleanupSystem.java +++ b/core/src/main/java/io/github/com/quillraven/system/CleanupSystem.java @@ -33,7 +33,7 @@ public class CleanupSystem extends EntitySystem implements EntityListener { // This ONLY works when an entity with a Physic component gets removed entirely from the engine. Physic physic = Physic.MAPPER.get(entity); if (physic != null) { - Body body = physic.body(); + Body body = physic.getBody(); body.getWorld().destroyBody(body); } } diff --git a/core/src/main/java/io/github/com/quillraven/system/RenderSystem.java b/core/src/main/java/io/github/com/quillraven/system/RenderSystem.java index e4b2880..52ba40d 100644 --- a/core/src/main/java/io/github/com/quillraven/system/RenderSystem.java +++ b/core/src/main/java/io/github/com/quillraven/system/RenderSystem.java @@ -65,22 +65,22 @@ public class RenderSystem extends SortedIteratingSystem implements Disposable { protected void processEntity(Entity entity, float deltaTime) { Transform transform = Transform.MAPPER.get(entity); Graphic graphic = Graphic.MAPPER.get(entity); - if (graphic.region() == null) { + if (graphic.getRegion() == null) { return; } - Vector2 position = transform.position(); - Vector2 scaling = transform.scaling(); - Vector2 size = transform.size(); - batch.setColor(graphic.color()); + Vector2 position = transform.getPosition(); + Vector2 scaling = transform.getScaling(); + Vector2 size = transform.getSize(); + batch.setColor(graphic.getColor()); batch.draw( - graphic.region(), + graphic.getRegion(), position.x - (1f - scaling.x) * size.x * 0.5f, position.y - (1f - scaling.y) * size.y * 0.5f, size.x * 0.5f, size.y * 0.5f, size.x, size.y, scaling.x, scaling.y, - transform.rotationDeg() + transform.getRotationDeg() ); } diff --git a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java index 292f96e..d5e7492 100644 --- a/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java +++ b/core/src/main/java/io/github/com/quillraven/tiled/TiledAshleySpawner.java @@ -115,13 +115,13 @@ public class TiledAshleySpawner { BodyDef bodyDef = new BodyDef(); bodyDef.type = bodyType; Transform transform = entity.getComponent(Transform.class); - bodyDef.position.set(transform.position()); + bodyDef.position.set(transform.getPosition()); bodyDef.fixedRotation = true; Body body = this.physicWorld.createBody(bodyDef); body.setUserData(entity); for (MapObject object : mapObjects) { - FixtureDef fixtureDef = TiledPhysics.fixtureDefOfMapObject(object, transform.scaling(), relativeTo); + FixtureDef fixtureDef = TiledPhysics.fixtureDefOfMapObject(object, transform.getScaling(), relativeTo); body.createFixture(fixtureDef); fixtureDef.shape.dispose(); }