File: gameplay\spell-casting\spell-effects\System.js
/**
System responsible for updating all active spell effects. This system only updates durations, etc. and removes effects that have expired, etc.
Any modifications done by spell effects are handled by the respective systems, so for example an AttackSpeedSpellEffect will be used by
the AttackSystem to calculate the final attack cooldown.
@class SpellEffectsSystem
@constructor
@param entitySystemManager {Manager} The entity system manager whose entities this system will be working on.
@param healthSystem {HealthSystem}
*/
function SpellEffectsSystem(entitySystemManager, healthSystem){
var entities = entitySystemManager.createAspect([SpellEffects]);
/**
@method update
@param deltaTime {Number} The time that passed since last update.
*/
this.update = function(deltaTime){
//Update a spell effect and return true if it expired and thus should be removed.
function updateSpellEffect(spellEffect){
spellEffect._durationRemaining -= deltaTime;
return spellEffect._durationRemaining <= 0;
}
entities.iterate(function(entity){
var spellEffects = entity.get(SpellEffects);
//Movement spell effects.
spellEffects._movementSpeedSpellEffects.removeIf(updateSpellEffect, true);
spellEffects._jumpPowerSpellEffects.removeIf(updateSpellEffect, true);
//Health spell effects.
spellEffects._resistanceSpellEffects.removeIf(updateSpellEffect, true);
spellEffects._shieldSpellEffects.removeIf(function(shieldSpellEffect){
shieldSpellEffect._durationRemaining -= deltaTime;
return shieldSpellEffect._durationRemaining <= 0 || shieldSpellEffect._absorbtionAmountRemaining <= 0;
}, true);
spellEffects._damageOverTimeSpellEffects.removeIf(function(damageOverTimeSpellEffect){
//Calculate how many ticks still remain.
var ticksRemainingBeforeUpdate = Math.ceil(damageOverTimeSpellEffect._durationRemaining / damageOverTimeSpellEffect._damageInterval),
ticksDifference;
//Update the remaining duration.
damageOverTimeSpellEffect._durationRemaining -= deltaTime;
//Calculate the difference in ticks after update and deal that many instances of damage.
ticksDifference = ticksRemainingBeforeUpdate - Math.ceil(damageOverTimeSpellEffect._durationRemaining / damageOverTimeSpellEffect._damageInterval);;
for(var i=0; i<ticksDifference; ++i){
healthSystem.damageEntity(entity, damageOverTimeSpellEffect._damageAmount, damageOverTimeSpellEffect._damageType);
}
//Remove the effect if the duration has expired.
return damageOverTimeSpellEffect._durationRemaining <= 0;
}, true);
spellEffects._healingOverTimeSpellEffects.removeIf(function(healingOverTimeSpellEffect){
//Calculate how many ticks still remain.
var ticksRemainingBeforeUpdate = Math.ceil(healingOverTimeSpellEffect._durationRemaining / healingOverTimeSpellEffect._healingInterval),
ticksDifference;
//Update the remaining duration.
healingOverTimeSpellEffect._durationRemaining -= deltaTime;
//Calculate the difference in ticks after update and heal that many times.
ticksDifference = ticksRemainingBeforeUpdate - Math.ceil(healingOverTimeSpellEffect._durationRemaining / healingOverTimeSpellEffect._healingInterval);
for(var i=0; i<ticksDifference; ++i){
healthSystem.healEntity(entity, healingOverTimeSpellEffect._healingAmount);
}
//Remove the effect if the duration has expired.
return healingOverTimeSpellEffect._durationRemaining <= 0;
}, true);
//Attack spell effects.
spellEffects._attackSpeedSpellEffects.removeIf(updateSpellEffect, true);
if(spellEffects._attackEnchantSpellEffect){
spellEffects._attackEnchantSpellEffect._durationRemaining -= deltaTime;
if(spellEffects._attackEnchantSpellEffect._durationRemaining <= 0 || spellEffects._attackEnchantSpellEffect._numberOfChargesRemaining <= 0){
spellEffects._attackEnchantSpellEffect = null;
};
}
});
};
/**
@method destroy
*/
this.destroy = function(){
entities.destroy();
};
}