diff --git a/core/src/com/me/asteroids/EntityFactory.java b/core/src/com/me/asteroids/EntityFactory.java index c93be42..b81441d 100644 --- a/core/src/com/me/asteroids/EntityFactory.java +++ b/core/src/com/me/asteroids/EntityFactory.java @@ -8,6 +8,7 @@ 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.ModelComponent; import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PositionComponent; @@ -129,6 +130,7 @@ public class EntityFactory { debris.addComponent(position); debris.addComponent(velocity); debris.addComponent(model); + debris.addComponent(new DecayComponent(rand.nextFloat(0.5f, 2.5f))); debris.addComponent(new DebrisComponent()); entities[i] = debris; } diff --git a/core/src/com/me/asteroids/components/DecayComponent.java b/core/src/com/me/asteroids/components/DecayComponent.java new file mode 100644 index 0000000..cef122a --- /dev/null +++ b/core/src/com/me/asteroids/components/DecayComponent.java @@ -0,0 +1,19 @@ +package com.me.asteroids.components; + +import com.me.common.ecs.Component; + +public class DecayComponent implements Component { + + public float decayTimer; + public boolean remove; + + public DecayComponent(float decayTimer) { + this(decayTimer, true); + } + + public DecayComponent(float decayTimer, boolean remove) { + this.decayTimer = decayTimer; + this.remove = remove; + } + +} diff --git a/core/src/com/me/asteroids/screens/GameScreen.java b/core/src/com/me/asteroids/screens/GameScreen.java index 6d798a8..5836124 100644 --- a/core/src/com/me/asteroids/screens/GameScreen.java +++ b/core/src/com/me/asteroids/screens/GameScreen.java @@ -13,6 +13,7 @@ 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.ModelComponent; import com.me.asteroids.components.PlayerComponent; import com.me.asteroids.components.PositionComponent; @@ -23,6 +24,7 @@ import com.me.asteroids.events.PlayerASteroidCollisionEvent; import com.me.asteroids.events.ScreenWrapEvent; import com.me.asteroids.systems.AsteroidSpawningSystem; import com.me.asteroids.systems.CollisionSystem; +import com.me.asteroids.systems.DecaySystem; import com.me.asteroids.systems.ModelRenderSystem; import com.me.asteroids.systems.MovementSystem; import com.me.asteroids.systems.PlayerInputSystem; @@ -59,6 +61,7 @@ public class GameScreen extends Screen implements Listener { engine.registerComponentClass(BulletComponent.class); engine.registerComponentClass(AsteroidComponent.class); engine.registerComponentClass(DebrisComponent.class); + engine.registerComponentClass(DecayComponent.class); engine.registerComponentClass(ColliderComponent.class); engine.registerComponentClass(PositionComponent.class); engine.registerComponentClass(VelocityComponent.class); @@ -67,6 +70,7 @@ public class GameScreen extends Screen implements Listener { engine.registerSystem(new PlayerInputSystem(engine)); engine.registerSystem(new AsteroidSpawningSystem(engine)); + engine.registerSystem(new DecaySystem(engine)); engine.registerSystem(new MovementSystem(engine)); engine.registerSystem(new CollisionSystem(engine)); engine.registerSystem(new ScreenWrapSystem(engine)); diff --git a/core/src/com/me/asteroids/systems/DecaySystem.java b/core/src/com/me/asteroids/systems/DecaySystem.java new file mode 100644 index 0000000..295e921 --- /dev/null +++ b/core/src/com/me/asteroids/systems/DecaySystem.java @@ -0,0 +1,31 @@ +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 { + + private ComponentMapper decayTimerMapper; + + 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); + if ((decayComponent.decayTimer -= dt) <= 0) { + if (decayComponent.remove) { + entity.remove(); + } else { + entity.deactivate(); + } + + } + } + +}