1
0
Fork 0

Added InputManager and cheat mode

This commit is contained in:
Michaël Lemaire 2015-04-24 00:36:57 +02:00
parent 8e49ca6dfc
commit 5bf278db1e
5 changed files with 68 additions and 19 deletions

1
TODO
View file

@ -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 victory screen, with looting
* Add retreat from battle

View file

@ -9,6 +9,9 @@ module SpaceTac.View {
// Message notifications
messages: Messages;
// Input and key bindings
inputs: InputManager;
// Get the size of display
getWidth(): number {
return this.game.width || 1280;
@ -33,20 +36,8 @@ module SpaceTac.View {
// Notifications
this.messages = new Messages(this);
// Key mapping
var key_s = this.input.keyboard.addKey(Phaser.Keyboard.S);
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();
});
// Input manager
this.inputs = new InputManager(this);
}
}
}

View file

@ -114,8 +114,10 @@ module SpaceTac.View {
this.gameui.audio.startMusic("full-on");
// Key mapping
var key_space = this.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
key_space.onUp.add(this.onSpaceKeyPressed, this);
this.inputs.bind(Phaser.Keyboard.SPACEBAR, "End turn", this.onSpaceKeyPressed);
this.inputs.bindCheat(Phaser.Keyboard.W, "Win current battle", () => {
this.battle.endBattle(this.player.fleet);
});
}
// Leaving the view, we unbind the battle

View 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();
}
});
}
}
}

View file

@ -43,7 +43,7 @@ module SpaceTac.View {
this.gameui.audio.startMusic("walking-along");
// 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
@ -107,7 +107,6 @@ module SpaceTac.View {
// Reveal the whole map (this is a cheat)
revealAll(): void {
console.warn("Cheat : reveal whole map");
this.universe.stars.forEach((star: Game.Star) => {
this.player.setVisited(star);
});