1
0
Fork 0
spacetac/src/ui/BaseView.ts

275 lines
8.8 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
2017-02-16 22:59:41 +00:00
/**
* Base class for all game views
*/
export class BaseView extends Phaser.State {
// Link to the root UI
2018-01-31 18:19:50 +00:00
gameui!: MainUI
// Message notifications
2018-01-31 18:19:50 +00:00
messages!: Messages
2015-04-23 22:36:57 +00:00
// Input and key bindings
2018-01-31 18:19:50 +00:00
inputs!: InputManager
2015-04-23 22:36:57 +00:00
// Animations
2018-01-31 18:19:50 +00:00
animations!: Animations
2017-02-16 22:59:41 +00:00
// Timing
2018-01-31 18:19:50 +00:00
timer!: Timer
2017-02-16 22:59:41 +00:00
2017-03-15 21:40:19 +00:00
// Tooltip
2018-01-31 18:19:50 +00:00
tooltip_layer!: Phaser.Group
tooltip!: Tooltip
2017-03-15 21:40:19 +00:00
// Layers
2018-01-31 18:19:50 +00:00
layers!: Phaser.Group
2017-06-08 17:32:57 +00:00
// Modal dialogs
2018-01-31 18:19:50 +00:00
dialogs_layer!: Phaser.Group
dialogs_opened: UIDialog[] = []
2017-03-15 21:40:19 +00:00
2018-03-01 23:14:09 +00:00
// Verbose debug output
readonly debug = false
2015-04-07 00:00:00 +00:00
// Get the size of display
getWidth(): number {
return this.game.width || 1280;
}
getHeight(): number {
return this.game.height || 720;
}
getMidWidth(): number {
return this.getWidth() / 2;
}
getMidHeight(): number {
return this.getHeight() / 2;
}
init(...args: any[]) {
2017-02-09 00:00:35 +00:00
this.gameui = <MainUI>this.game;
this.timer = new Timer(this.gameui.headless);
}
create() {
2017-05-15 20:16:59 +00:00
// Phaser config
2017-03-15 21:40:19 +00:00
this.game.stage.backgroundColor = 0x000000;
this.game.stage.disableVisibilityChange = this.gameui.headless;
2017-05-15 20:16:59 +00:00
this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
2017-08-22 22:08:43 +00:00
this.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL;
2017-05-15 20:16:59 +00:00
this.input.maxPointers = 1;
2017-03-15 21:40:19 +00:00
2017-05-22 20:41:34 +00:00
// Tools
this.animations = new Animations(this.game.tweens);
2015-04-23 22:36:57 +00:00
this.inputs = new InputManager(this);
2017-05-22 20:41:34 +00:00
// Layers
this.layers = this.add.group(undefined, "View layers");
2017-06-08 17:32:57 +00:00
this.dialogs_layer = this.add.group(undefined, "Dialogs layer");
this.tooltip_layer = this.add.group(undefined, "Tooltip layer");
2017-03-15 21:40:19 +00:00
this.tooltip = new Tooltip(this);
2017-05-22 20:41:34 +00:00
this.messages = new Messages(this);
this.dialogs_opened = [];
2017-03-15 21:40:19 +00:00
// Browser console variable (for debugging purpose)
if (typeof window != "undefined") {
let session = this.gameui.session;
if (session) {
(<any>window).universe = session.universe;
(<any>window).player = session.player;
(<any>window).battle = session.player.getBattle();
(<any>window).view = this;
}
}
2017-02-16 22:59:41 +00:00
super.create();
}
shutdown() {
this.audio.stopMusic();
2017-02-16 22:59:41 +00:00
super.shutdown();
this.timer.cancelAll(true);
}
2017-03-15 21:40:19 +00:00
2017-06-08 17:32:57 +00:00
get audio() {
return this.gameui.audio;
}
get options() {
return this.gameui.options;
}
2017-06-08 21:00:56 +00:00
get session() {
return this.gameui.session;
}
2017-06-08 17:32:57 +00:00
/**
* Go back to the router state
*/
backToRouter() {
this.game.state.start('router');
}
2017-03-15 21:40:19 +00:00
/**
2017-10-11 20:58:08 +00:00
* Get or create a layer in the view, by its name
2017-03-15 21:40:19 +00:00
*/
2017-10-11 20:58:08 +00:00
getLayer(name: string): Phaser.Group {
let layer = <Phaser.Group>this.layers.getByName(name);
if (!layer) {
layer = this.add.group(this.layers, name);
}
2017-03-15 21:40:19 +00:00
return layer;
}
/**
* Get a network connection to the backend server
*/
getConnection(): Multi.Connection {
let device_id = this.gameui.getDeviceId();
if (device_id) {
return new Multi.Connection(device_id, new Multi.ParseRemoteStorage());
} else {
// TODO Should warn the user !
return new Multi.Connection("fake", new Multi.FakeRemoteStorage());
}
}
/**
* Auto-save current session to cloud
*
* This may be called at key points during the gameplay
*/
autoSave(): void {
let session = this.gameui.session;
if (session.primary) {
let connection = this.getConnection();
connection.publish(session, session.getDescription())
.then(() => this.messages.addMessage("Auto-saved to cloud"))
.catch(console.error)
//.catch(() => this.messages.addMessage("Error saving game to cloud"));
}
}
2017-05-15 23:15:07 +00:00
/**
2017-06-08 17:32:57 +00:00
* Open options dialog
2017-05-15 23:15:07 +00:00
*/
2018-04-12 22:03:29 +00:00
showOptions(credits = false): void {
new OptionsDialog(this, credits);
2017-05-15 23:15:07 +00:00
}
/**
* Set a value in localStorage, if available
*/
setStorage(key: string, value: string): void {
if (typeof localStorage != "undefined") {
localStorage.setItem("spacetac-" + key, value);
}
}
/**
* Get a value from localStorage
*/
getStorage(key: string): string | null {
if (typeof localStorage != "undefined") {
return localStorage.getItem("spacetac-" + key);
} else {
return null;
}
}
/**
* Check if the mouse is inside a given area
*/
isMouseInside(area: IBounded): boolean {
let pos = this.input.mousePointer.position;
return pos.x >= area.x && pos.x < area.x + area.width && pos.y >= area.y && pos.y < area.y + area.height;
}
2017-05-28 20:37:07 +00:00
2017-10-10 17:01:09 +00:00
/**
* Create a simple text
*/
newText(content: string, x = 0, y = 0, size = 16, color = "#ffffff", shadow = true, bold = false, center = true, vcenter = center, width = 0): Phaser.Text {
let style = { font: `${bold ? "bold " : ""}${size}pt SpaceTac`, fill: color, align: center ? "center" : "left" };
let text = new Phaser.Text(this.game, x, y, content, style);
text.anchor.set(center ? 0.5 : 0, vcenter ? 0.5 : 0);
if (width) {
text.wordWrap = true;
text.wordWrapWidth = width;
}
if (shadow) {
text.setShadow(3, 4, "rgba(0,0,0,0.6)", 6);
}
return text;
}
2017-05-28 20:37:07 +00:00
/**
2017-07-26 22:54:56 +00:00
* Get a new image from an atlas name
2017-05-28 20:37:07 +00:00
*/
2017-07-26 22:54:56 +00:00
newImage(name: string, x = 0, y = 0): Phaser.Image {
let info = this.getImageInfo(name);
let result = this.game.add.image(x, y, info.key, info.frame);
result.name = name;
return result;
}
2017-10-10 17:01:09 +00:00
/**
* Get a new button from an atlas name
*/
newButton(name: string, x = 0, y = 0, onclick?: Function): Phaser.Button {
let info = this.getImageInfo(name);
let button = new Phaser.Button(this.game, x, y, info.key, onclick || nop, null, info.frame, info.frame);
let clickable = bool(onclick);
button.input.useHandCursor = clickable;
if (clickable) {
UIComponent.setButtonSound(button);
}
return button;
}
2017-08-21 23:21:34 +00:00
/**
* Update an image from an atlas name
*/
2017-10-10 17:01:09 +00:00
changeImage(image: Phaser.Image | Phaser.Button, name: string): void {
2017-08-21 23:21:34 +00:00
let info = this.getImageInfo(name);
2017-08-24 23:02:32 +00:00
image.name = name;
2017-10-10 17:01:09 +00:00
if (image instanceof Phaser.Button) {
image.loadTexture(info.key);
image.setFrames(info.frame, info.frame);
} else {
image.loadTexture(info.key, info.frame);
}
2017-08-21 23:21:34 +00:00
}
2017-07-26 22:54:56 +00:00
/**
* Get an image from atlases
*/
getImageInfo(name: string): { key: string, frame: number, exists: boolean } {
2017-07-26 22:54:56 +00:00
// TODO Cache
2017-10-25 22:45:53 +00:00
if (this.game.cache.checkImageKey(name)) {
return { key: name, frame: 0, exists: true };
2017-10-25 22:45:53 +00:00
} else {
2018-03-08 19:16:05 +00:00
for (let j = 1; j <= 3; j++) {
let i = 1;
while (this.game.cache.checkImageKey(`atlas${j}-${i}`)) {
let data = this.game.cache.getFrameData(`atlas${j}-${i}`);
let frames = data.getFrames();
let frame = first(frames, frame => AssetLoading.getKey(frame.name) == `data-stage${j}-image-${name}`);
if (frame) {
return { key: `atlas${j}-${i}`, frame: frame.index, exists: true };
}
i++;
2017-10-25 22:45:53 +00:00
}
2017-07-26 22:54:56 +00:00
}
return { key: `-missing-${name}`, frame: 0, exists: false };
2017-07-26 22:54:56 +00:00
}
}
/**
2017-10-10 22:32:46 +00:00
* Returns the first image found in atlases
2017-07-26 22:54:56 +00:00
*/
2017-10-10 22:32:46 +00:00
getFirstImage(...names: string[]): string {
return first(names, name => this.getImageInfo(name).key.substr(0, 9) != '-missing-') || names[names.length - 1];
2017-05-28 20:37:07 +00:00
}
}
}