Add movement/acceleration/velocity components + movement system

This commit is contained in:
Matt Low 2020-01-24 13:08:39 +04:00
parent 89a7bc031c
commit e38b538a1e
4 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package com.me.asteroids.components;
import com.badlogic.gdx.math.Vector2;
import com.me.common.ecs.Component;
public class AccelerationComponent extends Component {
public Vector2 acceleration;
}

View File

@ -0,0 +1,11 @@
package com.me.asteroids.components;
import com.badlogic.gdx.math.Vector2;
import com.me.common.ecs.Component;
public class PositionComponent extends Component {
public Vector2 position;
public float rotation;
}

View File

@ -0,0 +1,11 @@
package com.me.asteroids.components;
import com.badlogic.gdx.math.Vector2;
import com.me.common.ecs.Component;
public class VelocityComponent extends Component {
public Vector2 velocity;
public float maxVelocity;
}

View File

@ -0,0 +1,38 @@
package com.me.asteroids.systems;
import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent;
import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem;
public class MovementSystem extends EntitySystem {
private Vector2 tmp = new Vector2();
public MovementSystem() {
super(PositionComponent.class, VelocityComponent.class);
}
@Override
public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = entity.getComponent(PositionComponent.class);
VelocityComponent velocityComponent = entity.getComponent(VelocityComponent.class);
Vector2 velocity = velocityComponent.velocity;
float maxVelocity = velocityComponent.maxVelocity;
Vector2 position = positionComponent.position;
AccelerationComponent accelComponent = entity.getComponent(AccelerationComponent.class);
if (accelComponent != null && !accelComponent.acceleration.isZero()) {
velocity.add(tmp.set(accelComponent.acceleration).scl(dt));
}
if (!velocity.isZero()) {
velocity.clamp(0, maxVelocity);
position.add(tmp.set(velocity).scl(dt));
}
}
}