tetris/js/inputHandler.js

29 lines
887 B
JavaScript

class InputHandler {
constructor(eventBus) {
this.eventBus = eventBus;
this.handleKeyDown = this.handleKeyDown.bind(this);
this.keys = {
"ArrowRight":()=>{
this.eventBus.emit("move", {dx:1,dy:0});
},
"ArrowLeft":()=>{
this.eventBus.emit("move", {dx:-1, dy:0});
},
"ArrowDown":()=>{
this.eventBus.emit("move", {dx:0, dy:1});
},
"ArrowUp":()=>{
this.eventBus.emit("rotate", {});
},
"Space":()=>{
this.eventBus.emit("gameStart", {});
}
}
document.addEventListener("keydown", this.handleKeyDown);
}
handleKeyDown(event) {
if(event.code in this.keys){
this.keys[event.code]()
}
}
}