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

91 lines
2.4 KiB
TypeScript
Raw Normal View History

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-03-24 00:00:00 +00:00
// Current fleet location
location: StarLocation;
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
constructor(player: Player = null) {
2015-03-03 00:00:00 +00:00
super();
this.player = player || new 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;
}
2015-03-25 00:00:00 +00:00
// Set the current location of the fleet
setLocation(location: StarLocation): void {
this.location = location;
this.player.setVisited(this.location.star);
// Check encounter
var battle = this.location.enterLocation(this.player.fleet);
if (battle) {
this.player.setBattle(battle);
}
2015-03-25 00:00:00 +00:00
}
// Add a ship in this fleet
addShip(ship: Ship): void {
if (this.ships.indexOf(ship) < 0) {
this.ships.push(ship);
}
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
// 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);
}
// Use the current warp location to make a jump to another star
jump(): boolean {
if (this.location && this.location.type === StarLocationType.WARP && this.location.jump_dest) {
this.player.fleet.setLocation(this.player.fleet.location.jump_dest);
return true;
} else {
return false;
}
}
2014-12-29 00:00:00 +00:00
}
2015-01-07 00:00:00 +00:00
}