diff --git a/core/src/com/me/asteroids/components/VelocityComponent.java b/core/src/com/me/asteroids/components/VelocityComponent.java index b38771a..4912b1a 100644 --- a/core/src/com/me/asteroids/components/VelocityComponent.java +++ b/core/src/com/me/asteroids/components/VelocityComponent.java @@ -6,6 +6,7 @@ import com.me.common.ecs.Component; public class VelocityComponent implements Component { public Vector2 velocity; + public float angularVelocity; public float maxVelocity = Float.MAX_VALUE; diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java index ea64986..85b7d5f 100644 --- a/core/src/com/me/asteroids/systems/MovementSystem.java +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -1,6 +1,8 @@ package com.me.asteroids.systems; +import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; +import com.me.asteroids.Utils; import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.model.Model; import com.me.asteroids.components.ModelComponent; @@ -35,19 +37,24 @@ public class MovementSystem extends EntitySystem { VelocityComponent velocityComponent = velocityMapper.get(entity); Vector2 velocity = velocityComponent.velocity; - float maxVelocity = velocityComponent.maxVelocity; - Vector2 position = positionComponent.position; AccelerationComponent accelComponent = accelMapper.get(entity); if (accelComponent != null && !accelComponent.acceleration.isZero()) { velocity.add(tmp.set(accelComponent.acceleration).scl(dt)); } + float maxVelocity = velocityComponent.maxVelocity; + Vector2 position = positionComponent.position; if (!velocity.isZero()) { velocity.clamp(0, maxVelocity); position.add(tmp.set(velocity).scl(dt)); } + float angularVelocity = velocityComponent.angularVelocity; + if (!MathUtils.isZero(angularVelocity)) { + positionComponent.rotation = Utils.wrapAngle(positionComponent.rotation + (angularVelocity * dt)); + } + ModelComponent modelComponent = modelMapper.get(entity); if (modelComponent != null) { Model model = modelComponent.model; diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java index af2f3d8..72db6af 100644 --- a/core/src/com/me/asteroids/systems/PlayerInputSystem.java +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -41,12 +41,14 @@ public class PlayerInputSystem extends EntitySystem { VelocityComponent velocityComponent = velocityMapper.get(entity); AccelerationComponent accelComponent = accelMapper.get(entity); - if (Gdx.input.isKeyPressed(Input.Keys.D)) { - positionComponent.rotation = Utils.rotate(positionComponent.rotation, -300 * dt); - } - - if (Gdx.input.isKeyPressed(Input.Keys.A)) { - positionComponent.rotation = Utils.rotate(positionComponent.rotation, 300 * dt); + boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A); + boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D); + if (isLeftPressed && !isRightPressed) { + velocityComponent.angularVelocity = 300; + } else if (isRightPressed && !isLeftPressed) { + velocityComponent.angularVelocity = -300; + } else { + velocityComponent.angularVelocity = 0; } Vector2 acceleration = accelComponent.acceleration;