public abstract class DelayedEntityProcessingSystem extends EntitySystem
An example system would be an ExpirationSystem, that deletes entities after a certain lifetime. Instead of running a system that decrements a timeLeft value for each entity, you can simply use this system to execute in a future at a time of the shortest lived entity, and then reset the system to run at a time in a future at a time of the shortest lived entity, etc.
Another example system would be an AnimationSystem. You know when you have to animate a certain entity, e.g. in 300 milliseconds. So you can set the system to run in 300 ms to perform the animation.
This will save CPU cycles in some scenarios.
Implementation notes:
Within processExpired(Entity e)
you must call offerDelay(float delay)
if the
entity's delay time is renewed. That method is also called by inserted(int entityId)
for each newly matched entity.
subscription
world
Constructor and Description |
---|
DelayedEntityProcessingSystem() |
DelayedEntityProcessingSystem(Aspect.Builder aspect)
Creates a new DelayedEntityProcessingSystem.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
checkProcessing()
Does the system desire processing.
|
float |
getInitialTimeDelay()
Get the initial delay that the system was ordered to process entities
after.
|
protected abstract float |
getRemainingDelay(Entity e)
Return the delay until this entity should be processed.
|
float |
getRemainingTimeUntilProcessing()
Get the time until the system is scheduled to run at.
|
protected float |
getTimeDelta()
Overridable method to provide custom time delta.
|
void |
inserted(Entity entity)
Called if entity has come into scope for this system, e.g
created or a component was added to it.
|
boolean |
isRunning()
Check if the system is counting down towards processing.
|
void |
offerDelay(float offeredDelay)
Restarts the system only if the delay offered is shorter than the time
that the system is currently scheduled to execute at.
|
protected abstract void |
processDelta(Entity e,
float accumulatedDelta)
Process an entity this system is interested in.
|
protected abstract void |
processExpired(Entity e) |
protected void |
processSystem()
Process the system.
|
void |
stop()
Stops the system from running, aborts current countdown.
|
getEntities, inserted, inserted, removed, removed, removed, setWorld
getEntityIds, getSubscription
begin, dispose, end, getWorld, initialize, isEnabled, process, setEnabled
public DelayedEntityProcessingSystem(Aspect.Builder aspect)
aspect
- the aspect to match against entitiespublic DelayedEntityProcessingSystem()
protected final void processSystem()
BaseSystem
processSystem
in class BaseSystem
public void inserted(Entity entity)
EntitySystem
inserted
in class EntitySystem
entity
- the entity that was added to this systemprotected abstract float getRemainingDelay(Entity e)
e
- entityprotected final boolean checkProcessing()
BaseSystem
checkProcessing
in class BaseSystem
both must be true before the system will process.
protected float getTimeDelta()
protected abstract void processDelta(Entity e, float accumulatedDelta)
Substract the accumulatedDelta from the entities defined delay.
e
- the entity to processaccumulatedDelta
- the delta time since this system was last executedprotected abstract void processExpired(Entity e)
public void offerDelay(float offeredDelay)
If the system is already stopped (not running) then the offered delay will be used to restart the system with no matter its value.
If the system is already counting down, and the offered delay is larger than the time remaining, the system will ignore it. If the offered delay is shorter than the time remaining, the system will restart itself to run at the offered delay.
offeredDelay
- delay to offerpublic float getInitialTimeDelay()
public float getRemainingTimeUntilProcessing()
Returns zero (0) if the system is not running.
Use isRunning()
before checking this value.
public boolean isRunning()
true
if it's counting down, false if it's not runningpublic void stop()
Call offerDelay or restart to run it again.
Copyright © 2019. All rights reserved.