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.ScreenWrapSystem;
import com.me.common.Screen;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
import com.me.common.ecs.event.EventHandler;
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 {
Engine engine;
@ -86,9 +92,7 @@ public class GameScreen extends Screen implements Listener {
engine.registerSystem(new GameDataRenderSystem(engine, graphics, font));
engine.registerListener(system);
engine.registerListener(this.new EventListener(engine));
engine.ready();
engine.registerListener(this.new EventListener());
Entity gameData = engine.createEntity();
gameData.addComponent(new GameDataComponent());
@ -106,7 +110,7 @@ public class GameScreen extends Screen implements Listener {
if (Constants.DEBUG) {
batch.begin();
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();
}
}
@ -118,25 +122,9 @@ public class GameScreen extends Screen 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
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
event.setCancelled(true);
event.entity.remove();
@ -145,13 +133,13 @@ public class GameScreen extends Screen implements Listener {
@EventHandler
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)) {
// AABBs intersect but let's only consider it a hit if the bullet's position
// is actually inside the asteroid
AsteroidComponent asteroid = asteroidMapper.get(event.getAsteroid());
AsteroidComponent asteroid = ASTEROID.get(event.getAsteroid());
engine.callEvent(new AsteroidHitEvent(asteroid));
event.getBullet().remove();
if (asteroid.generation < 2) {
@ -169,12 +157,12 @@ public class GameScreen extends Screen implements Listener {
@EventHandler
public void onPlayerAsteroidCollision(PlayerASteroidCollisionEvent event) {
PolygonModel asteroid = (PolygonModel) modelMapper.get(event.getAsteroid()).model;
PolygonModel player = (PolygonModel) modelMapper.get(event.getPlayer()).model;
PolygonModel asteroid = (PolygonModel) MODEL.get(event.getAsteroid()).model;
PolygonModel player = (PolygonModel) MODEL.get(event.getPlayer()).model;
if (asteroid.contains(player.getVertices()) || player.contains(asteroid.getVertices())) {
engine.callEvent(new PlayerDeathEvent(event.getPlayer()));
playerMapper.get(event.getPlayer()).afterBurner.deactivate();
PLAYER.get(event.getPlayer()).afterBurner.deactivate();
event.getPlayer().deactivate();
for (Entity debris : EntityFactory.createDebris(engine, event.getPlayer())) {
debris.activate();

View File

@ -6,33 +6,24 @@ import com.badlogic.gdx.utils.Array;
import com.me.asteroids.Constants;
import com.me.asteroids.EntityFactory;
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.model.Model;
import com.me.common.ecs.BaseSystem;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
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;
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;
public AsteroidSpawningSystem(Engine 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) {
@ -55,15 +46,15 @@ public class AsteroidSpawningSystem extends BaseSystem {
private void spawnAsteroid() {
Entity asteroid = EntityFactory.createAsteroid(engine);
Model model = modelMapper.get(asteroid).model;
Model model = MODEL.get(asteroid).model;
Rectangle aabb = model.getBoundingBox();
Vector2 position
= positionMapper.get(asteroid).position
= POSITION.get(asteroid).position
= getRandomSpawnLocation(aabb.getWidth(), aabb.getHeight());
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.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
// to count how many entities have a specific component. Maybe we should keep a count of
// how many entites a given component?
if (asteroidMapper.has(entity)) {
if (ASTEROID.has(entity)) {
asteroidCount++;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -4,20 +4,18 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.me.asteroids.Graphics;
import com.me.asteroids.components.ModelComponent;
import com.me.asteroids.components.model.Model;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
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;
public ModelRenderSystem(Engine engine, Graphics graphics) {
super(engine, ModelComponent.class);
this.modelMapper = engine.getComponentMapper(ModelComponent.class);
this.renderer = graphics.getShapeRenderer();
}
@ -28,7 +26,7 @@ public class ModelRenderSystem extends EntitySystem {
@Override
public void processEntity(Entity entity, float dt) {
Model model = modelMapper.get(entity).model;
Model model = MODEL.get(entity).model;
renderer.setColor(model.getColor());
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.VelocityComponent;
import com.me.asteroids.components.model.Model;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
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 {
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) {
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
public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = positionMapper.get(entity);
VelocityComponent velocityComponent = velocityMapper.get(entity);
PositionComponent positionComponent = POSITION.get(entity);
VelocityComponent velocityComponent = VELOCITY.get(entity);
Vector2 velocity = velocityComponent.velocity;
AccelerationComponent accelComponent = accelMapper.get(entity);
AccelerationComponent accelComponent = ACCELERATION.get(entity);
if (accelComponent != null && !accelComponent.acceleration.isZero()) {
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));
}
ModelComponent modelComponent = modelMapper.get(entity);
ModelComponent modelComponent = MODEL.get(entity);
if (modelComponent != null) {
Model model = modelComponent.model;
model.setPosition(position);

View File

@ -6,26 +6,24 @@ import com.badlogic.gdx.math.Vector2;
import com.me.asteroids.EntityFactory;
import com.me.asteroids.Utils;
import com.me.asteroids.components.AccelerationComponent;
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;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
import com.me.common.ecs.EntitySystem;
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 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) {
super(
engine,
@ -34,18 +32,14 @@ public class PlayerInputSystem extends EntitySystem {
AccelerationComponent.class,
PlayerComponent.class
);
positionMapper = engine.getComponentMapper(PositionComponent.class);
velocityMapper = engine.getComponentMapper(VelocityComponent.class);
accelMapper = engine.getComponentMapper(AccelerationComponent.class);
playerMapper = engine.getComponentMapper(PlayerComponent.class);
}
@Override
public void processEntity(Entity entity, float dt) {
PositionComponent positionComponent = positionMapper.get(entity);
VelocityComponent velocityComponent = velocityMapper.get(entity);
AccelerationComponent accelComponent = accelMapper.get(entity);
PlayerComponent playerComponent = playerMapper.get(entity);
PositionComponent positionComponent = POSITION.get(entity);
VelocityComponent velocityComponent = VELOCITY.get(entity);
AccelerationComponent accelComponent = ACCELERATION.get(entity);
PlayerComponent playerComponent = PLAYER.get(entity);
boolean isLeftPressed = Gdx.input.isKeyPressed(Input.Keys.A);
boolean isRightPressed = Gdx.input.isKeyPressed(Input.Keys.D);
@ -60,13 +54,13 @@ public class PlayerInputSystem extends EntitySystem {
Vector2 acceleration = accelComponent.acceleration;
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)) {
acceleration.set(Utils.setUnitVectorAngle(tmp, positionComponent.rotation).scl(12.5f));
if (rand.nextFloat() < 0.85) {
PositionComponent afterBurnerPosition = positionMapper.get(playerComponent.afterBurner);
PositionComponent afterBurnerPosition = POSITION.get(playerComponent.afterBurner);
afterBurnerPosition.position.set(positionComponent.position);
afterBurnerPosition.rotation = positionComponent.rotation;
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.PositionComponent;
import com.me.asteroids.events.ScreenWrapEvent;
import com.me.common.ecs.ComponentMapper;
import com.me.common.ecs.Engine;
import com.me.common.ecs.Entity;
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;
private ComponentMapper<ModelComponent> modelMapper;
public class ScreenWrapSystem extends EntitySystem {
public ScreenWrapSystem(Engine engine) {
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) {
@ -33,8 +30,8 @@ public class ScreenWrapSystem extends EntitySystem {
@Override
public void processEntity(Entity entity, float dt) {
Vector2 position = positionMapper.get(entity).position;
Rectangle aabb = modelMapper.get(entity).model.getBoundingBox();
Vector2 position = POSITION.get(entity).position;
Rectangle aabb = MODEL.get(entity).model.getBoundingBox();
// Check top/bottom edges
float minY = aabb.y;