1
0
Fork 0
spacetac/src/MainUI.ts

218 lines
6.5 KiB
TypeScript
Raw Normal View History

2019-05-13 16:36:25 +00:00
/// <reference path="../node_modules/phaser/types/phaser.d.ts"/>
2014-12-29 00:00:00 +00:00
declare var global: any;
declare var module: any;
2017-01-22 17:00:59 +00:00
if (typeof window != "undefined") {
// If jasmine is not present, ignore describe
2017-01-22 17:00:59 +00:00
(<any>window).describe = (<any>window).describe || function () { };
} else {
if (typeof global != "undefined") {
// In node, does not extend Phaser classes
var handler = {
2018-05-15 14:57:45 +00:00
get(target: any, name: any): any {
2019-05-13 21:17:58 +00:00
return new Proxy(function () { }, handler);
}
}
2019-05-13 21:17:58 +00:00
global.Phaser = new Proxy(function () { }, handler);
}
if (typeof module != "undefined") {
module.exports = { TK };
}
}
2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
2019-05-13 21:17:58 +00:00
/**
* Main class to bootstrap the whole game
*/
2017-02-09 00:00:35 +00:00
export class MainUI extends Phaser.Game {
// Current game session
session: GameSession
session_token: string | null
2019-05-14 09:14:41 +00:00
// Audio manager
audio = new UI.Audio(this)
2017-06-08 17:32:57 +00:00
// Game options
2019-05-14 09:14:41 +00:00
options = new UI.GameOptions(this)
2017-06-08 17:32:57 +00:00
2017-02-10 00:08:28 +00:00
// Storage used
storage: Storage
2017-02-10 00:08:28 +00:00
2018-05-15 14:57:45 +00:00
// Debug mode
debug = false
2017-02-10 00:08:28 +00:00
// Current scaling
scaling = 1
2019-05-06 17:14:12 +00:00
constructor(private testmode = false) {
2018-05-15 14:57:45 +00:00
super({
width: 1920,
height: 1080,
2019-05-13 21:17:58 +00:00
type: testmode ? Phaser.CANVAS : Phaser.WEBGL, // cannot really use HEADLESS because of bugs
2018-05-15 14:57:45 +00:00
backgroundColor: '#000000',
parent: '-space-tac',
disableContextMenu: true,
2019-05-06 17:14:12 +00:00
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH
},
2018-05-15 14:57:45 +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();
this.session_token = null;
2019-05-06 17:14:12 +00:00
if (!testmode) {
2018-06-12 21:40:33 +00:00
this.events.on("blur", () => {
this.scene.scenes.forEach(scene => this.scene.pause(scene));
});
this.events.on("focus", () => {
this.scene.scenes.forEach(scene => this.scene.resume(scene));
});
2018-05-15 14:57:45 +00:00
this.scene.add('boot', UI.Boot);
this.scene.add('loading', UI.AssetLoading);
this.scene.add('mainmenu', UI.MainMenu);
this.scene.add('router', UI.Router);
this.scene.add('battle', UI.BattleView);
this.scene.add('intro', UI.IntroView);
this.scene.add('creation', UI.FleetCreationView);
this.scene.add('universe', UI.UniverseMapView);
this.goToScene('boot');
2017-02-10 00:08:28 +00:00
}
}
2019-05-06 17:14:12 +00:00
get isTesting(): boolean {
return this.testmode;
2018-05-15 14:57:45 +00:00
}
/**
* Reset the game session
*/
resetSession(): void {
this.session = new GameSession();
this.session_token = null;
}
2017-02-10 00:08:28 +00:00
/**
* Display a popup message in current view
*/
displayMessage(message: string) {
2018-05-15 14:57:45 +00:00
iteritems(<any>this.scene.keys, (key: string, scene: UI.BaseView) => {
if (scene.messages && this.scene.isVisible(key)) {
scene.messages.addMessage(message);
}
});
}
/**
* Change the active scene
*/
goToScene(name: string): void {
2018-06-05 21:08:35 +00:00
keys(this.scene.keys).forEach(key => {
if (this.scene.isActive(key) || this.scene.isVisible(key)) {
this.scene.stop(key);
2018-05-15 14:57:45 +00:00
}
});
this.scene.start(name);
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.resetSession();
2018-05-15 14:57:45 +00:00
this.goToScene('router');
2017-03-12 23:32:41 +00:00
}
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, token?: string): void {
this.session = session;
this.session_token = token || null;
2018-05-15 14:57:45 +00:00
this.goToScene("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);
this.session_token = null;
console.log("Game loaded");
return true;
} else {
console.warn("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;
}
}
2017-06-08 17:32:57 +00:00
/**
* Check if the game is currently fullscreen
*/
isFullscreen(): boolean {
2019-05-06 17:14:12 +00:00
return this.scale.isFullscreen;
2017-06-08 17:32:57 +00:00
}
/**
* Toggle fullscreen mode.
*
* Returns true if the result is fullscreen
*/
toggleFullscreen(active: boolean | null = null): boolean {
2019-05-06 17:14:12 +00:00
if (active === false || (active !== true && this.isFullscreen())) {
this.scale.stopFullscreen();
2017-06-08 17:32:57 +00:00
return false;
} else {
2019-05-06 17:14:12 +00:00
this.scale.startFullscreen();
2017-06-08 17:32:57 +00:00
return true;
2019-05-06 17:14:12 +00:00
}
2017-06-08 17:32:57 +00:00
}
2014-12-29 00:00:00 +00:00
}
}