Skip to content

DelayedEntityProcessingSystem

Daan van Yperen edited this page Aug 16, 2014 · 8 revisions

This system keeps track of a cooldown per entity, processing the entity when the timer runs out. Examples would be a system for firing animations, or deleting entities after a cooldown.

This system is optimized to sleep in between process calls.

Your implementation is responsible for storing and updating the entity cooldowns. After the cooldown is reached processExpired method is called. See below for an example.

class ExampleSystem extends DelayedEntityProcessingSystem {

     ComponentManager<Animation> mAnim;

     /** Decrease entity timer by accumulatedDelta. Called incidentally when the system awakens. */ 
     @Override
     protected void processDelta(Entity e, float accumulatedDelta)
     {
         Animation anim = mAnim.get(e);
         anim.cooldown -= accumulatedDelta;
     }

     /** Returns entity timer. */ 
     @Override
     protected float getRemainingDelay(Entity e)
     {
         Animation anim = mAnim.get(e);
         return anim.cooldown;
     }

     /** Process entity when corresponding timer <= 0. */
     @Override
     protected void processExpired(Entity e)
     {
         Animation anim = mAnim.get(e);
         updateAnimation(anim);

         // Provide new cooldown when done. Can also opt to remove.
         anim.cooldown = 300;
         offerDelay(anim.cooldown);
     }
}
Clone this wiki locally