Add movement/acceleration/velocity components + movement system
This commit is contained in:
parent
89a7bc031c
commit
e38b538a1e
@ -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;
|
||||
|
||||
}
|
11
core/src/com/me/asteroids/components/PositionComponent.java
Normal file
11
core/src/com/me/asteroids/components/PositionComponent.java
Normal 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;
|
||||
|
||||
}
|
11
core/src/com/me/asteroids/components/VelocityComponent.java
Normal file
11
core/src/com/me/asteroids/components/VelocityComponent.java
Normal 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;
|
||||
|
||||
}
|
38
core/src/com/me/asteroids/systems/MovementSystem.java
Normal file
38
core/src/com/me/asteroids/systems/MovementSystem.java
Normal 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));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user