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 () { };
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:52:45 +00:00
|
|
|
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 {
|
2015-04-07 00:00:00 +00:00
|
|
|
// Current game session
|
2017-02-09 00:00:35 +00:00
|
|
|
session: GameSession;
|
2015-04-07 00:00:00 +00:00
|
|
|
|
2015-04-22 21:42:40 +00:00
|
|
|
// Audio manager
|
2017-02-09 00:00:35 +00:00
|
|
|
audio: UI.Audio;
|
2015-04-22 21:42:40 +00:00
|
|
|
|
2017-06-08 17:32:57 +00:00
|
|
|
// Game options
|
|
|
|
options: UI.GameOptions;
|
|
|
|
|
2017-02-10 00:08:28 +00:00
|
|
|
// Storage used
|
|
|
|
storage: Storage;
|
|
|
|
|
|
|
|
// Headless mode
|
|
|
|
headless: boolean;
|
|
|
|
|
2015-04-22 21:42:40 +00:00
|
|
|
constructor(headless: boolean = false) {
|
2017-01-08 22:04:07 +00:00
|
|
|
super(1920, 1080, headless ? Phaser.HEADLESS : Phaser.AUTO, '-space-tac');
|
2015-04-22 21:42:40 +00:00
|
|
|
|
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);
|
2017-02-10 00:08:28 +00:00
|
|
|
this.storage = localStorage;
|
|
|
|
|
2017-02-09 00:00:35 +00:00
|
|
|
this.session = new GameSession();
|
2015-02-27 00:00:00 +00:00
|
|
|
|
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);
|
2017-05-31 23:11:29 +00:00
|
|
|
this.state.add('intro', UI.IntroView);
|
2017-02-10 00:08:28 +00:00
|
|
|
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
|
|
|
}
|
2017-06-08 17:32:57 +00:00
|
|
|
|
|
|
|
this.audio = new UI.Audio(this);
|
|
|
|
this.options = new UI.GameOptions(this);
|
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
|
|
|
}
|
2015-02-27 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");
|
2015-03-04 00:00:00 +00:00
|
|
|
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;
|
2015-03-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 23:19:28 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
2015-03-04 00:00:00 +00:00
|
|
|
if (loaded) {
|
2017-02-09 00:00:35 +00:00
|
|
|
this.session = GameSession.loadFromString(loaded);
|
2015-03-04 00:00:00 +00:00
|
|
|
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;
|
2015-03-04 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 23:19:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2017-06-08 17:32:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the game is currently fullscreen
|
|
|
|
*/
|
|
|
|
isFullscreen(): boolean {
|
|
|
|
return this.scale.isFullScreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggle fullscreen mode.
|
|
|
|
*
|
|
|
|
* Returns true if the result is fullscreen
|
|
|
|
*/
|
|
|
|
toggleFullscreen(active: boolean | null = null): boolean {
|
|
|
|
if (active === false || (active !== true && this.isFullscreen())) {
|
|
|
|
this.scale.stopFullScreen();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.scale.startFullScreen(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-12-29 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|