1
0
Fork 0
spacetac/src/core/effects/ValueTransferEffect.ts
2019-11-21 23:14:27 +01:00

55 lines
1.5 KiB
TypeScript

import { BaseBattleDiff } from "../diffs/BaseBattleDiff";
import { Drone } from "../Drone";
import { Ship } from "../Ship";
import { ShipValues, SHIP_VALUES_NAMES } from "../ShipValue";
import { BaseEffect } from "./BaseEffect";
/**
* Transfer a value between two ships.
*/
export class ValueTransferEffect extends BaseEffect {
// Affected value
valuetype: keyof ShipValues
// Value to give to target (negative to take it)
amount: number
constructor(valuetype: keyof ShipValues, amount = 0) {
super("valuetransfer");
this.valuetype = valuetype;
this.amount = amount;
}
getOnDiffs(ship: Ship, source: Ship | Drone): BaseBattleDiff[] {
if (source instanceof Ship) {
if (this.amount < 0) {
return new ValueTransferEffect(this.valuetype, -this.amount).getOnDiffs(source, ship);
} else {
let amount = Math.min(source.getValue(this.valuetype), this.amount);
if (amount) {
return source.getValueDiffs(this.valuetype, -amount, true).concat(ship.getValueDiffs(this.valuetype, amount, true));
} else {
return [];
}
}
} else {
return [];
}
}
isBeneficial(): boolean {
return this.amount >= 0;
}
getFullCode(): string {
return `${this.code}-${this.valuetype}`;
}
getDescription(): string {
let attrname = SHIP_VALUES_NAMES[this.valuetype];
let verb = (this.amount < 0 ? "steal" : "give");
return `${verb} ${Math.abs(this.amount)} ${attrname}`;
}
}