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
This commit is contained in:
Matt Low 2020-02-01 20:40:39 +04:00
parent d8cabf4549
commit f4508fa0da
3 changed files with 5 additions and 13 deletions

View File

@ -73,6 +73,7 @@ public class EntityFactory {
player.addComponent(new AccelerationComponent(0f, 1f)); player.addComponent(new AccelerationComponent(0f, 1f));
player.addComponent(COLLIDER); player.addComponent(COLLIDER);
player.addComponent(new PlayerComponent(afterBurner)); player.addComponent(new PlayerComponent(afterBurner));
player.setTag("PLAYER");
afterBurner.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90)); afterBurner.addComponent(new PositionComponent(Constants.HALF_WIDTH, Constants.HALF_HEIGHT, 90));
afterBurner.addComponent(velocity); afterBurner.addComponent(velocity);
@ -186,6 +187,7 @@ public class EntityFactory {
asteroid.addComponent(model); asteroid.addComponent(model);
asteroid.addComponent(COLLIDER); asteroid.addComponent(COLLIDER);
asteroid.addComponent(new AsteroidComponent()); asteroid.addComponent(new AsteroidComponent());
asteroid.addGroup("ASTEROIDS");
return asteroid; return asteroid;
} }
@ -230,6 +232,7 @@ public class EntityFactory {
split.addComponent(velocity); split.addComponent(velocity);
split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1)); split.addComponent(new AsteroidComponent(ASTEROID.get(asteroid).generation + 1));
split.addComponent(COLLIDER); split.addComponent(COLLIDER);
split.addGroup("ASTEROIDS");
entities[i] = split; entities[i] = split;
} }

View File

@ -2,7 +2,6 @@ package com.me.asteroids.systems;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
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;
@ -12,7 +11,6 @@ import com.me.common.ecs.BaseSystem;
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.MODEL;
import static com.me.asteroids.Components.POSITION; import static com.me.asteroids.Components.POSITION;
import static com.me.asteroids.Components.VELOCITY; import static com.me.asteroids.Components.VELOCITY;
@ -63,16 +61,7 @@ public class AsteroidSpawningSystem extends BaseSystem {
@Override @Override
public void process(float dt) { public void process(float dt) {
Array<Entity> entities = engine.getEntities(); int asteroidCount = engine.getEntitiesInGroup("ASTEROIDS").size;
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++;
}
}
if (asteroidCount++ < Constants.ASTEROID_SPAWN_COUNT && (asteroidSpawnDelay -= dt) <= 0) { if (asteroidCount++ < Constants.ASTEROID_SPAWN_COUNT && (asteroidSpawnDelay -= dt) <= 0) {
spawnAsteroid(); spawnAsteroid();

View File

@ -85,7 +85,7 @@ public class GameDataSystem extends EntitySystem implements Listener {
} }
private void resetPlayer() { private void resetPlayer() {
Entity player = engine.getEntities().get(1); Entity player = engine.getEntityByTag("PLAYER");
PositionComponent position = POSITION.get(player); PositionComponent position = POSITION.get(player);
position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT); position.set(Constants.HALF_WIDTH, Constants.HALF_HEIGHT);
position.rotation = 90; position.rotation = 90;