Move all ComponentMappers to Components as public static constants

This commit is contained in:
Matt Low 2020-02-01 16:05:12 +04:00
parent 6ae11ef2d3
commit 69d5caf24d
11 changed files with 114 additions and 117 deletions

View File

@ -0,0 +1,44 @@
package com.me.asteroids;
import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.AsteroidComponent;
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;
import com.me.asteroids.components.PlayerComponent;
import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent;
import com.me.common.ecs.ComponentMapper;
public class Components {
public static final ComponentMapper<AccelerationComponent> ACCELERATION;
public static final ComponentMapper<AsteroidComponent> ASTEROID;
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;
public static final ComponentMapper<PlayerComponent> PLAYER;
public static final ComponentMapper<PositionComponent> POSITION;
public static final ComponentMapper<VelocityComponent> VELOCITY;
static {
ACCELERATION = ComponentMapper.getFor(AccelerationComponent.class);
ASTEROID = ComponentMapper.getFor(AsteroidComponent.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);
PLAYER = ComponentMapper.getFor(PlayerComponent.class);
POSITION = ComponentMapper.getFor(PositionComponent.class);
VELOCITY = ComponentMapper.getFor(VelocityComponent.class);
}
}

View File

@ -35,12 +35,18 @@ import com.me.asteroids.systems.MovementSystem;
import com.me.asteroids.systems.PlayerInputSystem; import com.me.asteroids.systems.PlayerInputSystem;
import com.me.asteroids.systems.ScreenWrapSystem; import com.me.asteroids.systems.ScreenWrapSystem;
import com.me.common.Screen; import com.me.common.Screen;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.event.EventHandler; import com.me.common.ecs.event.EventHandler;
import com.me.common.ecs.event.Listener; import com.me.common.ecs.event.Listener;
import static com.me.asteroids.Components.ASTEROID;
import static com.me.asteroids.Components.BULLET;
import static com.me.asteroids.Components.DEBRIS;
import static com.me.asteroids.Components.MODEL;
import static com.me.asteroids.Components.PLAYER;
import static com.me.asteroids.Components.POSITION;
public class GameScreen extends Screen implements Listener { public class GameScreen extends Screen implements Listener {
Engine engine; Engine engine;
@ -86,9 +92,7 @@ public class GameScreen extends Screen implements Listener {
engine.registerSystem(new GameDataRenderSystem(engine, graphics, font)); engine.registerSystem(new GameDataRenderSystem(engine, graphics, font));
engine.registerListener(system); engine.registerListener(system);
engine.registerListener(this.new EventListener(engine)); engine.registerListener(this.new EventListener());
engine.ready();
Entity gameData = engine.createEntity(); Entity gameData = engine.createEntity();
gameData.addComponent(new GameDataComponent()); gameData.addComponent(new GameDataComponent());
@ -106,7 +110,7 @@ public class GameScreen extends Screen implements Listener {
if (Constants.DEBUG) { if (Constants.DEBUG) {
batch.begin(); batch.begin();
font.setColor(Color.RED); font.setColor(Color.RED);
font.draw(batch, String.format("FPS: %d, Entities: %d", Gdx.graphics.getFramesPerSecond(), engine.getEntityCount()), 15, 15 + font.getCapHeight()); font.draw(batch, String.format("FPS: %d, Entities: %d", Gdx.graphics.getFramesPerSecond(), engine.getEntities().size), 15, 15 + font.getCapHeight());
batch.end(); batch.end();
} }
} }
@ -118,25 +122,9 @@ public class GameScreen extends Screen implements Listener {
private class EventListener implements Listener { private class EventListener implements Listener {
ComponentMapper<PositionComponent> positionMapper;
ComponentMapper<ModelComponent> modelMapper;
ComponentMapper<BulletComponent> bulletMapper;
ComponentMapper<DebrisComponent> debrisMapper;
ComponentMapper<AsteroidComponent> asteroidMapper;
ComponentMapper<PlayerComponent> playerMapper;
EventListener(Engine engine) {
this.positionMapper = engine.getComponentMapper(PositionComponent.class);
this.modelMapper = engine.getComponentMapper(ModelComponent.class);
this.bulletMapper = engine.getComponentMapper(BulletComponent.class);
this.debrisMapper = engine.getComponentMapper(DebrisComponent.class);
this.asteroidMapper = engine.getComponentMapper(AsteroidComponent.class);
this.playerMapper = engine.getComponentMapper(PlayerComponent.class);
}
@EventHandler @EventHandler
public void onScreenWrap(ScreenWrapEvent event) { public void onScreenWrap(ScreenWrapEvent event) {
if (bulletMapper.has(event.entity) || debrisMapper.has(event.entity)) { if (BULLET.has(event.entity) || DEBRIS.has(event.entity)) {
// Remove bullets when they leave the screen // Remove bullets when they leave the screen
event.setCancelled(true); event.setCancelled(true);
event.entity.remove(); event.entity.remove();
@ -145,13 +133,13 @@ public class GameScreen extends Screen implements Listener {
@EventHandler @EventHandler
public void onBulletAsteroidCollision(BulletAsteroidCollisionEvent event) { public void onBulletAsteroidCollision(BulletAsteroidCollisionEvent event) {
Vector2 bulletPosition = positionMapper.get(event.getBullet()).position; Vector2 bulletPosition = POSITION.get(event.getBullet()).position;
PolygonModel model = (PolygonModel) modelMapper.get(event.getAsteroid()).model; PolygonModel model = (PolygonModel) MODEL.get(event.getAsteroid()).model;
if (model.contains(bulletPosition)) { if (model.contains(bulletPosition)) {
// AABBs intersect but let's only consider it a hit if the bullet's position // AABBs intersect but let's only consider it a hit if the bullet's position
// is actually inside the asteroid // is actually inside the asteroid
AsteroidComponent asteroid = asteroidMapper.get(event.getAsteroid()); AsteroidComponent asteroid = ASTEROID.get(event.getAsteroid());
engine.callEvent(new AsteroidHitEvent(asteroid)); engine.callEvent(new AsteroidHitEvent(asteroid));
event.getBullet().remove(); event.getBullet().remove();
if (asteroid.generation < 2) { if (asteroid.generation < 2) {
@ -169,12 +157,12 @@ public class GameScreen extends Screen implements Listener {
@EventHandler @EventHandler
public void onPlayerAsteroidCollision(PlayerASteroidCollisionEvent event) { public void onPlayerAsteroidCollision(PlayerASteroidCollisionEvent event) {
PolygonModel asteroid = (PolygonModel) modelMapper.get(event.getAsteroid()).model; PolygonModel asteroid = (PolygonModel) MODEL.get(event.getAsteroid()).model;
PolygonModel player = (PolygonModel) modelMapper.get(event.getPlayer()).model; PolygonModel player = (PolygonModel) MODEL.get(event.getPlayer()).model;
if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) { if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) {
engine.callEvent(new PlayerDeathEvent(event.getPlayer())); engine.callEvent(new PlayerDeathEvent(event.getPlayer()));
playerMapper.get(event.getPlayer()).afterBurner.deactivate(); PLAYER.get(event.getPlayer()).afterBurner.deactivate();
event.getPlayer().deactivate(); event.getPlayer().deactivate();
for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) { for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) {
debris.activate(); debris.activate();

View File

@ -6,33 +6,24 @@ import com.badlogic.gdx.utils.Array;
import com.me.asteroids.Constants; import com.me.asteroids.Constants;
import com.me.asteroids.EntityFactory; import com.me.asteroids.EntityFactory;
import com.me.asteroids.Utils; import com.me.asteroids.Utils;
import com.me.asteroids.components.AsteroidComponent;
import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent; import com.me.asteroids.components.VelocityComponent;
import com.me.asteroids.components.model.Model; import com.me.asteroids.components.model.Model;
import com.me.common.ecs.BaseSystem; import com.me.common.ecs.BaseSystem;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
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.MODEL;
import static com.me.asteroids.Components.POSITION;
import static com.me.asteroids.Components.VELOCITY;
import static com.me.asteroids.Constants.rand; import static com.me.asteroids.Constants.rand;
public class AsteroidSpawningSystem extends BaseSystem { public class AsteroidSpawningSystem extends BaseSystem {
private ComponentMapper<AsteroidComponent> asteroidMapper;
private ComponentMapper<PositionComponent> positionMapper;
private ComponentMapper<VelocityComponent> velocityMapper;
private ComponentMapper<ModelComponent> modelMapper;
private float asteroidSpawnDelay = 0f; private float asteroidSpawnDelay = 0f;
public AsteroidSpawningSystem(Engine engine) { public AsteroidSpawningSystem(Engine engine) {
super(engine); super(engine);
asteroidMapper = engine.getComponentMapper(AsteroidComponent.class);
positionMapper = engine.getComponentMapper(PositionComponent.class);
velocityMapper = engine.getComponentMapper(VelocityComponent.class);
modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
private Vector2 getRandomSpawnLocation(float asteroidWidth, float asteroidHeight) { private Vector2 getRandomSpawnLocation(float asteroidWidth, float asteroidHeight) {
@ -55,15 +46,15 @@ public class AsteroidSpawningSystem extends BaseSystem {
private void spawnAsteroid() { private void spawnAsteroid() {
Entity asteroid = EntityFactory.createAsteroid(engine); Entity asteroid = EntityFactory.createAsteroid(engine);
Model model = modelMapper.get(asteroid).model; Model model = MODEL.get(asteroid).model;
Rectangle aabb = model.getBoundingBox(); Rectangle aabb = model.getBoundingBox();
Vector2 position Vector2 position
= positionMapper.get(asteroid).position = POSITION.get(asteroid).position
= getRandomSpawnLocation(aabb.getWidth(), aabb.getHeight()); = getRandomSpawnLocation(aabb.getWidth(), aabb.getHeight());
model.setPosition(position); model.setPosition(position);
VelocityComponent velocityComponent = velocityMapper.get(asteroid); VelocityComponent velocityComponent = VELOCITY.get(asteroid);
velocityComponent.velocity = new Vector2().setToRandomDirection().scl(rand.nextFloat(1.875f, 3.125f)); velocityComponent.velocity = new Vector2().setToRandomDirection().scl(rand.nextFloat(1.875f, 3.125f));
velocityComponent.angularVelocity = rand.nextFloat(-30, 30); velocityComponent.angularVelocity = rand.nextFloat(-30, 30);
@ -78,7 +69,7 @@ public class AsteroidSpawningSystem extends BaseSystem {
// It's rather inefficient to have to check our entire entity list every frame // It's rather inefficient to have to check our entire entity list every frame
// to count how many entities have a specific component. Maybe we should keep a count of // to count how many entities have a specific component. Maybe we should keep a count of
// how many entites a given component? // how many entites a given component?
if (asteroidMapper.has(entity)) { if (ASTEROID.has(entity)) {
asteroidCount++; asteroidCount++;
} }
} }

View File

@ -4,18 +4,16 @@ 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.ColliderComponent;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
public class CollisionSystem extends EntitySystem { import static com.me.asteroids.Components.MODEL;
private ComponentMapper<ModelComponent> modelMapper; public class CollisionSystem extends EntitySystem {
public CollisionSystem(Engine engine) { public CollisionSystem(Engine engine) {
super(engine, ColliderComponent.class, ModelComponent.class); super(engine, ColliderComponent.class, ModelComponent.class);
modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
@Override @Override
@ -27,15 +25,14 @@ public class CollisionSystem extends EntitySystem {
continue; continue;
} }
Rectangle aabbA = modelMapper.get(entityA).model.getBoundingBox(); Rectangle aabbA = MODEL.get(entityA).model.getBoundingBox();
for (int j = i + 1; j < n; j++) { for (int j = i + 1; j < n; j++) {
Entity entityB = entities[j]; Entity entityB = entities[j];
if (!entityB.isActive()) { if (!entityB.isActive()) {
continue; continue;
} }
Rectangle aabbB = modelMapper.get(entityB).model.getBoundingBox(); Rectangle aabbB = MODEL.get(entityB).model.getBoundingBox();
if (aabbA.overlaps(aabbB)) { if (aabbA.overlaps(aabbB)) {
engine.callEvent(EventFactory.getNewCollisionEvent(entityA, entityB)); engine.callEvent(EventFactory.getNewCollisionEvent(entityA, entityB));
} }

View File

@ -1,30 +1,27 @@
package com.me.asteroids.systems; package com.me.asteroids.systems;
import com.me.asteroids.components.DecayComponent; import com.me.asteroids.components.DecayComponent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
public class DecaySystem extends EntitySystem { import static com.me.asteroids.Components.DECAY;
private ComponentMapper<DecayComponent> decayTimerMapper; public class DecaySystem extends EntitySystem {
public DecaySystem(Engine engine) { public DecaySystem(Engine engine) {
super(engine, DecayComponent.class); super(engine, DecayComponent.class);
decayTimerMapper = engine.getComponentMapper(DecayComponent.class);
} }
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
DecayComponent decayComponent = decayTimerMapper.get(entity); DecayComponent decayComponent = DECAY.get(entity);
if ((decayComponent.decayTimer -= dt) <= 0) { if ((decayComponent.decayTimer -= dt) <= 0) {
if (decayComponent.remove) { if (decayComponent.remove) {
entity.remove(); entity.remove();
} else { } else {
entity.deactivate(); entity.deactivate();
} }
} }
} }

View File

@ -7,14 +7,13 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.me.asteroids.Constants; import com.me.asteroids.Constants;
import com.me.asteroids.Graphics; import com.me.asteroids.Graphics;
import com.me.asteroids.components.GameDataComponent; import com.me.asteroids.components.GameDataComponent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
public class GameDataRenderSystem extends EntitySystem { import static com.me.asteroids.Components.GAME_DATA;
private ComponentMapper<GameDataComponent> gameDataMapper; public class GameDataRenderSystem extends EntitySystem {
private SpriteBatch batch; private SpriteBatch batch;
private BitmapFont font; private BitmapFont font;
@ -24,8 +23,7 @@ public class GameDataRenderSystem extends EntitySystem {
public GameDataRenderSystem(Engine engine, Graphics graphics, BitmapFont font) { public GameDataRenderSystem(Engine engine, Graphics graphics, BitmapFont font) {
super(engine, GameDataComponent.class); super(engine, GameDataComponent.class);
gameDataMapper = engine.getComponentMapper(GameDataComponent.class); this.batch = graphics.getUISpriteBatch();
batch = graphics.getUISpriteBatch();
this.font = font; this.font = font;
this.gameOverLayout = new GlyphLayout(font, "GAME OVER"); this.gameOverLayout = new GlyphLayout(font, "GAME OVER");
} }
@ -38,7 +36,7 @@ public class GameDataRenderSystem extends EntitySystem {
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
if(gameData == null) { if(gameData == null) {
gameData = gameDataMapper.get(entity); gameData = GAME_DATA.get(entity);
} }
switch (gameData.gameMode) { switch (gameData.gameMode) {

View File

@ -4,31 +4,30 @@ import com.me.asteroids.Constants;
import com.me.asteroids.GameMode; import com.me.asteroids.GameMode;
import com.me.asteroids.components.GameDataComponent; import com.me.asteroids.components.GameDataComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent;
import com.me.asteroids.events.AsteroidHitEvent; import com.me.asteroids.events.AsteroidHitEvent;
import com.me.asteroids.events.PlayerDeathEvent; import com.me.asteroids.events.PlayerDeathEvent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
import com.me.common.ecs.event.EventHandler; import com.me.common.ecs.event.EventHandler;
import com.me.common.ecs.event.Listener; import com.me.common.ecs.event.Listener;
public class GameDataSystem extends EntitySystem implements Listener { import static com.me.asteroids.Components.GAME_DATA;
import static com.me.asteroids.Components.POSITION;
import static com.me.asteroids.Components.VELOCITY;
private ComponentMapper<GameDataComponent> gameDataMapper; public class GameDataSystem extends EntitySystem implements Listener {
private GameDataComponent gameData; private GameDataComponent gameData;
public GameDataSystem(Engine engine) { public GameDataSystem(Engine engine) {
super(engine, GameDataComponent.class); super(engine, GameDataComponent.class);
gameDataMapper = engine.getComponentMapper(GameDataComponent.class);
} }
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
if(gameData == null) { if(gameData == null) {
gameData = gameDataMapper.get(entity); gameData = GAME_DATA.get(entity);
} }
if (gameData.gameModeTimer > 0 && (gameData.gameModeTimer -= dt) < 0) { if (gameData.gameModeTimer > 0 && (gameData.gameModeTimer -= dt) < 0) {
@ -87,10 +86,10 @@ public class GameDataSystem extends EntitySystem implements Listener {
private void resetPlayer() { private void resetPlayer() {
Entity player = engine.getEntities().get(1); Entity player = engine.getEntities().get(1);
PositionComponent position = player.getComponent(PositionComponent.class); PositionComponent position = POSITION.get(player);
position.rotation = 90; position.rotation = 90;
position.position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT); position.position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
player.getComponent(VelocityComponent.class).velocity.set(0, 0); VELOCITY.get(player).velocity.set(0, 0);
player.activate(); player.activate();
} }

View File

@ -4,20 +4,18 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.me.asteroids.Graphics; import com.me.asteroids.Graphics;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.model.Model; import com.me.asteroids.components.model.Model;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
public class ModelRenderSystem extends EntitySystem { import static com.me.asteroids.Components.MODEL;
private ComponentMapper<ModelComponent> modelMapper; public class ModelRenderSystem extends EntitySystem {
private ShapeRenderer renderer; private ShapeRenderer renderer;
public ModelRenderSystem(Engine engine, Graphics graphics) { public ModelRenderSystem(Engine engine, Graphics graphics) {
super(engine, ModelComponent.class); super(engine, ModelComponent.class);
this.modelMapper = engine.getComponentMapper(ModelComponent.class);
this.renderer = graphics.getShapeRenderer(); this.renderer = graphics.getShapeRenderer();
} }
@ -28,7 +26,7 @@ public class ModelRenderSystem extends EntitySystem {
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
Model model = modelMapper.get(entity).model; Model model = MODEL.get(entity).model;
renderer.setColor(model.getColor()); renderer.setColor(model.getColor());
model.render(renderer); model.render(renderer);

View File

@ -8,37 +8,31 @@ import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent; import com.me.asteroids.components.VelocityComponent;
import com.me.asteroids.components.model.Model; import com.me.asteroids.components.model.Model;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
import static com.me.asteroids.Components.ACCELERATION;
import static com.me.asteroids.Components.MODEL;
import static com.me.asteroids.Components.POSITION;
import static com.me.asteroids.Components.VELOCITY;
public class MovementSystem extends EntitySystem { public class MovementSystem extends EntitySystem {
private Vector2 tmp = new Vector2(); private Vector2 tmp = new Vector2();
private ComponentMapper<PositionComponent> positionMapper;
private ComponentMapper<VelocityComponent> velocityMapper;
private ComponentMapper<AccelerationComponent> accelMapper;
private ComponentMapper<ModelComponent> modelMapper;
public MovementSystem(Engine engine) { public MovementSystem(Engine engine) {
super(engine, PositionComponent.class, VelocityComponent.class); super(engine, PositionComponent.class, VelocityComponent.class);
positionMapper = engine.getComponentMapper(PositionComponent.class);
velocityMapper = engine.getComponentMapper(VelocityComponent.class);
accelMapper = engine.getComponentMapper(AccelerationComponent.class);
modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = positionMapper.get(entity); PositionComponent positionComponent = POSITION.get(entity);
VelocityComponent velocityComponent = velocityMapper.get(entity); VelocityComponent velocityComponent = VELOCITY.get(entity);
Vector2 velocity = velocityComponent.velocity; Vector2 velocity = velocityComponent.velocity;
AccelerationComponent accelComponent = accelMapper.get(entity); 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)); velocity.add(tmp.set(accelComponent.acceleration).scl(dt));
} }
@ -55,7 +49,7 @@ public class MovementSystem extends EntitySystem {
positionComponent.rotation = Utils.wrapAngle(positionComponent.rotation + (angularVelocity * dt)); positionComponent.rotation = Utils.wrapAngle(positionComponent.rotation + (angularVelocity * dt));
} }
ModelComponent modelComponent = modelMapper.get(entity); ModelComponent modelComponent = MODEL.get(entity);
if (modelComponent != null) { if (modelComponent != null) {
Model model = modelComponent.model; Model model = modelComponent.model;
model.setPosition(position); model.setPosition(position);

View File

@ -6,26 +6,24 @@ import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.EntityFactory; import com.me.asteroids.EntityFactory;
import com.me.asteroids.Utils; import com.me.asteroids.Utils;
import com.me.asteroids.components.AccelerationComponent; import com.me.asteroids.components.AccelerationComponent;
import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PlayerComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.components.VelocityComponent; import com.me.asteroids.components.VelocityComponent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
import static com.me.asteroids.Constants.rand; import static com.me.asteroids.Constants.rand;
import static com.me.asteroids.Components.ACCELERATION;
import static com.me.asteroids.Components.MODEL;
import static com.me.asteroids.Components.PLAYER;
import static com.me.asteroids.Components.POSITION;
import static com.me.asteroids.Components.VELOCITY;
public class PlayerInputSystem extends EntitySystem { public class PlayerInputSystem extends EntitySystem {
public Vector2 tmp = new Vector2(0, 1); public Vector2 tmp = new Vector2(0, 1);
private ComponentMapper<PositionComponent> positionMapper;
private ComponentMapper<VelocityComponent> velocityMapper;
private ComponentMapper<AccelerationComponent> accelMapper;
private ComponentMapper<PlayerComponent> playerMapper;
public PlayerInputSystem(Engine engine) { public PlayerInputSystem(Engine engine) {
super( super(
engine, engine,
@ -34,18 +32,14 @@ public class PlayerInputSystem extends EntitySystem {
AccelerationComponent.class, AccelerationComponent.class,
PlayerComponent.class PlayerComponent.class
); );
positionMapper = engine.getComponentMapper(PositionComponent.class);
velocityMapper = engine.getComponentMapper(VelocityComponent.class);
accelMapper = engine.getComponentMapper(AccelerationComponent.class);
playerMapper = engine.getComponentMapper(PlayerComponent.class);
} }
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = positionMapper.get(entity); PositionComponent positionComponent = POSITION.get(entity);
VelocityComponent velocityComponent = velocityMapper.get(entity); VelocityComponent velocityComponent = VELOCITY.get(entity);
AccelerationComponent accelComponent = accelMapper.get(entity); AccelerationComponent accelComponent = ACCELERATION.get(entity);
PlayerComponent playerComponent = playerMapper.get(entity); PlayerComponent playerComponent = PLAYER.get(entity);
boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A); boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A);
boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D); boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D);
@ -60,13 +54,13 @@ public class PlayerInputSystem extends EntitySystem {
Vector2 acceleration = accelComponent.acceleration; Vector2 acceleration = accelComponent.acceleration;
Vector2 velocity = velocityComponent.velocity; Vector2 velocity = velocityComponent.velocity;
playerComponent.afterBurner.getComponent(ModelComponent.class).model.setPosition(positionComponent.position); MODEL.get(playerComponent.afterBurner).model.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)); acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f));
if (rand.nextFloat() < 0.85) { if (rand.nextFloat() < 0.85) {
PositionComponent afterBurnerPosition = positionMapper.get(playerComponent.afterBurner); PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner);
afterBurnerPosition.position.set(positionComponent.position); afterBurnerPosition.position.set(positionComponent.position);
afterBurnerPosition.rotation = positionComponent.rotation; afterBurnerPosition.rotation = positionComponent.rotation;
playerComponent.afterBurner.activate(); playerComponent.afterBurner.activate();

View File

@ -6,20 +6,17 @@ import com.me.asteroids.Constants;
import com.me.asteroids.components.ModelComponent; import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.PositionComponent; import com.me.asteroids.components.PositionComponent;
import com.me.asteroids.events.ScreenWrapEvent; import com.me.asteroids.events.ScreenWrapEvent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine; import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity; import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem; import com.me.common.ecs.EntitySystem;
public class ScreenWrapSystem extends EntitySystem { import static com.me.asteroids.Components.MODEL;
import static com.me.asteroids.Components.POSITION;
private ComponentMapper<PositionComponent> positionMapper; public class ScreenWrapSystem extends EntitySystem {
private ComponentMapper<ModelComponent> modelMapper;
public ScreenWrapSystem(Engine engine) { public ScreenWrapSystem(Engine engine) {
super(engine, PositionComponent.class, ModelComponent.class); super(engine, PositionComponent.class, ModelComponent.class);
positionMapper = engine.getComponentMapper(PositionComponent.class);
modelMapper = engine.getComponentMapper(ModelComponent.class);
} }
private void updatePosition(Entity entity, Vector2 position, float newX, float newY) { private void updatePosition(Entity entity, Vector2 position, float newX, float newY) {
@ -33,8 +30,8 @@ public class ScreenWrapSystem extends EntitySystem {
@Override @Override
public void processEntity(Entity entity, float dt) { public void processEntity(Entity entity, float dt) {
Vector2 position = positionMapper.get(entity).position; Vector2 position = POSITION.get(entity).position;
Rectangle aabb = modelMapper.get(entity).model.getBoundingBox(); Rectangle aabb = MODEL.get(entity).model.getBoundingBox();
// Check top/bottom edges // Check top/bottom edges
float minY = aabb.y; float minY = aabb.y;