2017-02-09 00:00:35 +00:00
|
|
|
module TS.SpaceTac {
|
2015-04-07 00:00:00 +00:00
|
|
|
// A game session, binding a universe and a player
|
2017-02-07 18:54:53 +00:00
|
|
|
export class GameSession {
|
2015-04-07 00:00:00 +00:00
|
|
|
// Game universe
|
|
|
|
universe: Universe;
|
|
|
|
|
|
|
|
// Current connected player
|
|
|
|
player: Player;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.universe = new Universe();
|
|
|
|
this.player = new Player(this.universe);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a real single player game
|
|
|
|
startNewGame(): void {
|
2015-04-07 00:00:00 +00:00
|
|
|
var fleet_generator = new FleetGenerator();
|
|
|
|
|
2015-04-07 00:00:00 +00:00
|
|
|
this.universe = new Universe();
|
|
|
|
this.universe.generate();
|
|
|
|
|
|
|
|
var start_location = this.universe.stars[0].locations[0];
|
|
|
|
start_location.encounter_gen = true;
|
|
|
|
start_location.encounter = null;
|
|
|
|
|
2017-02-09 00:00:35 +00:00
|
|
|
this.player = new Player(this.universe);
|
2015-04-07 00:00:00 +00:00
|
|
|
this.player.fleet = fleet_generator.generate(1, this.player);
|
2015-04-07 00:00:00 +00:00
|
|
|
this.player.fleet.setLocation(start_location);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Return true if the session has a universe to explore
|
|
|
|
*/
|
|
|
|
hasUniverse(): boolean {
|
|
|
|
return this.universe.stars.length > 0;
|
|
|
|
}
|
2015-04-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|