From 69d5caf24d5cbc34919870149d9bb34b20f856c8 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 1 Feb 2020 16:05:12 +0400 Subject: [PATCH] Move all ComponentMappers to Components as public static constants --- core/src/com/me/asteroids/Components.java | 44 +++++++++++++++++++ .../com/me/asteroids/screens/GameScreen.java | 44 +++++++------------ .../systems/AsteroidSpawningSystem.java | 25 ++++------- .../me/asteroids/systems/CollisionSystem.java | 11 ++--- .../com/me/asteroids/systems/DecaySystem.java | 9 ++-- .../systems/GameDataRenderSystem.java | 10 ++--- .../me/asteroids/systems/GameDataSystem.java | 15 +++---- .../asteroids/systems/ModelRenderSystem.java | 8 ++-- .../me/asteroids/systems/MovementSystem.java | 24 ++++------ .../asteroids/systems/PlayerInputSystem.java | 28 +++++------- .../asteroids/systems/ScreenWrapSystem.java | 13 +++--- 11 files changed, 114 insertions(+), 117 deletions(-) create mode 100644 core/src/com/me/asteroids/Components.java diff --git a/core/src/com/me/asteroids/Components.java b/core/src/com/me/asteroids/Components.java new file mode 100644 index 0000000..cd5d845 --- /dev/null +++ b/core/src/com/me/asteroids/Components.java @@ -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 ACCELERATION; + public static final ComponentMapper ASTEROID; + public static final ComponentMapper BULLET; + public static final ComponentMapper COLLIDER; + public static final ComponentMapper DEBRIS; + public static final ComponentMapper DECAY; + public static final ComponentMapper GAME_DATA; + public static final ComponentMapper MODEL; + public static final ComponentMapper PLAYER; + public static final ComponentMapper POSITION; + public static final ComponentMapper 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); + } +} diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 8b7da44..50b9b86 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -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 positionMapper; - ComponentMapper modelMapper; - ComponentMapper bulletMapper; - ComponentMapper debrisMapper; - ComponentMapper asteroidMapper; - ComponentMapper 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(); diff --git a/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java b/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java index 2ac0680..0b59f81 100644 --- a/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java +++ b/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java @@ -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 asteroidMapper; - private ComponentMapper positionMapper; - private ComponentMapper velocityMapper; - private ComponentMapper 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++; } } diff --git a/core/src/com/me/asteroids/systems/CollisionSystem.java b/core/src/com/me/asteroids/systems/CollisionSystem.java index 4982802..70afbf4 100644 --- a/core/src/com/me/asteroids/systems/CollisionSystem.java +++ b/core/src/com/me/asteroids/systems/CollisionSystem.java @@ -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 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)); } diff --git a/core/src/com/me/asteroids/systems/DecaySystem.java b/core/src/com/me/asteroids/systems/DecaySystem.java index 295e921..440dee7 100644 --- a/core/src/com/me/asteroids/systems/DecaySystem.java +++ b/core/src/com/me/asteroids/systems/DecaySystem.java @@ -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 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(); } - } } diff --git a/core/src/com/me/asteroids/systems/GameDataRenderSystem.java b/core/src/com/me/asteroids/systems/GameDataRenderSystem.java index f2cef73..668c784 100644 --- a/core/src/com/me/asteroids/systems/GameDataRenderSystem.java +++ b/core/src/com/me/asteroids/systems/GameDataRenderSystem.java @@ -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 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) { diff --git a/core/src/com/me/asteroids/systems/GameDataSystem.java b/core/src/com/me/asteroids/systems/GameDataSystem.java index 7b793cb..f78696f 100644 --- a/core/src/com/me/asteroids/systems/GameDataSystem.java +++ b/core/src/com/me/asteroids/systems/GameDataSystem.java @@ -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 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(); } diff --git a/core/src/com/me/asteroids/systems/ModelRenderSystem.java b/core/src/com/me/asteroids/systems/ModelRenderSystem.java index e7b175f..2e91db7 100644 --- a/core/src/com/me/asteroids/systems/ModelRenderSystem.java +++ b/core/src/com/me/asteroids/systems/ModelRenderSystem.java @@ -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 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); diff --git a/core/src/com/me/asteroids/systems/MovementSystem.java b/core/src/com/me/asteroids/systems/MovementSystem.java index 6bd2374..092f5c9 100644 --- a/core/src/com/me/asteroids/systems/MovementSystem.java +++ b/core/src/com/me/asteroids/systems/MovementSystem.java @@ -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 positionMapper; - private ComponentMapper velocityMapper; - private ComponentMapper accelMapper; - private ComponentMapper 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); diff --git a/core/src/com/me/asteroids/systems/PlayerInputSystem.java b/core/src/com/me/asteroids/systems/PlayerInputSystem.java index 8a7f568..b3fceae 100644 --- a/core/src/com/me/asteroids/systems/PlayerInputSystem.java +++ b/core/src/com/me/asteroids/systems/PlayerInputSystem.java @@ -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 positionMapper; - private ComponentMapper velocityMapper; - private ComponentMapper accelMapper; - private ComponentMapper 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(); diff --git a/core/src/com/me/asteroids/systems/ScreenWrapSystem.java b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java index c733498..99f872a 100644 --- a/core/src/com/me/asteroids/systems/ScreenWrapSystem.java +++ b/core/src/com/me/asteroids/systems/ScreenWrapSystem.java @@ -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 positionMapper; - private ComponentMapper 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;