Change MovementSystem logic + others

- When acceleration results in a velocity vector in the opposite direction
of the current velocity, zero out velocity

- Moved maxVelocity from VelocityComponent to AccelerationComponent

-
This commit is contained in:
Matt Low 2020-02-02 01:59:49 +04:00
parent 3cf1d19c4d
commit 5c11df128f
5 changed files with 34 additions and 28 deletions

View File

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

View File

@ -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() {

View File

@ -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() {

View File

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

View File

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