2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2017-05-04 23:19:28 +00:00
|
|
|
/**
|
|
|
|
* A game session, binding a universe and a player
|
|
|
|
*
|
|
|
|
* This represents the current state of game
|
|
|
|
*/
|
2017-02-07 18:54:53 +00:00
|
|
|
export class GameSession {
|
2017-07-02 18:21:04 +00:00
|
|
|
// "Hopefully" unique session id
|
2017-05-04 23:19:28 +00:00
|
|
|
id: string
|
|
|
|
|
2015-04-07 00:00:00 +00:00
|
|
|
// Game universe
|
2017-05-04 23:19:28 +00:00
|
|
|
universe: Universe
|
2015-04-07 00:00:00 +00:00
|
|
|
|
|
|
|
// Current connected player
|
2017-05-04 23:19:28 +00:00
|
|
|
player: Player
|
2015-04-07 00:00:00 +00:00
|
|
|
|
2017-05-31 23:11:29 +00:00
|
|
|
// Starting location
|
|
|
|
start_location: StarLocation
|
|
|
|
|
2017-06-08 21:58:23 +00:00
|
|
|
// Indicator that the session is the primary one
|
|
|
|
primary = true
|
|
|
|
|
|
|
|
// Indicator of spectator mode
|
|
|
|
spectator = false
|
|
|
|
|
2015-04-07 00:00:00 +00:00
|
|
|
constructor() {
|
2017-05-04 23:19:28 +00:00
|
|
|
this.id = RandomGenerator.global.id(20);
|
2015-04-07 00:00:00 +00:00
|
|
|
this.universe = new Universe();
|
|
|
|
this.player = new Player(this.universe);
|
2017-05-31 23:11:29 +00:00
|
|
|
this.start_location = new StarLocation();
|
2015-04-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:19:28 +00:00
|
|
|
/**
|
|
|
|
* Get an indicative description of the session (to help identify game saves)
|
|
|
|
*/
|
|
|
|
getDescription(): string {
|
|
|
|
let level = this.player.fleet.getLevel();
|
|
|
|
let ships = this.player.fleet.ships.length;
|
|
|
|
return `Level ${level} - ${ships} ships`;
|
|
|
|
}
|
|
|
|
|
2015-04-07 00:00:00 +00:00
|
|
|
// Load a game state from a string
|
|
|
|
static loadFromString(serialized: string): GameSession {
|
2017-02-09 00:00:35 +00:00
|
|
|
var serializer = new Serializer(TS.SpaceTac);
|
2015-04-07 00:00:00 +00:00
|
|
|
return <GameSession>serializer.unserialize(serialized);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serializes the game state to a string
|
|
|
|
saveToString(): string {
|
2017-02-09 00:00:35 +00:00
|
|
|
var serializer = new Serializer(TS.SpaceTac);
|
2015-04-07 00:00:00 +00:00
|
|
|
return serializer.serialize(this);
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:11:29 +00:00
|
|
|
/**
|
|
|
|
* Generate a real single player game (campaign)
|
|
|
|
*
|
|
|
|
* If *fleet* is false, the player fleet will be empty, and needs to be set with *setCampaignFleet*.
|
|
|
|
*/
|
2017-06-27 15:42:28 +00:00
|
|
|
startNewGame(fleet = true, story = false): void {
|
2015-04-07 00:00:00 +00:00
|
|
|
this.universe = new Universe();
|
|
|
|
this.universe.generate();
|
|
|
|
|
2017-05-31 23:11:29 +00:00
|
|
|
this.start_location = this.universe.getStartLocation();
|
|
|
|
this.start_location.clearEncounter();
|
|
|
|
this.start_location.addShop();
|
2015-04-07 00:00:00 +00:00
|
|
|
|
2017-02-09 00:00:35 +00:00
|
|
|
this.player = new Player(this.universe);
|
2017-05-31 23:11:29 +00:00
|
|
|
|
|
|
|
if (fleet) {
|
2017-06-27 15:42:28 +00:00
|
|
|
this.setCampaignFleet(null, story);
|
2017-05-31 23:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the initial campaign fleet, null for a default fleet
|
2017-06-27 15:42:28 +00:00
|
|
|
*
|
|
|
|
* If *story* is true, the main story arc will be started.
|
2017-05-31 23:11:29 +00:00
|
|
|
*/
|
2017-06-27 15:42:28 +00:00
|
|
|
setCampaignFleet(fleet: Fleet | null = null, story = true) {
|
2017-05-31 23:11:29 +00:00
|
|
|
if (fleet) {
|
|
|
|
this.player.fleet = fleet;
|
|
|
|
} else {
|
|
|
|
let fleet_generator = new FleetGenerator();
|
2017-07-02 18:21:04 +00:00
|
|
|
this.player.fleet = fleet_generator.generate(1, this.player, 2);
|
2017-05-31 23:11:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.player.fleet.setLocation(this.start_location);
|
2017-03-23 18:58:09 +00:00
|
|
|
this.player.fleet.credits = 500;
|
2017-06-27 15:42:28 +00:00
|
|
|
|
|
|
|
if (story) {
|
|
|
|
this.player.missions.startMainStory(this.universe, this.player.fleet);
|
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start a new "quick battle" game
|
|
|
|
startQuickBattle(with_ai: boolean = false): void {
|
2017-02-16 22:59:41 +00:00
|
|
|
var battle = Battle.newQuickRandom();
|
2015-04-07 00:00:00 +00:00
|
|
|
this.player = battle.fleets[0].player;
|
|
|
|
this.player.setBattle(battle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.player.getBattle();
|
|
|
|
}
|
2017-02-05 22:03:35 +00:00
|
|
|
|
2017-03-15 22:00:15 +00:00
|
|
|
/**
|
|
|
|
* Set the end of current battle
|
|
|
|
*/
|
|
|
|
setBattleEnded() {
|
|
|
|
let battle = this.getBattle();
|
|
|
|
|
|
|
|
if (battle && battle.ended) {
|
2017-05-09 17:19:26 +00:00
|
|
|
// Generate experience
|
|
|
|
battle.outcome.grantExperience(battle.fleets);
|
|
|
|
|
2017-03-15 22:00:15 +00:00
|
|
|
if (battle.outcome.winner == this.player.fleet) {
|
|
|
|
// In case of victory, generate loot
|
|
|
|
battle.outcome.createLoot(battle);
|
|
|
|
|
|
|
|
// In case of victorious encounter, clear the encouter
|
|
|
|
let location = this.player.fleet.location;
|
|
|
|
if (location) {
|
|
|
|
location.clearEncounter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-05 22:03:35 +00:00
|
|
|
/**
|
2017-05-31 23:11:29 +00:00
|
|
|
* Returns true if the session has a universe to explore
|
2017-02-05 22:03:35 +00:00
|
|
|
*/
|
|
|
|
hasUniverse(): boolean {
|
|
|
|
return this.universe.stars.length > 0;
|
|
|
|
}
|
2017-05-31 23:11:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if initial fleet creation has been done.
|
|
|
|
*/
|
|
|
|
isFleetCreated(): boolean {
|
|
|
|
return this.player.fleet.ships.length > 0;
|
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|