1
0
Fork 0
spacetac/src/MainUI.ts

137 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-01-03 22:17:52 +00:00
/// <reference path="../typings/index.d.ts"/>
2014-12-29 00:00:00 +00:00
2017-01-22 17:00:59 +00:00
if (typeof window != "undefined") {
(<any>window).describe = (<any>window).describe || function () { };
}
module TS.SpaceTac {
2014-12-29 00:00:00 +00:00
// Router between game views
2017-02-09 00:00:35 +00:00
export class MainUI extends Phaser.Game {
// Current game session
2017-02-09 00:00:35 +00:00
session: GameSession;
// Audio manager
2017-02-09 00:00:35 +00:00
audio: UI.Audio;
2017-02-10 00:08:28 +00:00
// Storage used
storage: Storage;
// Headless mode
headless: boolean;
constructor(headless: boolean = false) {
super(1920, 1080, headless ? Phaser.HEADLESS : Phaser.AUTO, '-space-tac');
2017-02-10 00:08:28 +00:00
this.headless = headless;
2017-02-09 00:00:35 +00:00
this.audio = new UI.Audio(this);
2014-12-29 00:00:00 +00:00
2017-02-10 00:08:28 +00:00
this.storage = localStorage;
2017-02-09 00:00:35 +00:00
this.session = new GameSession();
2017-02-10 00:08:28 +00:00
if (!headless) {
this.state.add('boot', UI.Boot);
this.state.add('preload', UI.Preload);
this.state.add('mainmenu', UI.MainMenu);
this.state.add('router', UI.Router);
this.state.add('battle', UI.BattleView);
this.state.add('universe', UI.UniverseMapView);
this.state.start('boot');
}
}
boot() {
2017-05-31 15:53:11 +00:00
if (this.renderType == Phaser.HEADLESS) {
this.headless = true;
}
super.boot();
2017-02-10 00:08:28 +00:00
if (!this.headless) {
2017-05-31 15:53:11 +00:00
this.plugins.add((<any>Phaser.Plugin).SceneGraph);
2017-02-10 00:08:28 +00:00
}
}
2014-12-29 00:00:00 +00:00
2017-02-10 00:08:28 +00:00
/**
* Display a popup message in current view
*/
displayMessage(message: string) {
let state = <UI.BaseView>this.state.getCurrentState();
if (state) {
state.messages.addMessage(message);
}
2014-12-29 00:00:00 +00:00
}
2017-03-12 23:32:41 +00:00
/**
* Quit the current session, and go back to mainmenu
*/
quitGame() {
this.session = new GameSession();
this.state.start('router');
}
2017-02-10 00:08:28 +00:00
/**
* Save current game in local browser storage
*/
saveGame(name = "spacetac-savegame"): boolean {
if (typeof this.storage != "undefined") {
this.storage.setItem(name, this.session.saveToString());
this.displayMessage("Game saved");
return true;
} else {
2017-02-10 00:08:28 +00:00
this.displayMessage("Your browser does not support saving");
2016-10-26 21:15:04 +00:00
return false;
}
}
/**
* Set the current game session, and redirect to view router
*/
setSession(session: GameSession): void {
this.session = session;
this.state.start("router");
}
2017-02-10 00:08:28 +00:00
/**
* Load current game from local browser storage
*/
loadGame(name = "spacetac-savegame"): boolean {
if (typeof this.storage != "undefined") {
var loaded = this.storage.getItem(name);
if (loaded) {
2017-02-09 00:00:35 +00:00
this.session = GameSession.loadFromString(loaded);
console.log("Game loaded");
return true;
} else {
console.error("No saved game found");
return false;
}
} else {
console.error("localStorage not available");
2016-10-26 21:15:04 +00:00
return false;
}
}
/**
* Get an hopefully unique device identifier
*/
getDeviceId(): string | null {
if (this.storage) {
const key = "spacetac-device-id";
let stored = this.storage.getItem(key);
if (stored) {
return stored;
} else {
let generated = RandomGenerator.global.id(20);
this.storage.setItem(key, generated);
return generated;
}
} else {
return null;
}
}
2014-12-29 00:00:00 +00:00
}
}