From f4508fa0da294c7710a272cb87ea235d6371abee Mon Sep 17 00:00:00 2001 From: Matt Low Date: Sat, 1 Feb 2020 20:40:39 +0400 Subject: [PATCH] Utilize a PLAYER tag and ASTEROIDS group User PLAYER tag for access to the player entity Use ASTEROIDS group to count how many asteroids there are --- core/src/com/me/asteroids/EntityFactory.java | 3 +++ .../asteroids/systems/AsteroidSpawningSystem.java | 13 +------------ .../com/me/asteroids/systems/GameDataSystem.java | 2 +- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index 9853ef9..9be54d7 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -73,6 +73,7 @@ public class EntityFactory { player.addComponent(new AccelerationComponent(0f, 1f)); player.addComponent(COLLIDER); player.addComponent(new PlayerComponent(afterBurner)); + player.setTag("PLAYER"); afterBurner.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90)); afterBurner.addComponent(velocity); @@ -186,6 +187,7 @@ public class EntityFactory { asteroid.addComponent(model); asteroid.addComponent(COLLIDER); asteroid.addComponent(new AsteroidComponent()); + asteroid.addGroup("ASTEROIDS"); return asteroid; } @@ -230,6 +232,7 @@ public class EntityFactory { split.addComponent(velocity); split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1)); split.addComponent(COLLIDER); + split.addGroup("ASTEROIDS"); entities[i] = split; } diff --git a/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java b/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java index 0b59f81..e5f378d 100644 --- a/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java +++ b/core/src/com/me/asteroids/systems/AsteroidSpawningSystem.java @@ -2,7 +2,6 @@ package com.me.asteroids.systems; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.utils.Array; import com.me.asteroids.Constants; import com.me.asteroids.EntityFactory; import com.me.asteroids.Utils; @@ -12,7 +11,6 @@ import com.me.common.ecs.BaseSystem; 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; @@ -63,16 +61,7 @@ public class AsteroidSpawningSystem extends BaseSystem { @Override public void process(float dt) { - Array entities = engine.getEntities(); - int asteroidCount = 0; - for (Entity entity : entities) { - // 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 (ASTEROID.has(entity)) { - asteroidCount++; - } - } + int asteroidCount = engine.getEntitiesInGroup("ASTEROIDS").size; if (asteroidCount++ < Constants.ASTEROID_SPAWN_COUNT && (asteroidSpawnDelay -= dt) <= 0) { spawnAsteroid(); diff --git a/core/src/com/me/asteroids/systems/GameDataSystem.java b/core/src/com/me/asteroids/systems/GameDataSystem.java index 652ebcc..b21ecad 100644 --- a/core/src/com/me/asteroids/systems/GameDataSystem.java +++ b/core/src/com/me/asteroids/systems/GameDataSystem.java @@ -85,7 +85,7 @@ public class GameDataSystem extends EntitySystem implements Listener { } private void resetPlayer() { - Entity player = engine.getEntities().get(1); + Entity player = engine.getEntityByTag("PLAYER"); PositionComponent position = POSITION.get(player); position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT); position.rotation = 90;