convert records to normal classes

This commit is contained in:
Quillraven
2025-05-27 20:37:23 +02:00
parent ab2216c191
commit b81e7038e0
6 changed files with 84 additions and 29 deletions

View File

@@ -5,13 +5,23 @@ import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.graphics.g2d.TextureRegion;
/** public class Graphic implements Component {
* 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 static final ComponentMapper<Graphic> MAPPER = ComponentMapper.getFor(Graphic.class); public static final ComponentMapper<Graphic> 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;
}
} }

View File

@@ -5,9 +5,23 @@ import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.Body;
public record Physic( public class Physic implements Component {
Body body,
Vector2 prevPosition
) implements Component {
public static final ComponentMapper<Physic> MAPPER = ComponentMapper.getFor(Physic.class); public static final ComponentMapper<Physic> 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;
}
} }

View File

@@ -4,15 +4,29 @@ import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
public record Transform( public class Transform implements Component, Comparable<Transform> {
Vector2 position,
int z,
Vector2 size,
Vector2 scaling,
float rotationDeg
) implements Component, Comparable<Transform> {
public static final ComponentMapper<Transform> MAPPER = ComponentMapper.getFor(Transform.class); public static final ComponentMapper<Transform> 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 @Override
public int compareTo(Transform other) { public int compareTo(Transform other) {
if (this.z != other.z) { if (this.z != other.z) {
@@ -23,4 +37,21 @@ public record Transform(
} }
return Float.compare(this.position.x, other.position.x); 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;
}
} }

View File

@@ -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. // This ONLY works when an entity with a Physic component gets removed entirely from the engine.
Physic physic = Physic.MAPPER.get(entity); Physic physic = Physic.MAPPER.get(entity);
if (physic != null) { if (physic != null) {
Body body = physic.body(); Body body = physic.getBody();
body.getWorld().destroyBody(body); body.getWorld().destroyBody(body);
} }
} }

View File

@@ -65,22 +65,22 @@ public class RenderSystem extends SortedIteratingSystem implements Disposable {
protected void processEntity(Entity entity, float deltaTime) { protected void processEntity(Entity entity, float deltaTime) {
Transform transform = Transform.MAPPER.get(entity); Transform transform = Transform.MAPPER.get(entity);
Graphic graphic = Graphic.MAPPER.get(entity); Graphic graphic = Graphic.MAPPER.get(entity);
if (graphic.region() == null) { if (graphic.getRegion() == null) {
return; return;
} }
Vector2 position = transform.position(); Vector2 position = transform.getPosition();
Vector2 scaling = transform.scaling(); Vector2 scaling = transform.getScaling();
Vector2 size = transform.size(); Vector2 size = transform.getSize();
batch.setColor(graphic.color()); batch.setColor(graphic.getColor());
batch.draw( batch.draw(
graphic.region(), graphic.getRegion(),
position.x - (1f - scaling.x) * size.x * 0.5f, position.x - (1f - scaling.x) * size.x * 0.5f,
position.y - (1f - scaling.y) * size.y * 0.5f, position.y - (1f - scaling.y) * size.y * 0.5f,
size.x * 0.5f, size.y * 0.5f, size.x * 0.5f, size.y * 0.5f,
size.x, size.y, size.x, size.y,
scaling.x, scaling.y, scaling.x, scaling.y,
transform.rotationDeg() transform.getRotationDeg()
); );
} }

View File

@@ -115,13 +115,13 @@ public class TiledAshleySpawner {
BodyDef bodyDef = new BodyDef(); BodyDef bodyDef = new BodyDef();
bodyDef.type = bodyType; bodyDef.type = bodyType;
Transform transform = entity.getComponent(Transform.class); Transform transform = entity.getComponent(Transform.class);
bodyDef.position.set(transform.position()); bodyDef.position.set(transform.getPosition());
bodyDef.fixedRotation = true; bodyDef.fixedRotation = true;
Body body = this.physicWorld.createBody(bodyDef); Body body = this.physicWorld.createBody(bodyDef);
body.setUserData(entity); body.setUserData(entity);
for (MapObject object : mapObjects) { for (MapObject object : mapObjects) {
FixtureDef fixtureDef = TiledPhysics.fixtureDefOfMapObject(object, transform.scaling(), relativeTo); FixtureDef fixtureDef = TiledPhysics.fixtureDefOfMapObject(object, transform.getScaling(), relativeTo);
body.createFixture(fixtureDef); body.createFixture(fixtureDef);
fixtureDef.shape.dispose(); fixtureDef.shape.dispose();
} }