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:
parent
3cf1d19c4d
commit
5c11df128f
@ -47,7 +47,7 @@ public class EntityFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Entity createPlayer(Engine engine) {
|
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));
|
ModelComponent playerModel = new ModelComponent(new PolygonModel(Color.WHITE));
|
||||||
playerModel.setVertices(new float[]{
|
playerModel.setVertices(new float[]{
|
||||||
@ -71,7 +71,7 @@ public class EntityFactory {
|
|||||||
player.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90));
|
player.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90));
|
||||||
player.addComponent(velocity);
|
player.addComponent(velocity);
|
||||||
player.addComponent(playerModel);
|
player.addComponent(playerModel);
|
||||||
player.addComponent(new AccelerationComponent(0f, 1f));
|
player.addComponent(new AccelerationComponent(10f));
|
||||||
player.addComponent(COLLIDER_TAG);
|
player.addComponent(COLLIDER_TAG);
|
||||||
player.addComponent(new PlayerComponent(afterBurner));
|
player.addComponent(new PlayerComponent(afterBurner));
|
||||||
player.setTag("PLAYER");
|
player.setTag("PLAYER");
|
||||||
|
@ -6,17 +6,23 @@ import com.me.common.ecs.Component;
|
|||||||
public class AccelerationComponent implements Component {
|
public class AccelerationComponent implements Component {
|
||||||
|
|
||||||
public Vector2 acceleration;
|
public Vector2 acceleration;
|
||||||
|
public float maxVelocity;
|
||||||
|
|
||||||
public AccelerationComponent() {
|
public AccelerationComponent() {
|
||||||
this(0f, 0f);
|
this(0f, 0f, Float.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AccelerationComponent(float x, float y) {
|
public AccelerationComponent(float maxVelocity) {
|
||||||
this(new Vector2(x, y));
|
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.acceleration = acceleration;
|
||||||
|
this.maxVelocity = maxVelocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getX() {
|
public float getX() {
|
||||||
|
@ -7,7 +7,6 @@ public class VelocityComponent implements Component {
|
|||||||
|
|
||||||
public Vector2 velocity;
|
public Vector2 velocity;
|
||||||
public float angularVelocity;
|
public float angularVelocity;
|
||||||
public float maxVelocity;
|
|
||||||
|
|
||||||
public VelocityComponent() {
|
public VelocityComponent() {
|
||||||
this(0f, 0f, 0f);
|
this(0f, 0f, 0f);
|
||||||
@ -22,21 +21,12 @@ public class VelocityComponent implements Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public VelocityComponent(float x, float y, float angularVelocity) {
|
public VelocityComponent(float x, float y, float angularVelocity) {
|
||||||
this(x, y, angularVelocity, Float.MAX_VALUE);
|
this(new Vector2(x, y), angularVelocity);
|
||||||
}
|
|
||||||
|
|
||||||
public VelocityComponent(float x, float y, float angularVelocity, float maxVelocity) {
|
|
||||||
this(new Vector2(x, y), angularVelocity, maxVelocity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public VelocityComponent(Vector2 velocity, float 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.velocity = velocity;
|
||||||
this.angularVelocity = angularVelocity;
|
this.angularVelocity = angularVelocity;
|
||||||
this.maxVelocity = maxVelocity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getX() {
|
public float getX() {
|
||||||
|
@ -29,18 +29,29 @@ public class MovementSystem extends EntitySystem {
|
|||||||
public void processEntity(Entity entity, float dt) {
|
public void processEntity(Entity entity, float dt) {
|
||||||
PositionComponent positionComponent = POSITION.get(entity);
|
PositionComponent positionComponent = POSITION.get(entity);
|
||||||
VelocityComponent velocityComponent = VELOCITY.get(entity);
|
VelocityComponent velocityComponent = VELOCITY.get(entity);
|
||||||
|
AccelerationComponent accelComponent = ACCELERATION.get(entity);
|
||||||
|
|
||||||
Vector2 velocity = velocityComponent.velocity;
|
Vector2 velocity = velocityComponent.velocity;
|
||||||
|
|
||||||
AccelerationComponent accelComponent = ACCELERATION.get(entity);
|
|
||||||
if (accelComponent != null && !accelComponent.acceleration.isZero()) {
|
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;
|
Vector2 position = positionComponent.position;
|
||||||
|
|
||||||
if (!velocity.isZero()) {
|
if (!velocity.isZero()) {
|
||||||
velocity.clamp(0, maxVelocity);
|
positionComponent.position.add(tmp.set(velocity).scl(dt));
|
||||||
position.add(tmp.set(velocity).scl(dt));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float angularVelocity = velocityComponent.angularVelocity;
|
float angularVelocity = velocityComponent.angularVelocity;
|
||||||
|
@ -51,13 +51,12 @@ public class PlayerInputSystem extends EntitySystem {
|
|||||||
velocityComponent.angularVelocity = 0;
|
velocityComponent.angularVelocity = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 acceleration = accelComponent.acceleration;
|
|
||||||
Vector2 velocity = velocityComponent.velocity;
|
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)) {
|
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) {
|
if (rand.nextFloat() < 0.85) {
|
||||||
PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner);
|
PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner);
|
||||||
@ -69,10 +68,10 @@ public class PlayerInputSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
playerComponent.afterBurner.deactivate();
|
playerComponent.afterBurner.deactivate();
|
||||||
if (!velocity.isZero(1f)) {
|
if (velocity.isZero(0f)) {
|
||||||
acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f));
|
accelComponent.set(0, 0);
|
||||||
} else {
|
} else {
|
||||||
acceleration.set(0, 0);
|
accelComponent.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user