Compare commits

...

4 Commits

Author SHA1 Message Date
9850b52d84 Make afterburnerModel cyan 2020-02-02 02:05:30 +04:00
5c11df128f 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

-
2020-02-02 01:59:49 +04:00
3cf1d19c4d Use ComponentMappers everywhere else 2020-02-01 21:51:54 +04:00
84a353cfd2 Rename tag compontents to make it obvious they're for tagging 2020-02-01 21:15:57 +04:00
12 changed files with 84 additions and 75 deletions

View File

@ -2,9 +2,9 @@ package com.me.asteroids;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent; import com.me.asteroids.components.BulletTagComponent;
import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.ColliderTagComponent;
import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.DebrisTagComponent;
import com.me.asteroids.components.DecayComponent; import com.me.asteroids.components.DecayComponent;
import com.me.asteroids.components.GameDataComponent; import com.me.asteroids.components.GameDataComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
@ -17,9 +17,9 @@ public class Components {
public static final ComponentMapper<AccelerationComponent> ACCELERATION; public static final ComponentMapper<AccelerationComponent> ACCELERATION;
public static final ComponentMapper<AsteroidComponent> ASTEROID; public static final ComponentMapper<AsteroidComponent> ASTEROID;
public static final ComponentMapper<BulletComponent> BULLET; public static final ComponentMapper<BulletTagComponent> BULLET;
public static final ComponentMapper<ColliderComponent> COLLIDER; public static final ComponentMapper<ColliderTagComponent> COLLIDER;
public static final ComponentMapper<DebrisComponent> DEBRIS; public static final ComponentMapper<DebrisTagComponent> DEBRIS;
public static final ComponentMapper<DecayComponent> DECAY; public static final ComponentMapper<DecayComponent> DECAY;
public static final ComponentMapper<GameDataComponent> GAME_DATA; public static final ComponentMapper<GameDataComponent> GAME_DATA;
public static final ComponentMapper<ModelComponent> MODEL; public static final ComponentMapper<ModelComponent> MODEL;
@ -31,9 +31,9 @@ public class Components {
static { static {
ACCELERATION = ComponentMapper.getFor(AccelerationComponent.class); ACCELERATION = ComponentMapper.getFor(AccelerationComponent.class);
ASTEROID = ComponentMapper.getFor(AsteroidComponent.class); ASTEROID = ComponentMapper.getFor(AsteroidComponent.class);
BULLET = ComponentMapper.getFor(BulletComponent.class); BULLET = ComponentMapper.getFor(BulletTagComponent.class);
COLLIDER = ComponentMapper.getFor(ColliderComponent.class); COLLIDER = ComponentMapper.getFor(ColliderTagComponent.class);
DEBRIS = ComponentMapper.getFor(DebrisComponent.class); DEBRIS = ComponentMapper.getFor(DebrisTagComponent.class);
DECAY = ComponentMapper.getFor(DecayComponent.class); DECAY = ComponentMapper.getFor(DecayComponent.class);
GAME_DATA = ComponentMapper.getFor(GameDataComponent.class); GAME_DATA = ComponentMapper.getFor(GameDataComponent.class);
MODEL = ComponentMapper.getFor(ModelComponent.class); MODEL = ComponentMapper.getFor(ModelComponent.class);

View File

@ -5,9 +5,9 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent; import com.me.asteroids.components.BulletTagComponent;
import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.ColliderTagComponent;
import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.DebrisTagComponent;
import com.me.asteroids.components.DecayComponent; import com.me.asteroids.components.DecayComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
@ -34,8 +34,9 @@ public class EntityFactory {
private static final Vector2 tmpA = new Vector2(); private static final Vector2 tmpA = new Vector2();
private static final Vector2 tmpB = new Vector2(); private static final Vector2 tmpB = new Vector2();
private static final ColliderComponent COLLIDER = new ColliderComponent(); private static final ColliderTagComponent COLLIDER_TAG = new ColliderTagComponent();
private static final DebrisComponent DEBRIS = new DebrisComponent(); private static final DebrisTagComponent DEBRIS_TAG = new DebrisTagComponent();
private static final BulletTagComponent BULLET_TAG = new BulletTagComponent();
private static Entity createEntity(Engine engine) { private static Entity createEntity(Engine engine) {
Entity entity = engine.createEntity(); Entity entity = engine.createEntity();
@ -46,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[]{
@ -57,7 +58,7 @@ public class EntityFactory {
5/16f, -0.5f, // bottom right 5/16f, -0.5f, // bottom right
}); });
ModelComponent afterburnerModel = new ModelComponent(new LineModel(Color.BLUE)); ModelComponent afterburnerModel = new ModelComponent(new LineModel(Color.CYAN));
afterburnerModel.setVertices(new float[]{ afterburnerModel.setVertices(new float[]{
-2/16f, -5/16f, -2/16f, -5/16f,
0f, -0.8f, 0f, -0.8f,
@ -70,8 +71,8 @@ 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); player.addComponent(COLLIDER_TAG);
player.addComponent(new PlayerComponent(afterBurner)); player.addComponent(new PlayerComponent(afterBurner));
player.setTag("PLAYER"); player.setTag("PLAYER");
@ -135,7 +136,7 @@ public class EntityFactory {
debris.addComponent(new PositionComponent(new Vector2(model.getPosition()), 90)); debris.addComponent(new PositionComponent(new Vector2(model.getPosition()), 90));
debris.addComponent(velocity); debris.addComponent(velocity);
debris.addComponent(model); debris.addComponent(model);
debris.addComponent(DEBRIS); debris.addComponent(DEBRIS_TAG);
debris.addComponent(new DecayComponent(rand.nextFloat(0.5f, 2.5f))); debris.addComponent(new DecayComponent(rand.nextFloat(0.5f, 2.5f)));
entities[i] = debris; entities[i] = debris;
} }
@ -144,8 +145,8 @@ public class EntityFactory {
public static Entity createBullet(Engine engine, Entity player) { public static Entity createBullet(Engine engine, Entity player) {
float[] modelVertices = player.getComponent(ModelComponent.class).model.getVertices(); float[] modelVertices = MODEL.get(player).model.getVertices();
float rotation = player.getComponent(PositionComponent.class).rotation; float rotation = POSITION.get(player).rotation;
PositionComponent position = new PositionComponent(modelVertices[0], modelVertices[1], rotation); PositionComponent position = new PositionComponent(modelVertices[0], modelVertices[1], rotation);
ModelComponent model = new ModelComponent(new PolygonModel(Color.YELLOW)); ModelComponent model = new ModelComponent(new PolygonModel(Color.YELLOW));
@ -164,8 +165,8 @@ public class EntityFactory {
bullet.addComponent(position); bullet.addComponent(position);
bullet.addComponent(new VelocityComponent(new Vector2(direction).scl(12.5f))); bullet.addComponent(new VelocityComponent(new Vector2(direction).scl(12.5f)));
bullet.addComponent(model); bullet.addComponent(model);
bullet.addComponent(COLLIDER); bullet.addComponent(COLLIDER_TAG);
bullet.addComponent(new BulletComponent()); bullet.addComponent(BULLET_TAG);
return bullet; return bullet;
} }
@ -185,7 +186,7 @@ public class EntityFactory {
asteroid.addComponent(new PositionComponent()); asteroid.addComponent(new PositionComponent());
asteroid.addComponent(new VelocityComponent()); asteroid.addComponent(new VelocityComponent());
asteroid.addComponent(model); asteroid.addComponent(model);
asteroid.addComponent(COLLIDER); asteroid.addComponent(COLLIDER_TAG);
asteroid.addComponent(new AsteroidComponent()); asteroid.addComponent(new AsteroidComponent());
asteroid.addGroup("ASTEROIDS"); asteroid.addGroup("ASTEROIDS");
return asteroid; return asteroid;
@ -231,7 +232,7 @@ public class EntityFactory {
split.addComponent(new PositionComponent(new Vector2(chunkPosition), 90)); split.addComponent(new PositionComponent(new Vector2(chunkPosition), 90));
split.addComponent(velocity); split.addComponent(velocity);
split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1)); split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1));
split.addComponent(COLLIDER); split.addComponent(COLLIDER_TAG);
split.addGroup("ASTEROIDS"); split.addGroup("ASTEROIDS");
entities[i] = split; entities[i] = split;
} }

View File

@ -1,22 +1,23 @@
package com.me.asteroids; package com.me.asteroids;
import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent;
import com.me.asteroids.components.PlayerComponent;
import com.me.asteroids.events.BulletAsteroidCollisionEvent; import com.me.asteroids.events.BulletAsteroidCollisionEvent;
import com.me.asteroids.events.CollisionEvent; import com.me.asteroids.events.CollisionEvent;
import com.me.asteroids.events.PlayerASteroidCollisionEvent; import com.me.asteroids.events.PlayerASteroidCollisionEvent;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import static com.me.asteroids.Components.ASTEROID;
import static com.me.asteroids.Components.BULLET;
import static com.me.asteroids.Components.PLAYER;
public class EventFactory { public class EventFactory {
public static CollisionEvent getNewCollisionEvent(Entity a, Entity b) { public static CollisionEvent getNewCollisionEvent(Entity a, Entity b) {
boolean isEntityAPlayer = a.hasComponent(PlayerComponent.class); boolean isEntityAPlayer = PLAYER.has(a);
boolean isEntityBPlayer = b.hasComponent(PlayerComponent.class); boolean isEntityBPlayer = PLAYER.has(b);
boolean isEntityABullet = !isEntityAPlayer && a.hasComponent(BulletComponent.class); boolean isEntityABullet = !isEntityAPlayer && BULLET.has(a);
boolean isEntityBBullet = !isEntityBPlayer && b.hasComponent(BulletComponent.class); boolean isEntityBBullet = !isEntityBPlayer && BULLET.has(b);
boolean isEntityAAsteroid = !isEntityAPlayer && !isEntityABullet && a.hasComponent(AsteroidComponent.class); boolean isEntityAAsteroid = !isEntityAPlayer && !isEntityABullet && ASTEROID.has(a);
boolean isEntityBAsteroid = !isEntityBPlayer && !isEntityBBullet && b.hasComponent(AsteroidComponent.class); boolean isEntityBAsteroid = !isEntityBPlayer && !isEntityBBullet && ASTEROID.has(b);
if (isEntityAAsteroid || isEntityBAsteroid) { if (isEntityAAsteroid || isEntityBAsteroid) {
if (isEntityAPlayer && isEntityBAsteroid) { if (isEntityAPlayer && isEntityBAsteroid) {

View File

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

View File

@ -2,6 +2,6 @@ package com.me.asteroids.components;
import com.me.common.ecs.Component; import com.me.common.ecs.Component;
public class BulletComponent implements Component { public class BulletTagComponent implements Component {
// TODO: See PlayerComponent's TODO // TODO: See PlayerComponent's TODO
} }

View File

@ -5,5 +5,5 @@ import com.me.common.ecs.Component;
/** /**
* Add this to Entities that should be checked for collisions with other entities. * Add this to Entities that should be checked for collisions with other entities.
*/ */
public class ColliderComponent implements Component { public class ColliderTagComponent implements Component {
} }

View File

@ -2,6 +2,6 @@ package com.me.asteroids.components;
import com.me.common.ecs.Component; import com.me.common.ecs.Component;
public class DebrisComponent implements Component { public class DebrisTagComponent implements Component {
// TODO: See PlayerComponent's TODO // TODO: See PlayerComponent's TODO
} }

View File

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

View File

@ -10,9 +10,9 @@ import com.me.asteroids.EntityFactory;
import com.me.asteroids.Graphics; import com.me.asteroids.Graphics;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent; import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.BulletComponent; import com.me.asteroids.components.BulletTagComponent;
import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.ColliderTagComponent;
import com.me.asteroids.components.DebrisComponent; import com.me.asteroids.components.DebrisTagComponent;
import com.me.asteroids.components.DecayComponent; import com.me.asteroids.components.DecayComponent;
import com.me.asteroids.components.GameDataComponent; import com.me.asteroids.components.GameDataComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
@ -67,17 +67,18 @@ public class GameScreen extends Screen implements Listener {
engine = new Engine(); engine = new Engine();
engine.registerComponentClass(GameDataComponent.class); engine.registerComponentClass(BulletTagComponent.class);
engine.registerComponentClass(PlayerComponent.class); engine.registerComponentClass(ColliderTagComponent.class);
engine.registerComponentClass(BulletComponent.class); engine.registerComponentClass(DebrisTagComponent.class);
engine.registerComponentClass(AccelerationComponent.class);
engine.registerComponentClass(AsteroidComponent.class); engine.registerComponentClass(AsteroidComponent.class);
engine.registerComponentClass(DebrisComponent.class);
engine.registerComponentClass(DecayComponent.class); engine.registerComponentClass(DecayComponent.class);
engine.registerComponentClass(ColliderComponent.class); engine.registerComponentClass(GameDataComponent.class);
engine.registerComponentClass(ModelComponent.class);
engine.registerComponentClass(PlayerComponent.class);
engine.registerComponentClass(PositionComponent.class); engine.registerComponentClass(PositionComponent.class);
engine.registerComponentClass(VelocityComponent.class); engine.registerComponentClass(VelocityComponent.class);
engine.registerComponentClass(AccelerationComponent.class);
engine.registerComponentClass(ModelComponent.class);
GameDataSystem system = new GameDataSystem(engine); GameDataSystem system = new GameDataSystem(engine);

View File

@ -2,7 +2,7 @@ package com.me.asteroids.systems;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.me.asteroids.EventFactory; import com.me.asteroids.EventFactory;
import com.me.asteroids.components.ColliderComponent; import com.me.asteroids.components.ColliderTagComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
@ -13,7 +13,7 @@ import static com.me.asteroids.Components.MODEL;
public class CollisionSystem extends EntitySystem { public class CollisionSystem extends EntitySystem {
public CollisionSystem(Engine engine) { public CollisionSystem(Engine engine) {
super(engine, ColliderComponent.class, ModelComponent.class); super(engine, ColliderTagComponent.class, ModelComponent.class);
} }
@Override @Override

View File

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

View File

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