Input Manager
Still making progress. I build an input manager class that simplifies adding/removing key input and allowing the player to customize the controls.
I define keyboard input events like so:
k['moveForward'] = new KeyResponse([38, 87, 16],/* arrow_up, w, shift */);
k['moveBackward'] = new KeyResponse([40, 83], /* arrow_down, s */);
k['strafeLeft'] = new KeyResponse([37, 65], /* arrow_left, a */);
k['strafeRight'] = new KeyResponse([39, 68], /* arrow_right, d */);
k['jump'] = new KeyResponse([32] /* space */);And then we can hook up a function that responds to the events anywhere we like, in this case this is in my player character class:
let input = this.game.inputManager;
input.setKeyDown('moveForward', () => { moveDir.forward = 1; });
input.setKeyUp('moveForward', () => { moveDir.forward = 0; });
input.setKeyDown('moveBackward', () => { moveDir.back = 1; });
input.setKeyUp('moveBackward', () => { moveDir.back = 0; });
input.setKeyDown('strafeLeft', () => { moveDir.left = 1; });
input.setKeyUp('strafeLeft', () => { moveDir.left = 0; });
input.setKeyDown('strafeRight', () => { moveDir.right = 1; });
input.setKeyUp('strafeRight', () => { moveDir.right = 0; });
input.setKeyDown('jump', () => { this.jump(); });
input.setKeyUp('jump', () => { this.resetJump(); });