Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/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; | ||||
}; | }; | ||||
/** | |||||
* @return {boolean} Whether the units are injured. Dead units are not considered injured. | |||||
*/ | |||||
Health.prototype.IsInjured = function() | |||||
{ | |||||
return this.hitpoints > 0 && this.hitpoints < 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.hitpoints == 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.hitpoints; | ||||
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.hitpoints <= 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.hitpoints == 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; | ||||
} | } | ||||
Show All 37 Lines | Health.prototype.Reduce = function(amount) | ||||
{ | { | ||||
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); | ||||
Show All 40 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.hitpoints, "new": this.hitpoints }; | ||||
// If we're already dead, don't allow resurrection | // If we're already dead, don't allow resurrection | ||||
if (this.hitpoints == 0) | if (this.hitpoints == 0) | ||||
return undefined; | return undefined; | ||||
let old = this.hitpoints; | let old = this.hitpoints; | ||||
this.hitpoints = Math.min(this.hitpoints + amount, this.GetMaxHitpoints()); | this.hitpoints = Math.min(this.hitpoints + 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); | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | Health.prototype.CreateDeathSpawnedEntity = function() | ||||
return spawnedEntity; | return spawnedEntity; | ||||
}; | }; | ||||
Health.prototype.UpdateActor = function() | Health.prototype.UpdateActor = function() | ||||
{ | { | ||||
if (!this.template.DamageVariants) | if (!this.template.DamageVariants) | ||||
return; | return; | ||||
let ratio = this.GetHitpoints() / this.GetMaxHitpoints(); | let ratio = this.hitpoints / this.GetMaxHitpoints(); | ||||
let newDamageVariant = "alive"; | let newDamageVariant = "alive"; | ||||
if (ratio > 0) | if (ratio > 0) | ||||
{ | { | ||||
let minTreshold = 1; | let minTreshold = 1; | ||||
for (let key in this.template.DamageVariants) | for (let key in this.template.DamageVariants) | ||||
{ | { | ||||
let treshold = +this.template.DamageVariants[key]; | let treshold = +this.template.DamageVariants[key]; | ||||
if (treshold < ratio || treshold > minTreshold) | if (treshold < ratio || treshold > minTreshold) | ||||
Show All 19 Lines | |||||
{ | { | ||||
if (msg.component != "Health") | if (msg.component != "Health") | ||||
return; | return; | ||||
let oldMaxHitpoints = this.GetMaxHitpoints(); | let oldMaxHitpoints = this.GetMaxHitpoints(); | ||||
let newMaxHitpoints = ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity); | let newMaxHitpoints = ApplyValueModificationsToEntity("Health/Max", +this.template.Max, this.entity); | ||||
if (oldMaxHitpoints != newMaxHitpoints) | if (oldMaxHitpoints != newMaxHitpoints) | ||||
{ | { | ||||
let newHitpoints = this.GetHitpoints() * newMaxHitpoints/oldMaxHitpoints; | let newHitpoints = this.hitpoints * newMaxHitpoints/oldMaxHitpoints; | ||||
this.maxHitpoints = newMaxHitpoints; | this.maxHitpoints = newMaxHitpoints; | ||||
this.SetHitpoints(newHitpoints); | this.SetHitpoints(newHitpoints); | ||||
} | } | ||||
let oldRegenRate = this.regenRate; | let oldRegenRate = this.regenRate; | ||||
this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity); | this.regenRate = ApplyValueModificationsToEntity("Health/RegenRate", +this.template.RegenRate, this.entity); | ||||
let oldIdleRegenRate = this.idleRegenRate; | let oldIdleRegenRate = this.idleRegenRate; | ||||
Show All 14 Lines |
Wildfire Games · Phabricator