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

104 lines
3.9 KiB
TypeScript
Raw Normal View History

2015-04-07 00:00:00 +00:00
/// <reference path="BaseView.ts"/>
2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI {
2015-04-07 00:00:00 +00:00
export class MainMenu extends BaseView {
2017-03-15 21:40:19 +00:00
layer_stars: Phaser.Group;
layer_title: Phaser.Group;
button_new_game: Phaser.Button;
button_quick_battle: Phaser.Button;
button_load_game: Phaser.Button;
create() {
2017-03-15 21:40:19 +00:00
super.create();
this.layer_stars = this.addLayer();
this.layer_title = this.addLayer();
this.layer_title.x = 5000;
2017-01-22 17:41:32 +00:00
// Stars
for (let i = 0; i < 300; i++) {
let fade = Math.random() * 0.5 + 0.5;
let x = Math.random() * 0.998 + 0.001;
2017-03-15 21:40:19 +00:00
let star = this.add.image(1920 * x, Math.random() * 1080, "menu-star", 0, this.layer_stars);
2017-01-22 17:41:32 +00:00
star.anchor.set(0.5, 0.5);
star.alpha = 0.7 * fade;
star.scale.set(0.1 * fade, 0.1 * fade);
this.tweens.create(star).to({ x: -30 }, 30000 * x / fade).to({ x: 1950 }, 0.00001).to({ x: 1920 * x }, 30000 * (1 - x) / fade).loop().start();
}
// Menu buttons
2017-03-15 21:40:19 +00:00
this.button_new_game = this.addButton(322, 674, "New Game", "Start a new campaign in a generated universe", this.onNewGame);
this.button_load_game = this.addButton(960, 674, "Load Game", "Load a saved campaign", this.onLoadGame);
this.button_quick_battle = this.addButton(1606, 674, "Quick Battle", "Play a single generated battle", this.onQuickBattle);
2017-01-22 16:17:59 +00:00
// Title
2017-03-15 21:40:19 +00:00
let title = this.add.image(960, 225, "menu-title", 0, this.layer_title);
2017-01-22 16:17:59 +00:00
title.anchor.set(0.5, 0);
2017-01-23 18:12:36 +00:00
2017-03-15 21:40:19 +00:00
this.tweens.create(this.layer_title).to({ x: 0 }, 3000, Phaser.Easing.Circular.Out).start();
}
2017-03-15 21:40:19 +00:00
addButton(x: number, y: number, caption: string, tooltip: string, callback: Function): Phaser.Button {
var button = this.add.button(x - 20, y + 20, "menu-button", callback, this, null, null, null, null, this.layer_title);
2017-01-22 16:17:59 +00:00
button.anchor.set(0.5, 0);
button.input.useHandCursor = true;
2017-01-22 16:17:59 +00:00
var text = new Phaser.Text(this.game, 0, 76, caption,
{ align: "center", font: "bold 40pt Arial", fill: "#529aee" });
text.anchor.set(0.5, 0.5);
button.addChild(text);
2017-01-23 18:12:36 +00:00
button.onInputOver.add(() => {
button.loadTexture("menu-button-hover");
text.fill = "#54b9ff";
});
button.onInputOut.add(() => {
button.loadTexture("menu-button");
text.fill = "#529aee";
});
2017-03-15 21:40:19 +00:00
this.tooltip.bindStaticText(button, tooltip);
return button;
}
// Called when "New Game" is clicked
onNewGame(): void {
2017-02-09 00:00:35 +00:00
var gameui = <MainUI>this.game;
gameui.session.startNewGame();
this.game.state.start("router");
}
// Called when "Quick Battle" is clicked
onQuickBattle(): void {
2017-02-09 00:00:35 +00:00
var gameui = <MainUI>this.game;
gameui.session.startQuickBattle(true);
this.game.state.start("router");
}
// Called when "Load Game" is clicked
onLoadGame(): void {
2017-02-09 00:00:35 +00:00
var gameui = <MainUI>this.game;
if (gameui.loadGame()) {
this.game.state.start("router");
} else {
var error = this.game.add.text(this.button_load_game.x, this.button_load_game.y + 40,
"No saved game found",
2017-01-22 16:17:59 +00:00
{ font: "bold 16px Arial", fill: "#e04040" });
error.anchor.set(0.5, 0.5);
var tween = this.game.tweens.create(error);
2017-01-22 16:17:59 +00:00
tween.to({ y: error.y + 100, alpha: 0 }, 1000, Phaser.Easing.Exponential.In);
tween.onComplete.addOnce(() => {
error.destroy();
});
tween.start();
}
}
}
}