Add DecayComponent and DecaySystem

Add DecayComponent to debris entities so they disappear after while.

decayTimer set to a random value between 0.5 and 2.5 seconds.
This commit is contained in:
Matt Low 2020-01-30 15:34:49 +04:00
parent c969b87eca
commit de942c44aa
4 changed files with 56 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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));

View File

@ -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<DecayComponent> 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();
}
}
}
}