1
0
Fork 0
spacetac/src/scripts/GameRouter.ts

62 lines
1.9 KiB
TypeScript
Raw Normal View History

2014-12-29 00:00:00 +00:00
/// <reference path="definitions/phaser.d.ts"/>
module SpaceTac {
2015-01-07 00:00:00 +00:00
"use strict";
2014-12-29 00:00:00 +00:00
// Router between game views
export class GameRouter extends Phaser.Game {
// Currently playing universe
universe: Game.Universe;
2014-12-29 00:00:00 +00:00
constructor() {
2015-01-18 00:00:00 +00:00
super(1280, 720, Phaser.AUTO, '-space-tac');
2014-12-29 00:00:00 +00:00
this.universe = this.newGame();
2014-12-29 00:00:00 +00:00
this.state.add('boot', View.Boot);
this.state.add('preload', View.Preload);
this.state.add('main', View.Main);
this.state.add('battle', View.BattleView);
this.state.start('boot');
}
// Start a new game
newGame(): Game.Universe {
// Currently create a quick battle
var universe = new Game.Universe();
universe.startQuickBattle(true);
return universe;
}
// Save current game in local browser storage
saveGame(): boolean {
if (typeof(Storage) !== "undefined") {
localStorage.setItem("spacetac-savegame", this.universe.saveToString());
console.log("Game saved");
return true;
} else {
console.error("localStorage not available");
}
}
// Load current game from local browser storage
loadGame(): boolean {
if (typeof(Storage) !== "undefined") {
var loaded = localStorage.getItem("spacetac-savegame");
if (loaded) {
this.universe = Game.Universe.loadFromString(loaded);
this.state.start('main');
console.log("Game loaded");
return true;
} else {
console.error("No saved game found");
return false;
}
} else {
console.error("localStorage not available");
}
}
2014-12-29 00:00:00 +00:00
}
}