diff --git a/core/src/com/me/asteroids/components/AccelerationComponent.java b/core/src/com/me/asteroids/components/AccelerationComponent.java new file mode 100644 index 0000000..ff84783 --- /dev/null +++ b/core/src/com/me/asteroids/components/AccelerationComponent.java @@ -0,0 +1,10 @@ +package com.me.asteroids.components; + +import com.badlogic.gdx.math.Vector2; +import com.me.common.ecs.Component; + +public class AccelerationComponent extends Component { + + public Vector2 acceleration; + +} diff --git a/core/src/com/me/asteroids/components/PositionComponent.java b/core/src/com/me/asteroids/components/PositionComponent.java new file mode 100644 index 0000000..c708848 --- /dev/null +++ b/core/src/com/me/asteroids/components/PositionComponent.java @@ -0,0 +1,11 @@ +package com.me.asteroids.components; + +import com.badlogic.gdx.math.Vector2; +import com.me.common.ecs.Component; + +public class PositionComponent extends Component { + + public Vector2 position; + public float rotation; + +} diff --git a/core/src/com/me/asteroids/components/VelocityComponent.java b/core/src/com/me/asteroids/components/VelocityComponent.java new file mode 100644 index 0000000..17b5a77 --- /dev/null +++ b/core/src/com/me/asteroids/components/VelocityComponent.java @@ -0,0 +1,11 @@ +package com.me.asteroids.components; + +import com.badlogic.gdx.math.Vector2; +import com.me.common.ecs.Component; + +public class VelocityComponent extends Component { + + public Vector2 velocity; + public float maxVelocity; + +} diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java new file mode 100644 index 0000000..d472165 --- /dev/null +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -0,0 +1,38 @@ +package com.me.asteroids.systems; + +import com.badlogic.gdx.math.Vector2; +import com.me.asteroids.components.AccelerationComponent; +import com.me.asteroids.components.PositionComponent; +import com.me.asteroids.components.VelocityComponent; +import com.me.common.ecs.Entity; +import com.me.common.ecs.EntitySystem; + +public class MovementSystem extends EntitySystem { + + private Vector2 tmp = new Vector2(); + + public MovementSystem() { + super(PositionComponent.class, VelocityComponent.class); + } + + @Override + public void processEntity(Entity entity, float dt) { + PositionComponent positionComponent = entity.getComponent(PositionComponent.class); + VelocityComponent velocityComponent = entity.getComponent(VelocityComponent.class); + + Vector2 velocity = velocityComponent.velocity; + float maxVelocity = velocityComponent.maxVelocity; + Vector2 position = positionComponent.position; + + AccelerationComponent accelComponent = entity.getComponent(AccelerationComponent.class); + if (accelComponent != null && !accelComponent.acceleration.isZero()) { + velocity.add(tmp.set(accelComponent.acceleration).scl(dt)); + } + + if (!velocity.isZero()) { + velocity.clamp(0, maxVelocity); + position.add(tmp.set(velocity).scl(dt)); + } + } + +}