diff --git a/TODO b/TODO index 25fbb83..f898bbc 100644 --- a/TODO +++ b/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 victory screen, with looting * Add retreat from battle diff --git a/src/app/view/BaseView.ts b/src/app/view/BaseView.ts index 6390b36..1ad1162 100644 --- a/src/app/view/BaseView.ts +++ b/src/app/view/BaseView.ts @@ -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); } } } diff --git a/src/app/view/battle/BattleView.ts b/src/app/view/battle/BattleView.ts index b3b2754..8c40af2 100644 --- a/src/app/view/battle/BattleView.ts +++ b/src/app/view/battle/BattleView.ts @@ -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 diff --git a/src/app/view/common/InputManager.ts b/src/app/view/common/InputManager.ts new file mode 100644 index 0000000..95e5519 --- /dev/null +++ b/src/app/view/common/InputManager.ts @@ -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(); + } + }); + } + } +} diff --git a/src/app/view/map/UniverseMapView.ts b/src/app/view/map/UniverseMapView.ts index 93535b2..df8ca6a 100644 --- a/src/app/view/map/UniverseMapView.ts +++ b/src/app/view/map/UniverseMapView.ts @@ -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); });