h-input
v1.0.18
Published
Typescript input helpers
Downloads
26
Readme
import {SnInputCmd, SnInputCmds} from "engine/input/SnInputs"; import {SnGame} from "engine/SnGame"; import {SynthButtonCodes} from "m/h-input/gamepad/SynthButtonCodes"; import {InputCls} from "m/h-input/Input"; import {InputBind, InputBinder} from "m/h-input/InputBinder"; import {RenderableTerminal} from "m/h-term/Terminal"; import {KeyCodes} from "m/wip/KeyCodes";
export class SnUserInterface {
game?: SnGame terminal?: RenderableTerminal running: boolean = false input: InputCls = new InputCls() inputBinder: InputBinder = new InputBinder(this.input)
constructor() {}
setGame(game: SnGame) { this.game = game }
setTerminal(terminal: RenderableTerminal): any { this.terminal = terminal }
addInputs() { //TODO: HTML Prevent Default! this.inputBinder.addBinds( InputBinder.createMovementBinds({ U: SnInputCmds.U, D: SnInputCmds.D, L: SnInputCmds.L, R: SnInputCmds.R, }), ) this.inputBinder.addBind( new InputBind( SnInputCmds.EndTurn, InputBinder.key(KeyCodes.Enter), InputBinder.btn(SynthButtonCodes.Start), ), ) this.inputBinder.addBind( new InputBind( SnInputCmds.NextActor, InputBinder.key(KeyCodes.Tab), InputBinder.btn(SynthButtonCodes.RB), ) ) this.inputBinder.addBind( new InputBind( SnInputCmds.PrevActor, InputBinder.key(KeyCodes.Tab,{shift: true}), InputBinder.btn(SynthButtonCodes.LB), ) )
}
startInput() { this.input.start() this.addInputs() }
stopInput() { this.input.stop() }
startLoop() { this.running = true requestAnimationFrame(this.gameLoop) }
singleLoop = (time: number) => {
this.input.beginUpdate()
this.inputBinder.beginUpdate()
if (this.game ) {
if (this.inputBinder.getCommands().length > 0) {
//Displatch the commands
this.game.handleCommands(this.inputBinder.getCommands())
}
if (this.terminal) {
this.game.render(this.terminal)
this.terminal.render()
}
}
this.inputBinder.endUpdate()
this.input.endUpdate()
}
gameLoop = (time: number) => { this.singleLoop(time) if (this.running) { requestAnimationFrame(this.gameLoop) } } }