2023-11-19 19:20:02 +01:00
|
|
|
class Keyboard{
|
|
|
|
constructor(){
|
|
|
|
this.keysDown = new Set()
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 0;
|
2023-11-19 19:20:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
updateDir(){
|
|
|
|
if(this.keysDown.has('z')){
|
|
|
|
if(this.keysDown.has('d')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 2;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else if(this.keysDown.has('s')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 0;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else if(this.keysDown.has('q')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 8;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else{
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 1;
|
2023-11-19 19:20:02 +01:00
|
|
|
}
|
|
|
|
}else if(this.keysDown.has('d')){
|
|
|
|
if(this.keysDown.has('s')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 4;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else if(this.keysDown.has('q')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 0;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else{
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 3;
|
2023-11-19 19:20:02 +01:00
|
|
|
}
|
|
|
|
}else if(this.keysDown.has('s')){
|
|
|
|
if(this.keysDown.has('q')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 6;
|
2023-11-19 19:20:02 +01:00
|
|
|
}else{
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 5;
|
2023-11-19 19:20:02 +01:00
|
|
|
}
|
|
|
|
}else if(this.keysDown.has('q')){
|
2023-11-20 10:03:07 +01:00
|
|
|
this.dir = 7;
|
2023-11-19 19:20:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 10:03:07 +01:00
|
|
|
getDirection(){
|
|
|
|
return this.dir;
|
|
|
|
}
|
|
|
|
|
2023-11-19 19:20:02 +01:00
|
|
|
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();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|