Open GregHib opened 4 years ago
ComponentMapper.set(entityId, boolean) is just a helper function for adding and removing an entity component, which does call inserted and removed.
Working POC
abstract class Resettable : Component() {
var cooldown = 0f
}
abstract class ResettableSystem<T: Resettable>(aspect: com.artemis.Aspect.Builder) : DelayedIteratingSystem(aspect) {
abstract var resettableMapper: ComponentMapper<T>
override fun processDelta(entityId: Int, accumulatedDelta: Float) {
val resettable = resettableMapper.get(entityId)
resettable.cooldown -= accumulatedDelta
}
override fun getRemainingDelay(entityId: Int): Float {
val resettable = resettableMapper.get(entityId)
return resettable.cooldown
}
override fun processExpired(entityId: Int) {
val resettable = resettableMapper.get(entityId)
if(resettable.cooldown > 0) {
offerDelay(resettable.cooldown)
} else {
resettableMapper.remove(entityId)
}
}
}
Usage
class TestComponent(ticks: Int) : Resettable() {
init {
cooldown = 0.6f * ticks
}
}
world.setDelta(0.6f)//600ms
Whether it's doors opening/closing, entities respawning, shooting star starting/stopping. Lots of features follow this delayed reactivate/respawn pattern so it makes sense to have a reusable respawning system.
If a component is toggled, does inserted/removed get called again?