Differential D2337 Diff 10007 binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js
Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js
function StatusEffectsReceiver() {} | function StatusEffectsReceiver() {} | ||||
/** | |||||
* Initialises the status effects. | |||||
*/ | |||||
StatusEffectsReceiver.prototype.Init = function() | StatusEffectsReceiver.prototype.Init = function() | ||||
{ | { | ||||
this.activeStatusEffects = {}; | this.activeStatusEffects = {}; | ||||
}; | }; | ||||
/** | |||||
* Which status effects are active on this entity. | |||||
* | |||||
* @return {Object} - An object containing the status effects which currently affect the entity. | |||||
*/ | |||||
StatusEffectsReceiver.prototype.GetActiveStatuses = function() | StatusEffectsReceiver.prototype.GetActiveStatuses = function() | ||||
{ | { | ||||
return this.activeStatusEffects; | return this.activeStatusEffects; | ||||
}; | }; | ||||
// Called by attacking effects. | /** | ||||
* Called by Attacking effects. Adds status effects for each entry in the effectData. | |||||
* | |||||
* @param {Object} effectData - An object containing the status effects to give to the entity. | |||||
* @param {number} attacker - The entity ID of the attacker. | |||||
* @param {number} attackerOwner - The player ID of the attacker. | |||||
* @param {number} bonusMultiplier - A value to multiply the damage with (not implemented yet for SE). | |||||
* | |||||
* @return {Object} - The names of the status effects which were processed. | |||||
*/ | |||||
StatusEffectsReceiver.prototype.GiveStatus = function(effectData, attacker, attackerOwner, bonusMultiplier) | StatusEffectsReceiver.prototype.GiveStatus = function(effectData, attacker, attackerOwner, bonusMultiplier) | ||||
{ | { | ||||
for (let effect in effectData) | for (let effect in effectData) | ||||
this.AddStatus(effect, effectData[effect]); | this.AddStatus(effect, effectData[effect]); | ||||
// TODO: implement loot / resistance. | // TODO: implement loot / resistance. | ||||
return { "inflictedStatuses": Object.keys(effectData) }; | return { "inflictedStatuses": Object.keys(effectData) }; | ||||
}; | }; | ||||
/** | |||||
* Adds a status effect to the entity. | |||||
* | |||||
* @param {string} statusName - The name of the status effect. | |||||
* @param {object} data - The various effects and timings. | |||||
*/ | |||||
StatusEffectsReceiver.prototype.AddStatus = function(statusName, data) | StatusEffectsReceiver.prototype.AddStatus = function(statusName, data) | ||||
{ | { | ||||
if (this.activeStatusEffects[statusName]) | if (this.activeStatusEffects[statusName]) | ||||
{ | |||||
// TODO: implement different behaviour when receiving the same status multiple times. | |||||
// For now, these are ignored. | |||||
return; | return; | ||||
} | |||||
this.activeStatusEffects[statusName] = {}; | this.activeStatusEffects[statusName] = {}; | ||||
let status = this.activeStatusEffects[statusName]; | let status = this.activeStatusEffects[statusName]; | ||||
Object.assign(status, data); | Object.assign(status, data); | ||||
status.Interval = +data.Interval; | |||||
status.TimeElapsed = 0; | if (status.Modifiers) | ||||
status.FirstTime = true; | { | ||||
let modifications = DeriveModificationsFromXMLTemplate(status.Modifiers); | |||||
let cmpModifiersManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ModifiersManager); | |||||
cmpModifiersManager.AddModifiers(statusName, modifications, this.entity); | |||||
} | |||||
// With neither an interval nor a duration, there is no point in starting a timer. | |||||
if (!status.Duration && !status.Interval) | |||||
return; | |||||
// We want an interval to update the GUI to show how much time of the status effect | |||||
// is left even if the status effect itself has no interval. | |||||
if (!status.Interval) | |||||
status._interval = 1000; | |||||
status._timeElapsed = 0; | |||||
status._firstTime = true; | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
status.Timer = cmpTimer.SetInterval(this.entity, IID_StatusEffectsReceiver, "ExecuteEffect", 0, +status.Interval, statusName); | status._timer = cmpTimer.SetInterval(this.entity, IID_StatusEffectsReceiver, "ExecuteEffect", 0, +(status.Interval || status._interval), statusName); | ||||
}; | }; | ||||
/** | |||||
* Removes a status effect from the entity. | |||||
* | |||||
* @param {string} statusName - The status effect to be removed. | |||||
*/ | |||||
StatusEffectsReceiver.prototype.RemoveStatus = function(statusName) | StatusEffectsReceiver.prototype.RemoveStatus = function(statusName) | ||||
{ | { | ||||
if (!this.activeStatusEffects[statusName]) | let statusEffect = this.activeStatusEffects[statusName]; | ||||
if (!statusEffect) | |||||
return; | return; | ||||
if (statusEffect.Modifiers) | |||||
{ | |||||
let cmpModifiersManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_ModifiersManager); | |||||
cmpModifiersManager.RemoveAllModifiers(statusName, this.entity); | |||||
} | |||||
if (statusEffect._timer) | |||||
{ | |||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.CancelTimer(this.activeStatusEffects[statusName].Timer); | cmpTimer.CancelTimer(statusEffect._timer); | ||||
} | |||||
delete this.activeStatusEffects[statusName]; | delete this.activeStatusEffects[statusName]; | ||||
}; | }; | ||||
/** | |||||
* Called by the timers. Executes a status effect. | |||||
* | |||||
* @param {string} statusName - The name of the status effect to be executed. | |||||
* @param {number} lateness - The delay between the calling of the function and the actual execution (turn time?). | |||||
*/ | |||||
StatusEffectsReceiver.prototype.ExecuteEffect = function(statusName, lateness) | StatusEffectsReceiver.prototype.ExecuteEffect = function(statusName, lateness) | ||||
{ | { | ||||
let status = this.activeStatusEffects[statusName]; | let status = this.activeStatusEffects[statusName]; | ||||
if (!status) | if (!status) | ||||
return; | return; | ||||
if (status.FirstTime) | if (status.Damage || status.Capture) | ||||
Attacking.HandleAttackEffects(statusName, status, this.entity, INVALID_ENTITY, INVALID_PLAYER); | |||||
if (!status.Duration) | |||||
return; | |||||
if (status._firstTime) | |||||
{ | { | ||||
status.FirstTime = false; | status._firstTime = false; | ||||
status.TimeElapsed += lateness; | status._timeElapsed += lateness; | ||||
} | } | ||||
else | else | ||||
status.TimeElapsed += status.Interval + lateness; | status._timeElapsed += +(status.Interval || status._interval) + lateness; | ||||
Attacking.HandleAttackEffects(statusName, status, this.entity, -1, -1); | |||||
if (status.Duration && status.TimeElapsed >= +status.Duration) | if (status._timeElapsed >= +status.Duration) | ||||
this.RemoveStatus(statusName); | this.RemoveStatus(statusName); | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_StatusEffectsReceiver, "StatusEffectsReceiver", StatusEffectsReceiver); | Engine.RegisterComponentType(IID_StatusEffectsReceiver, "StatusEffectsReceiver", StatusEffectsReceiver); |
Wildfire Games · Phabricator