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:
parent
c969b87eca
commit
de942c44aa
@ -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;
|
||||
}
|
||||
|
19
core/src/com/me/asteroids/components/DecayComponent.java
Normal file
19
core/src/com/me/asteroids/components/DecayComponent.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
|
31
core/src/com/me/asteroids/systems/DecaySystem.java
Normal file
31
core/src/com/me/asteroids/systems/DecaySystem.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user