Compare commits
No commits in common. "dev" and "grouping" have entirely different histories.
@ -2,9 +2,9 @@ package com.me.asteroids;
|
||||
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.AsteroidComponent;
|
||||
import com.me.asteroids.components.BulletTagComponent;
|
||||
import com.me.asteroids.components.ColliderTagComponent;
|
||||
import com.me.asteroids.components.DebrisTagComponent;
|
||||
import com.me.asteroids.components.BulletComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.DebrisComponent;
|
||||
import com.me.asteroids.components.DecayComponent;
|
||||
import com.me.asteroids.components.GameDataComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
@ -17,9 +17,9 @@ public class Components {
|
||||
|
||||
public static final ComponentMapper<AccelerationComponent> ACCELERATION;
|
||||
public static final ComponentMapper<AsteroidComponent> ASTEROID;
|
||||
public static final ComponentMapper<BulletTagComponent> BULLET;
|
||||
public static final ComponentMapper<ColliderTagComponent> COLLIDER;
|
||||
public static final ComponentMapper<DebrisTagComponent> DEBRIS;
|
||||
public static final ComponentMapper<BulletComponent> BULLET;
|
||||
public static final ComponentMapper<ColliderComponent> COLLIDER;
|
||||
public static final ComponentMapper<DebrisComponent> DEBRIS;
|
||||
public static final ComponentMapper<DecayComponent> DECAY;
|
||||
public static final ComponentMapper<GameDataComponent> GAME_DATA;
|
||||
public static final ComponentMapper<ModelComponent> MODEL;
|
||||
@ -31,9 +31,9 @@ public class Components {
|
||||
static {
|
||||
ACCELERATION = ComponentMapper.getFor(AccelerationComponent.class);
|
||||
ASTEROID = ComponentMapper.getFor(AsteroidComponent.class);
|
||||
BULLET = ComponentMapper.getFor(BulletTagComponent.class);
|
||||
COLLIDER = ComponentMapper.getFor(ColliderTagComponent.class);
|
||||
DEBRIS = ComponentMapper.getFor(DebrisTagComponent.class);
|
||||
BULLET = ComponentMapper.getFor(BulletComponent.class);
|
||||
COLLIDER = ComponentMapper.getFor(ColliderComponent.class);
|
||||
DEBRIS = ComponentMapper.getFor(DebrisComponent.class);
|
||||
DECAY = ComponentMapper.getFor(DecayComponent.class);
|
||||
GAME_DATA = ComponentMapper.getFor(GameDataComponent.class);
|
||||
MODEL = ComponentMapper.getFor(ModelComponent.class);
|
||||
|
@ -5,9 +5,9 @@ import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.AsteroidComponent;
|
||||
import com.me.asteroids.components.BulletTagComponent;
|
||||
import com.me.asteroids.components.ColliderTagComponent;
|
||||
import com.me.asteroids.components.DebrisTagComponent;
|
||||
import com.me.asteroids.components.BulletComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.DebrisComponent;
|
||||
import com.me.asteroids.components.DecayComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.asteroids.components.PlayerComponent;
|
||||
@ -34,9 +34,8 @@ public class EntityFactory {
|
||||
private static final Vector2 tmpA = new Vector2();
|
||||
private static final Vector2 tmpB = new Vector2();
|
||||
|
||||
private static final ColliderTagComponent COLLIDER_TAG = new ColliderTagComponent();
|
||||
private static final DebrisTagComponent DEBRIS_TAG = new DebrisTagComponent();
|
||||
private static final BulletTagComponent BULLET_TAG = new BulletTagComponent();
|
||||
private static final ColliderComponent COLLIDER = new ColliderComponent();
|
||||
private static final DebrisComponent DEBRIS = new DebrisComponent();
|
||||
|
||||
private static Entity createEntity(Engine engine) {
|
||||
Entity entity = engine.createEntity();
|
||||
@ -47,7 +46,7 @@ public class EntityFactory {
|
||||
}
|
||||
|
||||
public static Entity createPlayer(Engine engine) {
|
||||
VelocityComponent velocity = new VelocityComponent(0f, 0f, 0f);
|
||||
VelocityComponent velocity = new VelocityComponent(0f, 0f, 0f, 10f);
|
||||
|
||||
ModelComponent playerModel = new ModelComponent(new PolygonModel(Color.WHITE));
|
||||
playerModel.setVertices(new float[]{
|
||||
@ -58,7 +57,7 @@ public class EntityFactory {
|
||||
5/16f, -0.5f, // bottom right
|
||||
});
|
||||
|
||||
ModelComponent afterburnerModel = new ModelComponent(new LineModel(Color.CYAN));
|
||||
ModelComponent afterburnerModel = new ModelComponent(new LineModel(Color.BLUE));
|
||||
afterburnerModel.setVertices(new float[]{
|
||||
-2/16f, -5/16f,
|
||||
0f, -0.8f,
|
||||
@ -71,8 +70,8 @@ public class EntityFactory {
|
||||
player.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90));
|
||||
player.addComponent(velocity);
|
||||
player.addComponent(playerModel);
|
||||
player.addComponent(new AccelerationComponent(10f));
|
||||
player.addComponent(COLLIDER_TAG);
|
||||
player.addComponent(new AccelerationComponent(0f, 1f));
|
||||
player.addComponent(COLLIDER);
|
||||
player.addComponent(new PlayerComponent(afterBurner));
|
||||
player.setTag("PLAYER");
|
||||
|
||||
@ -136,7 +135,7 @@ public class EntityFactory {
|
||||
debris.addComponent(new PositionComponent(new Vector2(model.getPosition()), 90));
|
||||
debris.addComponent(velocity);
|
||||
debris.addComponent(model);
|
||||
debris.addComponent(DEBRIS_TAG);
|
||||
debris.addComponent(DEBRIS);
|
||||
debris.addComponent(new DecayComponent(rand.nextFloat(0.5f, 2.5f)));
|
||||
entities[i] = debris;
|
||||
}
|
||||
@ -145,8 +144,8 @@ public class EntityFactory {
|
||||
|
||||
|
||||
public static Entity createBullet(Engine engine, Entity player) {
|
||||
float[] modelVertices = MODEL.get(player).model.getVertices();
|
||||
float rotation = POSITION.get(player).rotation;
|
||||
float[] modelVertices = player.getComponent(ModelComponent.class).model.getVertices();
|
||||
float rotation = player.getComponent(PositionComponent.class).rotation;
|
||||
|
||||
PositionComponent position = new PositionComponent(modelVertices[0], modelVertices[1], rotation);
|
||||
ModelComponent model = new ModelComponent(new PolygonModel(Color.YELLOW));
|
||||
@ -165,8 +164,8 @@ public class EntityFactory {
|
||||
bullet.addComponent(position);
|
||||
bullet.addComponent(new VelocityComponent(new Vector2(direction).scl(12.5f)));
|
||||
bullet.addComponent(model);
|
||||
bullet.addComponent(COLLIDER_TAG);
|
||||
bullet.addComponent(BULLET_TAG);
|
||||
bullet.addComponent(COLLIDER);
|
||||
bullet.addComponent(new BulletComponent());
|
||||
return bullet;
|
||||
}
|
||||
|
||||
@ -186,7 +185,7 @@ public class EntityFactory {
|
||||
asteroid.addComponent(new PositionComponent());
|
||||
asteroid.addComponent(new VelocityComponent());
|
||||
asteroid.addComponent(model);
|
||||
asteroid.addComponent(COLLIDER_TAG);
|
||||
asteroid.addComponent(COLLIDER);
|
||||
asteroid.addComponent(new AsteroidComponent());
|
||||
asteroid.addGroup("ASTEROIDS");
|
||||
return asteroid;
|
||||
@ -232,7 +231,7 @@ public class EntityFactory {
|
||||
split.addComponent(new PositionComponent(new Vector2(chunkPosition), 90));
|
||||
split.addComponent(velocity);
|
||||
split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1));
|
||||
split.addComponent(COLLIDER_TAG);
|
||||
split.addComponent(COLLIDER);
|
||||
split.addGroup("ASTEROIDS");
|
||||
entities[i] = split;
|
||||
}
|
||||
|
@ -1,23 +1,22 @@
|
||||
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.CollisionEvent;
|
||||
import com.me.asteroids.events.PlayerASteroidCollisionEvent;
|
||||
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 static CollisionEvent getNewCollisionEvent(Entity a, Entity b) {
|
||||
boolean isEntityAPlayer = PLAYER.has(a);
|
||||
boolean isEntityBPlayer = PLAYER.has(b);
|
||||
boolean isEntityABullet = !isEntityAPlayer && BULLET.has(a);
|
||||
boolean isEntityBBullet = !isEntityBPlayer && BULLET.has(b);
|
||||
boolean isEntityAAsteroid = !isEntityAPlayer && !isEntityABullet && ASTEROID.has(a);
|
||||
boolean isEntityBAsteroid = !isEntityBPlayer && !isEntityBBullet && ASTEROID.has(b);
|
||||
boolean isEntityAPlayer = a.hasComponent(PlayerComponent.class);
|
||||
boolean isEntityBPlayer = b.hasComponent(PlayerComponent.class);
|
||||
boolean isEntityABullet = !isEntityAPlayer && a.hasComponent(BulletComponent.class);
|
||||
boolean isEntityBBullet = !isEntityBPlayer && b.hasComponent(BulletComponent.class);
|
||||
boolean isEntityAAsteroid = !isEntityAPlayer && !isEntityABullet && a.hasComponent(AsteroidComponent.class);
|
||||
boolean isEntityBAsteroid = !isEntityBPlayer && !isEntityBBullet && b.hasComponent(AsteroidComponent.class);
|
||||
|
||||
if (isEntityAAsteroid || isEntityBAsteroid) {
|
||||
if (isEntityAPlayer && isEntityBAsteroid) {
|
||||
|
@ -6,23 +6,17 @@ import com.me.common.ecs.Component;
|
||||
public class AccelerationComponent implements Component {
|
||||
|
||||
public Vector2 acceleration;
|
||||
public float maxVelocity;
|
||||
|
||||
public AccelerationComponent() {
|
||||
this(0f, 0f, Float.MAX_VALUE);
|
||||
this(0f, 0f);
|
||||
}
|
||||
|
||||
public AccelerationComponent(float maxVelocity) {
|
||||
this(0f, 0f, maxVelocity);
|
||||
public AccelerationComponent(float x, float y) {
|
||||
this(new Vector2(x, y));
|
||||
}
|
||||
|
||||
public AccelerationComponent(float x, float y, float maxVelocity) {
|
||||
this(new Vector2(x, y), maxVelocity);
|
||||
}
|
||||
|
||||
public AccelerationComponent(Vector2 acceleration, float maxVelocity) {
|
||||
public AccelerationComponent(Vector2 acceleration) {
|
||||
this.acceleration = acceleration;
|
||||
this.maxVelocity = maxVelocity;
|
||||
}
|
||||
|
||||
public float getX() {
|
||||
|
@ -2,6 +2,6 @@ package com.me.asteroids.components;
|
||||
|
||||
import com.me.common.ecs.Component;
|
||||
|
||||
public class BulletTagComponent implements Component {
|
||||
public class BulletComponent implements Component {
|
||||
// TODO: See PlayerComponent's TODO
|
||||
}
|
@ -5,5 +5,5 @@ import com.me.common.ecs.Component;
|
||||
/**
|
||||
* Add this to Entities that should be checked for collisions with other entities.
|
||||
*/
|
||||
public class ColliderTagComponent implements Component {
|
||||
public class ColliderComponent implements Component {
|
||||
}
|
@ -2,6 +2,6 @@ package com.me.asteroids.components;
|
||||
|
||||
import com.me.common.ecs.Component;
|
||||
|
||||
public class DebrisTagComponent implements Component {
|
||||
public class DebrisComponent implements Component {
|
||||
// TODO: See PlayerComponent's TODO
|
||||
}
|
@ -7,6 +7,7 @@ public class VelocityComponent implements Component {
|
||||
|
||||
public Vector2 velocity;
|
||||
public float angularVelocity;
|
||||
public float maxVelocity;
|
||||
|
||||
public VelocityComponent() {
|
||||
this(0f, 0f, 0f);
|
||||
@ -21,12 +22,21 @@ public class VelocityComponent implements Component {
|
||||
}
|
||||
|
||||
public VelocityComponent(float x, float y, float angularVelocity) {
|
||||
this(new Vector2(x, y), 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);
|
||||
}
|
||||
|
||||
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() {
|
||||
|
@ -10,9 +10,9 @@ import com.me.asteroids.EntityFactory;
|
||||
import com.me.asteroids.Graphics;
|
||||
import com.me.asteroids.components.AccelerationComponent;
|
||||
import com.me.asteroids.components.AsteroidComponent;
|
||||
import com.me.asteroids.components.BulletTagComponent;
|
||||
import com.me.asteroids.components.ColliderTagComponent;
|
||||
import com.me.asteroids.components.DebrisTagComponent;
|
||||
import com.me.asteroids.components.BulletComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.DebrisComponent;
|
||||
import com.me.asteroids.components.DecayComponent;
|
||||
import com.me.asteroids.components.GameDataComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
@ -67,18 +67,17 @@ public class GameScreen extends Screen implements Listener {
|
||||
|
||||
engine = new Engine();
|
||||
|
||||
engine.registerComponentClass(BulletTagComponent.class);
|
||||
engine.registerComponentClass(ColliderTagComponent.class);
|
||||
engine.registerComponentClass(DebrisTagComponent.class);
|
||||
|
||||
engine.registerComponentClass(AccelerationComponent.class);
|
||||
engine.registerComponentClass(AsteroidComponent.class);
|
||||
engine.registerComponentClass(DecayComponent.class);
|
||||
engine.registerComponentClass(GameDataComponent.class);
|
||||
engine.registerComponentClass(ModelComponent.class);
|
||||
engine.registerComponentClass(PlayerComponent.class);
|
||||
engine.registerComponentClass(BulletComponent.class);
|
||||
engine.registerComponentClass(AsteroidComponent.class);
|
||||
engine.registerComponentClass(DebrisComponent.class);
|
||||
engine.registerComponentClass(DecayComponent.class);
|
||||
engine.registerComponentClass(ColliderComponent.class);
|
||||
engine.registerComponentClass(PositionComponent.class);
|
||||
engine.registerComponentClass(VelocityComponent.class);
|
||||
engine.registerComponentClass(AccelerationComponent.class);
|
||||
engine.registerComponentClass(ModelComponent.class);
|
||||
|
||||
GameDataSystem system = new GameDataSystem(engine);
|
||||
|
||||
|
@ -2,7 +2,7 @@ package com.me.asteroids.systems;
|
||||
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
import com.me.asteroids.EventFactory;
|
||||
import com.me.asteroids.components.ColliderTagComponent;
|
||||
import com.me.asteroids.components.ColliderComponent;
|
||||
import com.me.asteroids.components.ModelComponent;
|
||||
import com.me.common.ecs.Engine;
|
||||
import com.me.common.ecs.Entity;
|
||||
@ -13,7 +13,7 @@ import static com.me.asteroids.Components.MODEL;
|
||||
public class CollisionSystem extends EntitySystem {
|
||||
|
||||
public CollisionSystem(Engine engine) {
|
||||
super(engine, ColliderTagComponent.class, ModelComponent.class);
|
||||
super(engine, ColliderComponent.class, ModelComponent.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,29 +29,18 @@ 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()) {
|
||||
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);
|
||||
}
|
||||
velocity.add(tmp.set(accelComponent.acceleration).scl(dt));
|
||||
}
|
||||
|
||||
float maxVelocity = velocityComponent.maxVelocity;
|
||||
Vector2 position = positionComponent.position;
|
||||
|
||||
if (!velocity.isZero()) {
|
||||
positionComponent.position.add(tmp.set(velocity).scl(dt));
|
||||
velocity.clamp(0, maxVelocity);
|
||||
position.add(tmp.set(velocity).scl(dt));
|
||||
}
|
||||
|
||||
float angularVelocity = velocityComponent.angularVelocity;
|
||||
|
@ -51,12 +51,13 @@ public class PlayerInputSystem extends EntitySystem {
|
||||
velocityComponent.angularVelocity = 0;
|
||||
}
|
||||
|
||||
Vector2 acceleration = accelComponent.acceleration;
|
||||
Vector2 velocity = velocityComponent.velocity;
|
||||
|
||||
MODEL.get(playerComponent.afterBurner).setPosition(positionComponent.position);
|
||||
MODEL.get(playerComponent.afterBurner).model.setPosition(positionComponent.position);
|
||||
|
||||
if (Gdx.input.isKeyPressed(Input.Keys.W)) {
|
||||
accelComponent.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f));
|
||||
acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f));
|
||||
|
||||
if (rand.nextFloat() < 0.85) {
|
||||
PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner);
|
||||
@ -68,10 +69,10 @@ public class PlayerInputSystem extends EntitySystem {
|
||||
}
|
||||
} else {
|
||||
playerComponent.afterBurner.deactivate();
|
||||
if (velocity.isZero(0f)) {
|
||||
accelComponent.set(0, 0);
|
||||
if (!velocity.isZero(1f)) {
|
||||
acceleration.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f));
|
||||
} else {
|
||||
accelComponent.set(Utils.setUnitVectorAngleRad(tmp, velocity.angleRad()).scl(-2.5f));
|
||||
acceleration.set(0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user