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

107 lines
3 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac {
2017-02-09 22:21:39 +00:00
/**
* A fleet of ships, all belonging to the same player
*/
export class Fleet {
2014-12-29 00:00:00 +00:00
// Fleet owner
player: Player;
// List of ships
ships: Ship[];
2015-03-24 00:00:00 +00:00
// Current fleet location
2017-03-14 22:28:07 +00:00
location: StarLocation | null = null;
previous_location: StarLocation | null = null;
2015-03-24 00:00:00 +00:00
2015-01-19 00:00:00 +00:00
// Current battle in which the fleet is engaged (null if not fighting)
2017-03-14 22:28:07 +00:00
battle: Battle | null = null;
2015-01-19 00:00:00 +00:00
2017-02-27 23:36:12 +00:00
// Amount of credits available
credits = 0;
2014-12-29 00:00:00 +00:00
// Create a fleet, bound to a player
2017-03-09 17:11:00 +00:00
constructor(player = new Player()) {
this.player = player;
this.ships = [];
}
2017-07-02 18:21:04 +00:00
jasmineToString(): string {
return `${this.player.name}'s fleet [${this.ships.map(ship => ship.name).join(",")}]`;
}
2017-02-09 22:21:39 +00:00
/**
* Set the current location of the fleet
*
* Returns true on success
*/
setLocation(location: StarLocation, force = false): boolean {
if (!force && this.location && location.star != this.location.star && (this.location.type != StarLocationType.WARP || this.location.jump_dest != location)) {
return false;
}
2017-03-14 22:28:07 +00:00
this.previous_location = this.location;
2015-03-25 00:00:00 +00:00
this.location = location;
2017-01-29 18:34:38 +00:00
this.player.setVisited(this.location);
// Check encounter
var battle = this.location.enterLocation(this.player.fleet);
if (battle) {
this.player.setBattle(battle);
}
2017-02-09 22:21:39 +00:00
return true;
2015-03-25 00:00:00 +00:00
}
2017-07-02 18:21:04 +00:00
/**
* Add a ship this fleet
*/
addShip(ship = new Ship()): Ship {
if (ship.fleet && ship.fleet != this) {
remove(ship.fleet.ships, ship);
}
add(this.ships, ship);
ship.fleet = this;
return ship;
2014-12-29 00:00:00 +00:00
}
2015-01-19 00:00:00 +00:00
2017-07-02 18:21:04 +00:00
/**
* Remove the ship from this fleet, transferring it to another fleet
*/
removeShip(ship: Ship, fleet = new Fleet()): void {
if (ship.fleet === this) {
fleet.addShip(ship);
}
}
2015-01-19 00:00:00 +00:00
// Set the current battle
2017-03-09 17:11:00 +00:00
setBattle(battle: Battle | null): void {
2015-01-19 00:00:00 +00:00
this.battle = battle;
}
2015-02-09 00:00:00 +00:00
// Get the average level of this fleet
getLevel(): number {
if (this.ships.length === 0) {
return 0;
}
var sum = 0;
this.ships.forEach((ship: Ship) => {
2017-03-17 00:07:00 +00:00
sum += ship.level.get();
});
var avg = sum / this.ships.length;
2017-03-17 00:07:00 +00:00
return Math.floor(avg);
}
2017-07-02 18:21:04 +00:00
/**
* Check if the fleet is considered alive (at least one ship alive, and no critical ship dead)
*/
2015-02-09 00:00:00 +00:00
isAlive(): boolean {
2017-07-02 18:21:04 +00:00
if (any(this.ships, ship => ship.critical && !ship.alive)) {
return false;
} else {
return any(this.ships, ship => ship.alive);
}
2015-02-09 00:00:00 +00:00
}
2014-12-29 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}