diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index ca10990..f05f44d 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -47,7 +47,7 @@ public class EntityFactory { } public static Entity createPlayer(Engine engine) { - VelocityComponent velocity = new VelocityComponent(0f, 0f, 0f, 10f); + VelocityComponent velocity = new VelocityComponent(0f, 0f, 0f); ModelComponent playerModel = new ModelComponent(new PolygonModel(Color.WHITE)); playerModel.setVertices(new float[]{ @@ -71,7 +71,7 @@ public class EntityFactory { player.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90)); player.addComponent(velocity); player.addComponent(playerModel); - player.addComponent(new AccelerationComponent(0f, 1f)); + player.addComponent(new AccelerationComponent(10f)); player.addComponent(COLLIDER_TAG); player.addComponent(new PlayerComponent(afterBurner)); player.setTag("PLAYER"); diff --git a/core/src/com/me/asteroids/components/AccelerationComponent.java b/core/src/com/me/asteroids/components/AccelerationComponent.java index fab120f..25f1e2e 100644 --- a/core/src/com/me/asteroids/components/AccelerationComponent.java +++ b/core/src/com/me/asteroids/components/AccelerationComponent.java @@ -6,17 +6,23 @@ import com.me.common.ecs.Component; public class AccelerationComponent implements Component { public Vector2 acceleration; + public float maxVelocity; public AccelerationComponent() { - this(0f, 0f); + this(0f, 0f, Float.MAX_VALUE); } - public AccelerationComponent(float x, float y) { - this(new Vector2(x, y)); + public AccelerationComponent(float maxVelocity) { + this(0f, 0f, maxVelocity); } - public AccelerationComponent(Vector2 acceleration) { + public AccelerationComponent(float x, float y, float maxVelocity) { + this(new Vector2(x, y), maxVelocity); + } + + public AccelerationComponent(Vector2 acceleration, float maxVelocity) { this.acceleration = acceleration; + this.maxVelocity = maxVelocity; } public float getX() { diff --git a/core/src/com/me/asteroids/components/VelocityComponent.java b/core/src/com/me/asteroids/components/VelocityComponent.java index e5f983c..e9ec1b1 100644 --- a/core/src/com/me/asteroids/components/VelocityComponent.java +++ b/core/src/com/me/asteroids/components/VelocityComponent.java @@ -7,7 +7,6 @@ public class VelocityComponent implements Component { public Vector2 velocity; public float angularVelocity; - public float maxVelocity; public VelocityComponent() { this(0f, 0f, 0f); @@ -22,21 +21,12 @@ public class VelocityComponent implements Component { } public VelocityComponent(float x, float y, float angularVelocity) { - this(x, y, angularVelocity, Float.MAX_VALUE); - } - - public VelocityComponent(float x, float y, float angularVelocity, float maxVelocity) { - this(new Vector2(x, y), angularVelocity, maxVelocity); + this(new Vector2(x, y), angularVelocity); } public VelocityComponent(Vector2 velocity, float angularVelocity) { - this(velocity, angularVelocity, Float.MAX_VALUE); - } - - public VelocityComponent(Vector2 velocity, float angularVelocity, float maxVelocity) { this.velocity = velocity; this.angularVelocity = angularVelocity; - this.maxVelocity = maxVelocity; } public float getX() { diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java index ed1a361..d5bdf0c 100644 --- a/core/src/com/me/asteroids/systems/MovementSystem.java +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -29,18 +29,29 @@ public class MovementSystem extends EntitySystem { public void processEntity(Entity entity, float dt) { PositionComponent positionComponent = POSITION.get(entity); VelocityComponent velocityComponent = VELOCITY.get(entity); + AccelerationComponent accelComponent = ACCELERATION.get(entity); + Vector2 velocity = velocityComponent.velocity; - AccelerationComponent accelComponent = ACCELERATION.get(entity); if (accelComponent != null && !accelComponent.acceleration.isZero()) { - velocity.add(tmp.set(accelComponent.acceleration).scl(dt)); + tmp.set(accelComponent.acceleration).scl(dt).add(velocity); + + if (velocity.dot(tmp) < 0) { + // current velocity is opposite of velocity with acceleration applied, which means + // we're coming to a stop. Set position correctly and velocity to zero + velocity.set(0, 0); + positionComponent.position.add(tmp.scl(dt)); + } else { + // Else just use the accelerated velocity + velocity.set(tmp); + velocity.clamp(0, accelComponent.maxVelocity); + } } - float maxVelocity = velocityComponent.maxVelocity; Vector2 position = positionComponent.position; + if (!velocity.isZero()) { - velocity.clamp(0, maxVelocity); - position.add(tmp.set(velocity).scl(dt)); + positionComponent.position.add(tmp.set(velocity).scl(dt)); } float angularVelocity = velocityComponent.angularVelocity; diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java index b3fceae..56dc330 100644 --- a/core/src/com/me/asteroids/systems/PlayerInputSystem.java +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -51,13 +51,12 @@ public class PlayerInputSystem extends EntitySystem { velocityComponent.angularVelocity = 0; } - Vector2 acceleration = accelComponent.acceleration; Vector2 velocity = velocityComponent.velocity; - MODEL.get(playerComponent.afterBurner).model.setPosition(positionComponent.position); + MODEL.get(playerComponent.afterBurner).setPosition(positionComponent.position); if (Gdx.input.isKeyPressed(Input.Keys.W)) { - acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f)); + accelComponent.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f)); if (rand.nextFloat() < 0.85) { PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner); @@ -69,10 +68,10 @@ public class PlayerInputSystem extends EntitySystem { } } else { playerComponent.afterBurner.deactivate(); - if (!velocity.isZero(1f)) { - acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f)); + if (velocity.isZero(0f)) { + accelComponent.set(0, 0); } else { - acceleration.set(0, 0); + accelComponent.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f)); } }