52 lines
No EOL
951 B
JavaScript
52 lines
No EOL
951 B
JavaScript
class Keyboard{
|
|
constructor(){
|
|
this.keysDown = new Set()
|
|
this.dir = 0;
|
|
}
|
|
|
|
updateDir(){
|
|
if(this.keysDown.has('z')){
|
|
if(this.keysDown.has('d')){
|
|
this.dir = 2;
|
|
}else if(this.keysDown.has('s')){
|
|
this.dir = 0;
|
|
}else if(this.keysDown.has('q')){
|
|
this.dir = 8;
|
|
}else{
|
|
this.dir = 1;
|
|
}
|
|
}else if(this.keysDown.has('d')){
|
|
if(this.keysDown.has('s')){
|
|
this.dir = 4;
|
|
}else if(this.keysDown.has('q')){
|
|
this.dir = 0;
|
|
}else{
|
|
this.dir = 3;
|
|
}
|
|
}else if(this.keysDown.has('s')){
|
|
if(this.keysDown.has('q')){
|
|
this.dir = 6;
|
|
}else{
|
|
this.dir = 5;
|
|
}
|
|
}else if(this.keysDown.has('q')){
|
|
this.dir = 7;
|
|
}
|
|
}
|
|
|
|
getDirection(){
|
|
return this.dir;
|
|
}
|
|
|
|
init(){
|
|
document.addEventListener("keydown", (e)=>{
|
|
this.keysDown.add(e.key.toLowerCase())
|
|
this.updateDir();
|
|
})
|
|
|
|
document.addEventListener("keyup", (e)=>{
|
|
this.keysDown.delete(e.key.toLowerCase())
|
|
this.updateDir();
|
|
})
|
|
}
|
|
} |