1
0
Fork 0
spacetac/src/core/Drone.ts

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
2017-02-06 21:46:55 +00:00
/**
* Drones are static objects that apply effects in a circular zone around themselves.
*/
export class Drone {
2017-05-10 22:52:16 +00:00
// Battle in which the drone is deployed
battle: Battle;
2017-02-08 18:54:02 +00:00
2017-05-10 22:52:16 +00:00
// Ship that launched the drone (informative, a drone is autonomous)
2017-02-06 21:46:55 +00:00
owner: Ship;
2017-05-10 22:52:16 +00:00
// Code of the drone
code: string;
2017-02-06 21:46:55 +00:00
// Location in arena
x: number;
y: number;
radius: number;
2017-05-10 22:52:16 +00:00
// Remaining lifetime in number of turns
duration: number;
2017-02-06 21:46:55 +00:00
// Effects to apply
effects: BaseEffect[] = [];
2017-05-10 22:52:16 +00:00
constructor(owner: Ship, code = "drone", base_duration = 1) {
this.battle = owner.getBattle() || new Battle();
2017-02-06 21:46:55 +00:00
this.owner = owner;
2017-02-08 18:54:02 +00:00
this.code = code;
2017-05-22 23:00:02 +00:00
this.duration = base_duration;
2017-02-06 21:46:55 +00:00
}
2017-05-10 17:48:28 +00:00
/**
* Get a textual description of this drone
*/
getDescription(): string {
let effects = this.effects.map(effect => "• " + effect.getDescription()).join("\n");
if (effects.length == 0) {
effects = "• do nothing";
}
2017-05-10 22:52:16 +00:00
return `For ${this.duration} activation${this.duration > 1 ? "s" : ""}:\n${effects}`;
2017-05-10 17:48:28 +00:00
}
/**
* Check if a location is in range
*/
isInRange(x: number, y: number): boolean {
return Target.newFromLocation(x, y).getDistanceTo(this) <= this.radius;
}
2017-02-06 21:46:55 +00:00
/**
2017-05-10 22:52:16 +00:00
* Get the list of affected ships.
2017-02-06 21:46:55 +00:00
*/
2017-05-10 22:52:16 +00:00
getAffectedShips(): Ship[] {
2017-05-22 23:00:02 +00:00
let ships = ifilter(this.battle.iships(), ship => ship.alive && ship.isInCircle(this.x, this.y, this.radius));
2017-05-10 22:52:16 +00:00
return imaterialize(ships);
2017-02-06 21:46:55 +00:00
}
/**
2017-05-10 22:52:16 +00:00
* Apply the effects on a list of ships
*
* This does not check if the ships are in range.
2017-02-06 21:46:55 +00:00
*/
2017-05-10 22:52:16 +00:00
apply(ships: Ship[], log = true) {
if (ships.length > 0) {
if (log) {
this.battle.log.add(new DroneAppliedEvent(this, ships));
2017-02-06 21:46:55 +00:00
}
2017-05-10 22:52:16 +00:00
ships.forEach(ship => {
this.effects.forEach(effect => effect.applyOnShip(ship, this));
2017-05-10 22:52:16 +00:00
});
2017-02-06 21:46:55 +00:00
}
}
/**
2017-05-10 22:52:16 +00:00
* Activate the drone
2017-02-06 21:46:55 +00:00
*/
2017-05-10 22:52:16 +00:00
activate(log = true) {
this.apply(this.getAffectedShips(), log);
2017-02-06 21:46:55 +00:00
2017-05-10 22:52:16 +00:00
this.duration--;
if (this.duration == 0) {
this.battle.removeDrone(this, log);
2017-02-06 21:46:55 +00:00
}
}
}
}