Skip to content

DelayedIteratingSystem

Daan van Yperen edited this page Sep 29, 2015 · 1 revision

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

This system is optimized to sleep in between process calls.

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

@Wire
class ExampleSystem extends DelayedIteratingSystem {

     ComponentMapper<Animation> mAnim;

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

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

     /** Process entity when corresponding timer <= 0. */
     @Override
     protected void processExpired(int 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