Changeset View
Changeset View
Standalone View
Standalone View
binaries/data/mods/public/simulation/components/Armour.js
Show All 28 Lines | |||||
Armour.prototype.SetInvulnerability = function(invulnerability) | Armour.prototype.SetInvulnerability = function(invulnerability) | ||||
{ | { | ||||
this.invulnerable = invulnerability; | this.invulnerable = invulnerability; | ||||
Engine.PostMessage(this.entity, MT_InvulnerabilityChanged, { "entity": this.entity, "invulnerability": invulnerability }); | Engine.PostMessage(this.entity, MT_InvulnerabilityChanged, { "entity": this.entity, "invulnerability": invulnerability }); | ||||
}; | }; | ||||
/** | /** | ||||
* Take damage according to the entity's armor. | * Take damage according to the entity's armor. | ||||
* @param {Object} strengths - { "hack": number, "pierce": number, "crush": number } or something like that. | * @param {Object} attackStrengths - { "hack": number, "pierce": number, "crush": number } or something like that. | ||||
* @param {number} multiplier - the damage multiplier. | * @param {number} multiplier - the damage multiplier. | ||||
* Returns object of the form { "killed": false, "change": -12 }. | * Returns object of the form { "killed": false, "change": -12 }. | ||||
*/ | */ | ||||
Armour.prototype.TakeDamage = function(strengths, multiplier = 1) | Armour.prototype.TakeDamage = function(attackStrengths, multiplier = 1) | ||||
{ | { | ||||
if (this.invulnerable) | if (this.invulnerable) | ||||
return { "killed": false, "change": 0 }; | return { "killed": false, "change": 0 }; | ||||
// Adjust damage values based on armour; exponential armour: damage = attack * 0.9^armour | // Adjust damage values based on armour; exponential armour: damage = attack * 0.9^armour | ||||
var armourStrengths = this.GetArmourStrengths(); | let armourStrengths = this.GetArmourStrengths(); | ||||
// Total is sum of individual damages | // Total is sum of individual damages | ||||
// Don't bother rounding, since HP is no longer integral. | // Don't bother rounding, since HP is no longer integral. | ||||
var total = 0; | let total = 0; | ||||
for (let type in strengths) | for (let type in attackStrengths) | ||||
total += strengths[type] * multiplier * Math.pow(0.9, armourStrengths[type] || 0); | { | ||||
if (armourStrengths[type] == "Infinity") | |||||
continue; | |||||
total += attackStrengths[type] * multiplier * Math.pow(0.9, armourStrengths[type] || 0); | |||||
} | |||||
// Reduce health | // Reduce health | ||||
var cmpHealth = Engine.QueryInterface(this.entity, IID_Health); | let cmpHealth = Engine.QueryInterface(this.entity, IID_Health); | ||||
return cmpHealth.Reduce(total); | return cmpHealth.Reduce(total); | ||||
}; | }; | ||||
Armour.prototype.GetArmourStrengths = function() | Armour.prototype.GetArmourStrengths = function() | ||||
{ | { | ||||
// Work out the armour values with technology effects | // Work out the armour values with technology effects | ||||
var applyMods = (type, foundation) => { | let applyMods = (type, foundation) => { | ||||
var strength; | let strength; | ||||
if (foundation) | if (foundation) | ||||
{ | { | ||||
strength = +this.template.Foundation[type]; | strength = this.template.Foundation[type]; | ||||
type = "Foundation/" + type; | type = "Foundation/" + type; | ||||
} | } | ||||
else | else | ||||
strength = +this.template[type]; | strength = this.template[type]; | ||||
return ApplyValueModificationsToEntity("Armour/" + type, strength, this.entity); | return strength == "Infinity" ? "Infinity" : | ||||
ApplyValueModificationsToEntity("Armour/" + type, +strength, this.entity); | |||||
}; | }; | ||||
var foundation = Engine.QueryInterface(this.entity, IID_Foundation) && this.template.Foundation; | let foundation = Engine.QueryInterface(this.entity, IID_Foundation) && this.template.Foundation; | ||||
let ret = {}; | let ret = {}; | ||||
for (let damageType of DamageTypes.GetTypes()) | for (let damageType of DamageTypes.GetTypes()) | ||||
ret[damageType] = applyMods(damageType, foundation); | ret[damageType] = applyMods(damageType, foundation); | ||||
return ret; | return ret; | ||||
}; | }; | ||||
Engine.RegisterComponentType(IID_DamageReceiver, "Armour", Armour); | Engine.RegisterComponentType(IID_DamageReceiver, "Armour", Armour); |
Wildfire Games · Phabricator