1
0
Fork 0
spacetac/src/core/effects/DamageEffect.ts

105 lines
3.1 KiB
TypeScript
Raw Normal View History

/// <reference path="BaseEffect.ts"/>
2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2018-03-21 23:52:13 +00:00
/**
* Mode for damage effect
*/
export enum DamageEffectMode {
// Apply on shield only
SHIELD_ONLY,
// Apply on shield, then remaining value on hull
SHIELD_THEN_HULL,
// Apply on shield only if up, otherwise on hull
SHIELD_OR_HULL,
// Apply on hull only
HULL_ONLY
}
/**
* Apply damage on a ship.
*/
export class DamageEffect extends BaseEffect {
2018-03-21 23:52:13 +00:00
// Damage amount
value: number
2018-03-21 23:52:13 +00:00
// Damage mode
mode: DamageEffectMode
2018-03-26 15:30:43 +00:00
// Evadable damage (applies evasion)
evadable: boolean
constructor(value: number, mode = DamageEffectMode.SHIELD_OR_HULL, evadable = true) {
super("damage");
this.value = value;
2018-03-21 23:52:13 +00:00
this.mode = mode;
2018-03-26 15:30:43 +00:00
this.evadable = true;
}
/**
* Get the effective damage done to both shield and hull (in this order)
*/
getEffectiveDamage(ship: Ship): ShipDamageDiff {
2018-03-21 23:52:13 +00:00
let shield = ship.getValue("shield");
let hull = ship.getValue("hull");
let dhull = 0;
let dshield = 0;
2018-03-26 15:30:43 +00:00
// Apply evasion
let evaded = this.evadable ? Math.min(this.value, ship.getAttribute("evasion")) : 0;
let damage = this.value - evaded;
2018-03-21 23:52:13 +00:00
// Split in shield/hull damage
if (this.mode == DamageEffectMode.HULL_ONLY) {
dhull = Math.min(damage, hull);
} else if (this.mode == DamageEffectMode.SHIELD_ONLY) {
dshield = Math.min(damage, shield);
} else if (this.mode == DamageEffectMode.SHIELD_OR_HULL) {
if (shield) {
dshield = Math.min(damage, shield);
} else {
dhull = Math.min(damage, hull);
}
} else {
dshield = Math.min(damage, shield);
dhull = Math.min(damage - dshield, hull);
}
2018-03-26 15:30:43 +00:00
return new ShipDamageDiff(ship, dhull, dshield, evaded, this.value);
}
getOnDiffs(ship: Ship, source: Ship | Drone): BaseBattleDiff[] {
let result: BaseBattleDiff[] = [];
2015-02-03 00:00:00 +00:00
let damage = this.getEffectiveDamage(ship);
if (damage.shield || damage.hull) {
result.push(damage);
}
if (damage.shield) {
result.push(new ShipValueDiff(ship, "shield", -damage.shield));
}
if (damage.hull) {
result.push(new ShipValueDiff(ship, "hull", -damage.hull));
}
return result;
2015-01-28 00:00:00 +00:00
}
getDescription(): string {
2018-03-21 23:52:13 +00:00
let mode = "";
if (this.mode == DamageEffectMode.HULL_ONLY) {
mode = " hull";
} else if (this.mode == DamageEffectMode.SHIELD_ONLY) {
mode = " shield";
} else if (this.mode == DamageEffectMode.SHIELD_THEN_HULL) {
mode = " piercing";
}
return `do ${this.value}${mode} damage`;
}
}
}