Index: binaries/data/mods/public/simulation/components/Health.js =================================================================== --- binaries/data/mods/public/simulation/components/Health.js +++ binaries/data/mods/public/simulation/components/Health.js @@ -176,73 +176,85 @@ */ Health.prototype.Reduce = function(amount) { + if (!amount) + return {"killed": false, "change": 0}; // Before changing the value, activate Fogging if necessary to hide changes let cmpFogging = Engine.QueryInterface(this.entity, IID_Fogging); if (cmpFogging) cmpFogging.Activate(); let state = { "killed": false }; - if (amount >= 0 && this.hitpoints == this.GetMaxHitpoints()) + let oldHitpoints = this.hitpoints; + + if (amount >= this.hitpoints) + { + state.killed = this.DeathCheck(); + state.change = this.hitpoints - oldHitpoints; + return state; + } + + if (this.hitpoints == this.GetMaxHitpoints()) { let cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager); if (cmpRangeManager) cmpRangeManager.SetEntityFlag(this.entity, "injured", true); } + + this.hitpoints -= amount; + this.RegisterHealthChanged(oldHitpoints); + state.change = this.hitpoints - oldHitpoints; + return state; +}; + +/** + * Checks if unit is alive and kill it + * Returns true if unit has been killed and false if allready dead + */ +Health.prototype.DeathCheck = function() +{ + // 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) + return false; + let oldHitpoints = this.hitpoints; - if (amount >= this.hitpoints) - { - // 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); - state.killed = true; - - let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); - if (cmpDeathDamage) - cmpDeathDamage.CauseDeathDamage(); - - PlaySound("death", this.entity); - - if (this.template.SpawnEntityOnDeath) - this.CreateDeathSpawnedEntity(); - - switch (this.template.DeathType) - { - case "corpse": - this.CreateCorpse(); - break; - - case "remain": - { - let resource = this.CreateCorpse(true); - if (resource != INVALID_ENTITY) - Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource }); - break; - } - - case "vanish": - break; - - default: - error("Invalid template.DeathType: " + this.template.DeathType); - break; - } + this.hitpoints = 0; + this.RegisterHealthChanged(oldHitpoints); + + let cmpDeathDamage = Engine.QueryInterface(this.entity, IID_DeathDamage); + if (cmpDeathDamage) + cmpDeathDamage.CauseDeathDamage(); + PlaySound("death", this.entity); - Engine.DestroyEntity(this.entity); - } + if (this.template.SpawnEntityOnDeath) + this.CreateDeathSpawnedEntity(); - } - else + switch (this.template.DeathType) + { + case "corpse": + this.CreateCorpse(); + break; + + case "remain": { - this.hitpoints -= amount; - this.RegisterHealthChanged(oldHitpoints); + let resource = this.CreateCorpse(true); + if (resource != INVALID_ENTITY) + Engine.PostMessage(this.entity, MT_EntityRenamed, { "entity": this.entity, "newentity": resource }); + break; } - state.change = this.hitpoints - oldHitpoints; - return state; -}; + + case "vanish": + break; + + default: + error("Invalid template.DeathType: " + this.template.DeathType); + break; + } + + Engine.DestroyEntity(this.entity); + return true; +} Health.prototype.Increase = function(amount) {