Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/simulation/components/Health.js
Show First 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
Health.prototype.Kill = function() | Health.prototype.Kill = function() | ||||
{ | { | ||||
this.Reduce(this.hitpoints); | this.Reduce(this.hitpoints); | ||||
}; | }; | ||||
/** | /** | ||||
* Reduces entity's health by amount HP. | * Reduces entity's health by amount HP. Kill if required. | ||||
* Returns object of the form { "killed": false, "change": -12 } | * @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 | |||||
// (The entity will exist a little while after calling DestroyEntity so this | |||||
// might get called multiple times) | |||||
// Likewise if the amount is 0. | |||||
if (!amount || !this.hitpoints) | |||||
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 state = { "killed": false }; | let oldHitpoints = this.hitpoints; | ||||
if (amount >= 0 && this.hitpoints == this.GetMaxHitpoints()) | // If we reached 0, then die. | ||||
if (amount >= this.hitpoints) | |||||
{ | |||||
this.hitpoints = 0; | |||||
this.RegisterHealthChanged(oldHitpoints); | |||||
this.HandleDeath(); | |||||
return {"killed": true, "change": -oldHitpoints}; | |||||
} | |||||
// If we are not marked as injured, do it now | |||||
if (this.hitpoints == this.GetMaxHitpoints()) | |||||
{ | { | ||||
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); | ||||
} | } | ||||
let oldHitpoints = this.hitpoints; | |||||
if (amount >= this.hitpoints) | this.hitpoints -= amount; | ||||
{ | |||||
// If this is the first time we reached 0, then die. | |||||
// (The entity will exist a little while after calling DestroyEntity so this | |||||
// might get called multiple times) | |||||
if (this.hitpoints) | |||||
{ | |||||
this.hitpoints = 0; | |||||
this.RegisterHealthChanged(oldHitpoints); | this.RegisterHealthChanged(oldHitpoints); | ||||
state.killed = true; | return {"killed": false, "change": (this.hitpoints - oldHitpoints)}; | ||||
}; | |||||
/** | |||||
* Handle what happens when the entity dies. | |||||
*/ | |||||
Health.prototype.HandleDeath = function() | |||||
{ | |||||
let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); | let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); | ||||
if (cmpDeathDamage) | if (cmpDeathDamage) | ||||
cmpDeathDamage.CauseDeathDamage(); | cmpDeathDamage.CauseDeathDamage(); | ||||
PlaySound("death", this.entity); | PlaySound("death", this.entity); | ||||
if (this.template.SpawnEntityOnDeath) | if (this.template.SpawnEntityOnDeath) | ||||
this.CreateDeathSpawnedEntity(); | this.CreateDeathSpawnedEntity(); | ||||
switch (this.template.DeathType) | switch (this.template.DeathType) | ||||
{ | { | ||||
case "corpse": | case "corpse": | ||||
this.CreateCorpse(); | this.CreateCorpse(); | ||||
break; | break; | ||||
case "remain": | case "remain": | ||||
{ | { | ||||
let resource = this.CreateCorpse(true); | let resource = this.CreateCorpse(true); | ||||
if (resource != INVALID_ENTITY) | if (resource != INVALID_ENTITY) | ||||
Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource }); | Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource }); | ||||
break; | break; | ||||
} | } | ||||
case "vanish": | case "vanish": | ||||
break; | break; | ||||
default: | default: | ||||
error("Invalid template.DeathType: " + this.template.DeathType); | error("Invalid template.DeathType: " + this.template.DeathType); | ||||
break; | break; | ||||
} | } | ||||
Engine.DestroyEntity(this.entity); | Engine.DestroyEntity(this.entity); | ||||
} | } | ||||
} | |||||
else | |||||
{ | |||||
this.hitpoints -= amount; | |||||
this.RegisterHealthChanged(oldHitpoints); | |||||
} | |||||
state.change = this.hitpoints - oldHitpoints; | |||||
return state; | |||||
}; | |||||
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.hitpoints == this.GetMaxHitpoints()) | ||||
▲ Show 20 Lines • Show All 152 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator