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

86 lines
2.2 KiB
TypeScript

import { BaseBattleDiff } from "../diffs/BaseBattleDiff";
import { ShipEffectAddedDiff, ShipEffectRemovedDiff } from "../diffs/ShipEffectAddedDiff";
import { ShipEffectChangedDiff } from "../diffs/ShipEffectChangedDiff";
import { Drone } from "../Drone";
import { Ship } from "../Ship";
import { BaseEffect } from "./BaseEffect";
/**
* Wrapper around another effect, to make it stick to a ship for a given number of turns.
*
* The "effect" is to stick the wrapped effect to the ship.
*/
export class StickyEffect extends BaseEffect {
// Wrapped effect
base: BaseEffect
// Duration, in number of turns
duration: number
// Base constructor
constructor(base: BaseEffect, duration = 0) {
super(base.code);
this.base = base;
this.duration = duration;
}
getOnDiffs(ship: Ship, source: Ship | Drone): BaseBattleDiff[] {
let result: BaseBattleDiff[] = [];
let previous = ship.active_effects.get(this.id);
if (previous) {
result = result.concat(previous.getOffDiffs(ship));
}
result.push(new ShipEffectAddedDiff(ship, this));
result = result.concat(this.base.getOnDiffs(ship, source));
return result;
}
getOffDiffs(ship: Ship): BaseBattleDiff[] {
let result: BaseBattleDiff[] = [];
if (ship.active_effects.get(this.id)) {
result.push(new ShipEffectRemovedDiff(ship, this));
result = result.concat(this.base.getOffDiffs(ship));
}
return result;
}
getTurnStartDiffs(ship: Ship): BaseBattleDiff[] {
if (ship.active_effects.get(this.id)) {
return this.base.getTurnStartDiffs(ship);
} else {
return [];
}
}
getTurnEndDiffs(ship: Ship): BaseBattleDiff[] {
if (ship.active_effects.get(this.id)) {
if (this.duration > 1) {
let result: BaseBattleDiff[] = [new ShipEffectChangedDiff(ship, this, -1)];
return result.concat(this.base.getTurnEndDiffs(ship));
} else {
return this.getOffDiffs(ship);
}
} else {
return [];
}
}
isBeneficial(): boolean {
return this.base.isBeneficial();
}
getFullCode(): string {
return this.base.getFullCode();
}
getDescription(): string {
return this.base.getDescription() + ` for ${this.duration} turn${this.duration > 1 ? "s" : ""}`;
}
}