2015-03-03 00:00:00 +00:00
|
|
|
/// <reference path="Serializable.ts"/>
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
module SpaceTac.Game {
|
2015-01-07 00:00:00 +00:00
|
|
|
"use strict";
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// A fleet of ships
|
2015-03-03 00:00:00 +00:00
|
|
|
export class Fleet extends Serializable {
|
2014-12-29 00:00:00 +00:00
|
|
|
// Fleet owner
|
|
|
|
player: Player;
|
|
|
|
|
|
|
|
// List of ships
|
|
|
|
ships: Ship[];
|
|
|
|
|
2015-01-19 00:00:00 +00:00
|
|
|
// Current battle in which the fleet is engaged (null if not fighting)
|
|
|
|
battle: Battle;
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Create a fleet, bound to a player
|
2015-01-29 00:00:00 +00:00
|
|
|
constructor(player: Player = null) {
|
2015-03-03 00:00:00 +00:00
|
|
|
super();
|
|
|
|
|
2015-01-29 00:00:00 +00:00
|
|
|
this.player = player || new Player();
|
2014-12-30 00:00:00 +00:00
|
|
|
this.ships = [];
|
2015-01-19 00:00:00 +00:00
|
|
|
this.battle = null;
|
2014-12-30 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a ship in this fleet
|
|
|
|
addShip(ship: Ship): void {
|
2015-02-13 00:00:00 +00:00
|
|
|
if (this.ships.indexOf(ship) < 0) {
|
|
|
|
this.ships.push(ship);
|
|
|
|
}
|
2014-12-30 00:00:00 +00:00
|
|
|
ship.fleet = this;
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-19 00:00:00 +00:00
|
|
|
|
|
|
|
// Set the current battle
|
|
|
|
setBattle(battle: Battle): void {
|
|
|
|
this.battle = battle;
|
|
|
|
}
|
2015-02-09 00:00:00 +00:00
|
|
|
|
2015-03-12 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
|
|
|
}
|