209 lines
5.2 KiB
TypeScript
209 lines
5.2 KiB
TypeScript
/// <reference path="../node_modules/phaser/types/phaser.d.ts"/>
|
|
|
|
import { RandomGenerator } from "./common/RandomGenerator"
|
|
import { iteritems, keys } from "./common/Tools"
|
|
import { GameSession } from "./core/GameSession"
|
|
import { AssetLoading } from "./ui/AssetLoading"
|
|
import { BaseView } from "./ui/BaseView"
|
|
import { BattleView } from "./ui/battle/BattleView"
|
|
import { Boot } from "./ui/Boot"
|
|
import { FleetCreationView } from "./ui/character/FleetCreationView"
|
|
import { AudioManager } from "./ui/common/AudioManager"
|
|
import { IntroView } from "./ui/intro/IntroView"
|
|
import { UniverseMapView } from "./ui/map/UniverseMapView"
|
|
import { MainMenu } from "./ui/menu/MainMenu"
|
|
import { GameOptions } from "./ui/options/GameOptions"
|
|
import { Router } from "./ui/Router"
|
|
|
|
/**
|
|
* Main class to bootstrap the whole game
|
|
*/
|
|
export class MainUI extends Phaser.Game {
|
|
// Current game session
|
|
session: GameSession
|
|
session_token: string | null
|
|
|
|
// Audio manager
|
|
audio = new AudioManager(this)
|
|
|
|
// Game options
|
|
options = new GameOptions(this)
|
|
|
|
// Storage used
|
|
storage: Storage
|
|
|
|
// Debug mode
|
|
debug = false
|
|
|
|
// Current scaling
|
|
scaling = 1
|
|
|
|
constructor(private testmode = false) {
|
|
super({
|
|
width: 1920,
|
|
height: 1080,
|
|
type: testmode ? Phaser.CANVAS : Phaser.WEBGL, // cannot really use HEADLESS because of bugs
|
|
backgroundColor: '#000000',
|
|
parent: '-space-tac',
|
|
disableContextMenu: true,
|
|
scale: {
|
|
mode: Phaser.Scale.FIT,
|
|
autoCenter: Phaser.Scale.CENTER_BOTH
|
|
},
|
|
});
|
|
|
|
this.storage = localStorage;
|
|
|
|
this.session = new GameSession();
|
|
this.session_token = null;
|
|
|
|
if (!testmode) {
|
|
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));
|
|
});
|
|
|
|
this.scene.add('boot', Boot);
|
|
this.scene.add('loading', AssetLoading);
|
|
this.scene.add('mainmenu', MainMenu);
|
|
this.scene.add('router', Router);
|
|
this.scene.add('battle', BattleView);
|
|
this.scene.add('intro', IntroView);
|
|
this.scene.add('creation', FleetCreationView);
|
|
this.scene.add('universe', UniverseMapView);
|
|
|
|
this.goToScene('boot');
|
|
}
|
|
}
|
|
|
|
get isTesting(): boolean {
|
|
return this.testmode;
|
|
}
|
|
|
|
/**
|
|
* Reset the game session
|
|
*/
|
|
resetSession(): void {
|
|
this.session = new GameSession();
|
|
this.session_token = null;
|
|
}
|
|
|
|
/**
|
|
* Display a popup message in current view
|
|
*/
|
|
displayMessage(message: string) {
|
|
iteritems(<any>this.scene.keys, (key: string, scene: BaseView) => {
|
|
if (scene.messages && this.scene.isVisible(key)) {
|
|
scene.messages.addMessage(message);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Change the active scene
|
|
*/
|
|
goToScene(name: string): void {
|
|
keys(this.scene.keys).forEach(key => {
|
|
if (this.scene.isActive(key) || this.scene.isVisible(key)) {
|
|
this.scene.stop(key);
|
|
}
|
|
});
|
|
this.scene.start(name);
|
|
}
|
|
|
|
/**
|
|
* Quit the current session, and go back to mainmenu
|
|
*/
|
|
quitGame() {
|
|
this.resetSession();
|
|
this.goToScene('router');
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
this.displayMessage("Your browser does not support saving");
|
|
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;
|
|
this.goToScene("router");
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
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");
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
return true;
|
|
}
|
|
}
|
|
}
|