Add angularVelocity to VelocityComponent

Use it to control player rotation.
This commit is contained in:
Matt Low 2020-01-28 23:03:45 +04:00
parent 3918f8c1f5
commit 3ffb274e8d
3 changed files with 18 additions and 8 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;