Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Health.js
Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | Health.prototype.GetHitpoints = function() | ||||
return this.hitpoints; | return this.hitpoints; | ||||
}; | }; | ||||
Health.prototype.GetMaxHitpoints = function() | Health.prototype.GetMaxHitpoints = function() | ||||
{ | { | ||||
return this.maxHitpoints; | return this.maxHitpoints; | ||||
}; | }; | ||||
// Dead units are not considered injured. | |||||
elexis: /* | |||||
StanUnsubmitted Done Inline Actions/** * @return {boolean} Whether the units are injured. Dead units are not considered injured. */ Stan: ```
/**
* @return {boolean} Whether the units are injured. Dead units are not considered… | |||||
Health.prototype.IsInjured = function() | |||||
{ | |||||
return this.GetHitpoints() > 0 && this.GetHitpoints() < this.GetMaxHitpoints(); | |||||
}; | |||||
Health.prototype.SetHitpoints = function(value) | Health.prototype.SetHitpoints = function(value) | ||||
{ | { | ||||
// If we're already dead, don't allow resurrection | // If we're already dead, don't allow resurrection | ||||
if (this.hitpoints == 0) | if (this.GetHitpoints() == 0) | ||||
return; | return; | ||||
// Before changing the value, activate Fogging if necessary to hide changes | // Before changing the value, activate Fogging if necessary to hide changes | ||||
let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | ||||
if (cmpFogging) | if (cmpFogging) | ||||
cmpFogging.Activate(); | cmpFogging.Activate(); | ||||
let old = this.hitpoints; | let old = this.GetHitpoints(); | ||||
this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value)); | this.hitpoints = Math.max(1, Math.min(this.GetMaxHitpoints(), value)); | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
if (cmpRangeManager) | if (cmpRangeManager) | ||||
cmpRangeManager.SetEntityFlag(this.entity, "injured", this.hitpoints < this.GetMaxHitpoints()); | cmpRangeManager.SetEntityFlag(this.entity, "injured", this.IsInjured()); | ||||
this.RegisterHealthChanged(old); | this.RegisterHealthChanged(old); | ||||
}; | }; | ||||
Health.prototype.IsRepairable = function() | Health.prototype.IsRepairable = function() | ||||
{ | { | ||||
return Engine.QueryInterface(this.entity, IID_Repairable) != null; | return Engine.QueryInterface(this.entity, IID_Repairable) != null; | ||||
}; | }; | ||||
Health.prototype.IsUnhealable = function() | Health.prototype.IsUnhealable = function() | ||||
{ | { | ||||
return this.template.Unhealable == "true" || | return this.template.Unhealable == "true" || | ||||
this.GetHitpoints() <= 0 || | this.GetHitpoints() <= 0 || !this.IsInjured(); | ||||
this.GetHitpoints() >= this.GetMaxHitpoints(); | |||||
}; | }; | ||||
Health.prototype.GetIdleRegenRate = function() | Health.prototype.GetIdleRegenRate = function() | ||||
{ | { | ||||
return this.idleRegenRate; | return this.idleRegenRate; | ||||
}; | }; | ||||
Health.prototype.GetRegenRate = function() | Health.prototype.GetRegenRate = function() | ||||
Show All 19 Lines | |||||
/* | /* | ||||
* Check if the regeneration timer needs to be started or stopped | * Check if the regeneration timer needs to be started or stopped | ||||
*/ | */ | ||||
Health.prototype.CheckRegenTimer = function() | Health.prototype.CheckRegenTimer = function() | ||||
{ | { | ||||
// check if we need a timer | // check if we need a timer | ||||
if (this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0 || | if (this.GetRegenRate() == 0 && this.GetIdleRegenRate() == 0 || | ||||
this.GetHitpoints() == this.GetMaxHitpoints() && this.GetRegenRate() >= 0 && this.GetIdleRegenRate() >= 0 || | !this.IsInjured() && this.GetRegenRate() >= 0 && this.GetIdleRegenRate() >= 0 || | ||||
this.GetHitpoints() == 0) | this.GetHitpoints() == 0) | ||||
{ | { | ||||
// we don't need a timer, disable if one exists | // we don't need a timer, disable if one exists | ||||
if (this.regenTimer) | if (this.regenTimer) | ||||
{ | { | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
cmpTimer.CancelTimer(this.regenTimer); | cmpTimer.CancelTimer(this.regenTimer); | ||||
this.regenTimer = undefined; | this.regenTimer = undefined; | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
// we need a timer, enable if one doesn't exist | // we need a timer, enable if one doesn't exist | ||||
if (this.regenTimer) | if (this.regenTimer) | ||||
return; | return; | ||||
let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | let cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer); | ||||
this.regenTimer = cmpTimer.SetInterval(this.entity, IID_Health, "ExecuteRegeneration", 1000, 1000, null); | this.regenTimer = cmpTimer.SetInterval(this.entity, IID_Health, "ExecuteRegeneration", 1000, 1000, null); | ||||
}; | }; | ||||
Health.prototype.Kill = function() | Health.prototype.Kill = function() | ||||
{ | { | ||||
this.Reduce(this.hitpoints); | this.Reduce(this.GetHitpoints()); | ||||
}; | }; | ||||
/** | /** | ||||
* Reduces entity's health by amount HP. Kill if required. | * Reduces entity's health by amount HP. Kill if required. | ||||
* @return {{ "killed": boolean, "change": Number }} the status change of the entity. | * @return {{ "killed": boolean, "change": Number }} the status change of the entity. | ||||
*/ | */ | ||||
Health.prototype.Reduce = function(amount) | Health.prototype.Reduce = function(amount) | ||||
{ | { | ||||
// If we are dead, do not do anything | // If we are dead, do not do anything | ||||
// (The entity will exist a little while after calling DestroyEntity so this | // (The entity will exist a little while after calling DestroyEntity so this | ||||
// might get called multiple times) | // might get called multiple times) | ||||
// Likewise if the amount is 0. | // Likewise if the amount is 0. | ||||
if (!amount || !this.hitpoints) | if (!amount || !this.GetHitpoints()) | ||||
return {"killed": false, "change": 0}; | return {"killed": false, "change": 0}; | ||||
// Before changing the value, activate Fogging if necessary to hide changes | // Before changing the value, activate Fogging if necessary to hide changes | ||||
let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | ||||
if (cmpFogging) | if (cmpFogging) | ||||
cmpFogging.Activate(); | cmpFogging.Activate(); | ||||
let oldHitpoints = this.hitpoints; | let oldHitpoints = this.GetHitpoints(); | ||||
// If we reached 0, then die. | // If we reached 0, then die. | ||||
if (amount >= this.hitpoints) | if (amount >= this.GetHitpoints()) | ||||
{ | { | ||||
this.hitpoints = 0; | this.hitpoints = 0; | ||||
this.RegisterHealthChanged(oldHitpoints); | this.RegisterHealthChanged(oldHitpoints); | ||||
this.HandleDeath(); | this.HandleDeath(); | ||||
return {"killed": true, "change": -oldHitpoints}; | return {"killed": true, "change": -oldHitpoints}; | ||||
} | } | ||||
// If we are not marked as injured, do it now | // If we are not marked as injured, do it now | ||||
if (this.hitpoints == this.GetMaxHitpoints()) | if (!this.IsInjured()) | ||||
{ | { | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
if (cmpRangeManager) | if (cmpRangeManager) | ||||
cmpRangeManager.SetEntityFlag(this.entity, "injured", true); | cmpRangeManager.SetEntityFlag(this.entity, "injured", true); | ||||
} | } | ||||
this.hitpoints -= amount; | this.hitpoints -= amount; | ||||
this.RegisterHealthChanged(oldHitpoints); | this.RegisterHealthChanged(oldHitpoints); | ||||
return {"killed": false, "change": (this.hitpoints - oldHitpoints)}; | return {"killed": false, "change": (this.GetHitpoints() - oldHitpoints)}; | ||||
elexisUnsubmitted Done Inline Actionsspace after { elexis: space after {
unneeded parenthesis | |||||
}; | }; | ||||
/** | /** | ||||
* Handle what happens when the entity dies. | * Handle what happens when the entity dies. | ||||
*/ | */ | ||||
Health.prototype.HandleDeath = function() | Health.prototype.HandleDeath = function() | ||||
{ | { | ||||
let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); | let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); | ||||
Show All 31 Lines | |||||
Health.prototype.Increase = function(amount) | Health.prototype.Increase = function(amount) | ||||
{ | { | ||||
// Before changing the value, activate Fogging if necessary to hide changes | // Before changing the value, activate Fogging if necessary to hide changes | ||||
let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); | ||||
if (cmpFogging) | if (cmpFogging) | ||||
cmpFogging.Activate(); | cmpFogging.Activate(); | ||||
if (this.hitpoints == this.GetMaxHitpoints()) | if (!this.IsInjured()) | ||||
return { "old": this.hitpoints, "new": this.hitpoints }; | return { "old": this.GetHitpoints(), "new": this.GetHitpoints() }; | ||||
// If we're already dead, don't allow resurrection | // If we're already dead, don't allow resurrection | ||||
if (this.hitpoints == 0) | if (this.GetHitpoints() == 0) | ||||
return undefined; | return undefined; | ||||
let old = this.hitpoints; | let old = this.GetHitpoints(); | ||||
this.hitpoints = Math.min(this.hitpoints + amount, this.GetMaxHitpoints()); | this.hitpoints = Math.min(this.GetHitpoints() + amount, this.GetMaxHitpoints()); | ||||
if (this.hitpoints == this.GetMaxHitpoints()) | if (!this.IsInjured()) | ||||
{ | { | ||||
let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); | ||||
if (cmpRangeManager) | if (cmpRangeManager) | ||||
cmpRangeManager.SetEntityFlag(this.entity, "injured", false); | cmpRangeManager.SetEntityFlag(this.entity, "injured", false); | ||||
} | } | ||||
this.RegisterHealthChanged(old); | this.RegisterHealthChanged(old); | ||||
return { "old": old, "new": this.hitpoints }; | return { "old": old, "new": this.GetHitpoints() }; | ||||
}; | }; | ||||
Health.prototype.CreateCorpse = function(leaveResources) | Health.prototype.CreateCorpse = function(leaveResources) | ||||
{ | { | ||||
// If the unit died while not in the world, don't create any corpse for it | // If the unit died while not in the world, don't create any corpse for it | ||||
// since there's nowhere for the corpse to be placed | // since there's nowhere for the corpse to be placed | ||||
let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | let cmpPosition = Engine.QueryInterface(this.entity, IID_Position); | ||||
if (!cmpPosition.IsInWorld()) | if (!cmpPosition.IsInWorld()) | ||||
▲ Show 20 Lines • Show All 113 Lines • ▼ Show 20 Lines | Health.prototype.OnValueModification = function(msg) | ||||
if (this.regenRate != oldRegenRate || this.idleRegenRate != oldIdleRegenRate) | if (this.regenRate != oldRegenRate || this.idleRegenRate != oldIdleRegenRate) | ||||
this.CheckRegenTimer(); | this.CheckRegenTimer(); | ||||
}; | }; | ||||
Health.prototype.RegisterHealthChanged = function(from) | Health.prototype.RegisterHealthChanged = function(from) | ||||
{ | { | ||||
this.CheckRegenTimer(); | this.CheckRegenTimer(); | ||||
this.UpdateActor(); | this.UpdateActor(); | ||||
Engine.PostMessage(this.entity, MT_HealthChanged, { "from": from, "to": this.hitpoints }); | Engine.PostMessage(this.entity, MT_HealthChanged, { "from": from, "to": this.GetHitpoints() }); | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_Health, "Health", Health); | Engine.RegisterComponentType(IID_Health, "Health", Health); |
Wildfire Games · Phabricator
/*