2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2014-12-29 00:00:00 +00:00
|
|
|
// One player (human or IA)
|
2017-02-07 18:54:53 +00:00
|
|
|
export class Player {
|
2017-04-25 23:27:42 +00:00
|
|
|
// Player's name
|
|
|
|
name: string;
|
|
|
|
|
2015-04-07 00:00:00 +00:00
|
|
|
// Universe in which we are playing
|
|
|
|
universe: Universe;
|
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Current fleet
|
|
|
|
fleet: Fleet;
|
|
|
|
|
2015-03-19 00:00:00 +00:00
|
|
|
// List of visited star systems
|
2017-01-29 18:34:38 +00:00
|
|
|
visited: StarLocation[] = [];
|
2015-03-19 00:00:00 +00:00
|
|
|
|
2014-12-29 00:00:00 +00:00
|
|
|
// Create a player, with an empty fleet
|
2017-04-25 23:27:42 +00:00
|
|
|
constructor(universe: Universe = new Universe(), name = "Player") {
|
2015-04-07 00:00:00 +00:00
|
|
|
this.universe = universe;
|
2017-04-25 23:27:42 +00:00
|
|
|
this.name = name;
|
2014-12-29 00:00:00 +00:00
|
|
|
this.fleet = new Fleet(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a quick random player, with a fleet, for testing purposes
|
2017-05-02 22:49:35 +00:00
|
|
|
static newQuickRandom(name: string, level = 1, shipcount = 4, upgrade = false): Player {
|
2017-04-25 23:27:42 +00:00
|
|
|
let player = new Player(new Universe(), name);
|
|
|
|
let generator = new FleetGenerator();
|
2017-05-02 22:49:35 +00:00
|
|
|
player.fleet = generator.generate(level, player, shipcount, upgrade);
|
2014-12-29 00:00:00 +00:00
|
|
|
return player;
|
|
|
|
}
|
2015-03-19 00:00:00 +00:00
|
|
|
|
2017-01-29 18:34:38 +00:00
|
|
|
/**
|
|
|
|
* Return true if the player has visited at least one location in a given system.
|
|
|
|
*/
|
|
|
|
hasVisitedSystem(system: Star): boolean {
|
|
|
|
return any(this.visited, location => location.star == system);
|
2015-03-19 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 18:34:38 +00:00
|
|
|
/**
|
|
|
|
* Return true if the player has visited a given star location.
|
|
|
|
*/
|
|
|
|
hasVisitedLocation(location: StarLocation): boolean {
|
|
|
|
return contains(this.visited, location);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a star location as visited.
|
|
|
|
*/
|
|
|
|
setVisited(location: StarLocation): void {
|
|
|
|
add(this.visited, location);
|
2015-03-19 00:00:00 +00:00
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
|
|
|
|
// Get currently played battle, null when none is in progress
|
2017-03-09 17:11:00 +00:00
|
|
|
getBattle(): Battle | null {
|
2015-04-07 00:00:00 +00:00
|
|
|
return this.fleet.battle;
|
|
|
|
}
|
2017-03-09 17:11:00 +00:00
|
|
|
setBattle(battle: Battle | null): void {
|
2015-04-07 00:00:00 +00:00
|
|
|
this.fleet.setBattle(battle);
|
|
|
|
}
|
2015-04-21 22:17:00 +00:00
|
|
|
|
2017-03-14 22:28:07 +00:00
|
|
|
/**
|
|
|
|
* Exit the current battle unconditionally, if any
|
|
|
|
*
|
|
|
|
* This does not apply retreat penalties, or battle outcome, only unbind the battle from current session
|
|
|
|
*/
|
2015-04-21 22:17:00 +00:00
|
|
|
exitBattle(): void {
|
|
|
|
this.setBattle(null);
|
|
|
|
}
|
2017-03-14 22:28:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Revert current battle, and put the player's fleet to its previous location, as if the battle never happened
|
|
|
|
*/
|
|
|
|
revertBattle(): void {
|
|
|
|
this.exitBattle();
|
|
|
|
|
|
|
|
if (this.fleet.previous_location) {
|
|
|
|
this.fleet.setLocation(this.fleet.previous_location);
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|