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

92 lines
2.5 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-09 17:11:00 +00:00
location: StarLocation | 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-09 17:11:00 +00:00
battle: Battle | 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 = [];
2015-03-24 00:00:00 +00:00
this.location = null;
2015-01-19 00:00:00 +00:00
this.battle = null;
}
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;
}
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
}
// Add a ship in 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
// 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) => {
sum += ship.level;
});
var avg = sum / this.ships.length;
return Math.round(avg);
}
2015-02-09 00:00:00 +00:00
// Check if the fleet still has living ships
isAlive(): boolean {
var count = 0;
this.ships.forEach((ship: Ship) => {
if (ship.alive) {
count += 1;
}
});
return (count > 0);
}
2014-12-29 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}