0ad/binaries/data/mods/public/simulation/components/StatusEffectsReceiver.js
wraitii 2333b1814e GUI support for Status Effects and extend their functionality to all attack effects following 16b452cf91
This fixes status effects following 16b452cf91 where the GiveStatus name
change broke them.

This lets status effects deal all types of damage, capture, or inflict
other status effects, like any attack.
This further adds some basic GUI spport to status effects, by showing up
to 5 icons in the top-right of the portrait, and including status
effects in the tooltips.

Status effects can specify a custom icon.

Differential Revision: https://code.wildfiregames.com/D2218
This was SVN commit r22901.
2019-09-15 09:24:52 +00:00

70 lines
1.9 KiB
JavaScript
Executable file

function StatusEffectsReceiver() {}
StatusEffectsReceiver.prototype.Init = function()
{
this.activeStatusEffects = {};
};
StatusEffectsReceiver.prototype.GetActiveStatuses = function()
{
return this.activeStatusEffects;
};
// Called by attacking effects.
StatusEffectsReceiver.prototype.GiveStatus = function(effectData, attacker, attackerOwner, bonusMultiplier)
{
for (let effect in effectData)
this.AddStatus(effect, effectData[effect]);
// TODO: implement loot / resistance.
return { "inflictedStatuses": Object.keys(effectData) };
};
StatusEffectsReceiver.prototype.AddStatus = function(statusName, data)
{
if (this.activeStatusEffects[statusName])
return;
this.activeStatusEffects[statusName] = {};
let status = this.activeStatusEffects[statusName];
Object.assign(status, data);
status.Interval = +data.Interval;
status.TimeElapsed = 0;
status.FirstTime = true;
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
status.Timer = cmpTimer.SetInterval(this.entity, IID_StatusEffectsReceiver, "ExecuteEffect", 0, +status.Interval, statusName);
};
StatusEffectsReceiver.prototype.RemoveStatus = function(statusName)
{
if (!this.activeStatusEffects[statusName])
return;
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
cmpTimer.CancelTimer(this.activeStatusEffects[statusName].Timer);
delete this.activeStatusEffects[statusName];
};
StatusEffectsReceiver.prototype.ExecuteEffect = function(statusName, lateness)
{
let status = this.activeStatusEffects[statusName];
if (!status)
return;
if (status.FirstTime)
{
status.FirstTime = false;
status.TimeElapsed += lateness;
}
else
status.TimeElapsed += status.Interval + lateness;
Attacking.HandleAttackEffects(statusName, status, this.entity, -1, -1);
if (status.Duration && status.TimeElapsed >= +status.Duration)
this.RemoveStatus(statusName);
};
Engine.RegisterComponentType(IID_StatusEffectsReceiver, "StatusEffectsReceiver", StatusEffectsReceiver);