1
0
Fork 0
spacetac/src/ui/common/InputManager.ts

60 lines
1.8 KiB
TypeScript

module TS.SpaceTac.UI {
// Manager for keyboard input / bindings
// Action callbacks will receive the view as 'this' context
export class InputManager {
private view: BaseView;
private game: MainUI;
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;
this.input.reset(true);
// 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, "", () => {
if (this.cheats_enabled) {
this.cheat = !this.cheat;
this.game.displayMessage(this.cheat ? "Cheats enabled" : "Cheats disabled");
}
});
}
// 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, `Cheat: ${desc}`, () => {
if (this.cheat) {
console.warn(`Cheat ! ${desc}`);
action();
}
});
}
}
}