Added InputManager and cheat mode
This commit is contained in:
parent
8e49ca6dfc
commit
5bf278db1e
1
TODO
1
TODO
|
@ -1,4 +1,3 @@
|
||||||
* Add a cheat system, to use for development (win battle, revive killed ship ...)
|
|
||||||
* Add a defeat screen (game over for now)
|
* Add a defeat screen (game over for now)
|
||||||
* Add a victory screen, with looting
|
* Add a victory screen, with looting
|
||||||
* Add retreat from battle
|
* Add retreat from battle
|
||||||
|
|
|
@ -9,6 +9,9 @@ module SpaceTac.View {
|
||||||
// Message notifications
|
// Message notifications
|
||||||
messages: Messages;
|
messages: Messages;
|
||||||
|
|
||||||
|
// Input and key bindings
|
||||||
|
inputs: InputManager;
|
||||||
|
|
||||||
// Get the size of display
|
// Get the size of display
|
||||||
getWidth(): number {
|
getWidth(): number {
|
||||||
return this.game.width || 1280;
|
return this.game.width || 1280;
|
||||||
|
@ -33,20 +36,8 @@ module SpaceTac.View {
|
||||||
// Notifications
|
// Notifications
|
||||||
this.messages = new Messages(this);
|
this.messages = new Messages(this);
|
||||||
|
|
||||||
// Key mapping
|
// Input manager
|
||||||
var key_s = this.input.keyboard.addKey(Phaser.Keyboard.S);
|
this.inputs = new InputManager(this);
|
||||||
key_s.onUp.add(() => {
|
|
||||||
this.gameui.saveGame();
|
|
||||||
});
|
|
||||||
var key_l = this.input.keyboard.addKey(Phaser.Keyboard.L);
|
|
||||||
key_l.onUp.add(() => {
|
|
||||||
this.gameui.loadGame();
|
|
||||||
this.game.state.start("router");
|
|
||||||
});
|
|
||||||
var key_m = this.input.keyboard.addKey(Phaser.Keyboard.M);
|
|
||||||
key_m.onUp.add(() => {
|
|
||||||
this.gameui.audio.toggleMute();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,8 +114,10 @@ module SpaceTac.View {
|
||||||
this.gameui.audio.startMusic("full-on");
|
this.gameui.audio.startMusic("full-on");
|
||||||
|
|
||||||
// Key mapping
|
// Key mapping
|
||||||
var key_space = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
|
this.inputs.bind(Phaser.Keyboard.SPACEBAR, "End turn", this.onSpaceKeyPressed);
|
||||||
key_space.onUp.add(this.onSpaceKeyPressed, this);
|
this.inputs.bindCheat(Phaser.Keyboard.W, "Win current battle", () => {
|
||||||
|
this.battle.endBattle(this.player.fleet);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leaving the view, we unbind the battle
|
// Leaving the view, we unbind the battle
|
||||||
|
|
58
src/app/view/common/InputManager.ts
Normal file
58
src/app/view/common/InputManager.ts
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
module SpaceTac.View {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// Manager for keyboard input / bindings
|
||||||
|
// Action callbacks will receive the view as 'this' context
|
||||||
|
export class InputManager {
|
||||||
|
|
||||||
|
private view: BaseView;
|
||||||
|
|
||||||
|
private game: GameUI;
|
||||||
|
|
||||||
|
private input: Phaser.Input;
|
||||||
|
|
||||||
|
private cheats_enabled: boolean;
|
||||||
|
|
||||||
|
private cheat: boolean;
|
||||||
|
|
||||||
|
constructor(view: BaseView) {
|
||||||
|
this.view = view;
|
||||||
|
this.game = view.gameui;
|
||||||
|
this.input = view.input;
|
||||||
|
this.cheats_enabled = true;
|
||||||
|
this.cheat = false;
|
||||||
|
|
||||||
|
// Default mappings
|
||||||
|
this.bind(Phaser.Keyboard.S, "Quick save", () => {
|
||||||
|
this.game.saveGame();
|
||||||
|
});
|
||||||
|
this.bind(Phaser.Keyboard.L, "Quick load", () => {
|
||||||
|
this.game.loadGame();
|
||||||
|
this.game.state.start("router");
|
||||||
|
});
|
||||||
|
this.bind(Phaser.Keyboard.M, "Toggle sound", () => {
|
||||||
|
this.game.audio.toggleMute();
|
||||||
|
});
|
||||||
|
this.bind(Phaser.Keyboard.NUMPAD_ADD, null, () => {
|
||||||
|
if (this.cheats_enabled) {
|
||||||
|
this.cheat = !this.cheat;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind a key to an action
|
||||||
|
bind(key: number, desc: string, action: Function): void {
|
||||||
|
this.input.keyboard.addKey(key).onUp.add(action, this.view);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bind a key to a cheat action
|
||||||
|
bindCheat(key: number, desc: string, action: Function): void {
|
||||||
|
this.bind(key, null, () => {
|
||||||
|
if (this.cheat) {
|
||||||
|
console.warn("Cheat ! " + desc);
|
||||||
|
action();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,7 +43,7 @@ module SpaceTac.View {
|
||||||
this.gameui.audio.startMusic("walking-along");
|
this.gameui.audio.startMusic("walking-along");
|
||||||
|
|
||||||
// Inputs
|
// Inputs
|
||||||
this.input.keyboard.addKey(Phaser.Keyboard.R).onUp.addOnce(this.revealAll, this);
|
this.inputs.bindCheat(Phaser.Keyboard.R, "Reveal whole map", this.revealAll);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Leaving the view, unbind and destroy
|
// Leaving the view, unbind and destroy
|
||||||
|
@ -107,7 +107,6 @@ module SpaceTac.View {
|
||||||
|
|
||||||
// Reveal the whole map (this is a cheat)
|
// Reveal the whole map (this is a cheat)
|
||||||
revealAll(): void {
|
revealAll(): void {
|
||||||
console.warn("Cheat : reveal whole map");
|
|
||||||
this.universe.stars.forEach((star: Game.Star) => {
|
this.universe.stars.forEach((star: Game.Star) => {
|
||||||
this.player.setVisited(star);
|
this.player.setVisited(star);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue