54 lines
No EOL
1 KiB
JavaScript
54 lines
No EOL
1 KiB
JavaScript
class Keyboard{
|
|
constructor(){
|
|
this.keysDown = new Set()
|
|
}
|
|
|
|
updateDir(){
|
|
let newDir = 0;
|
|
if(this.keysDown.has('z')){
|
|
if(this.keysDown.has('d')){
|
|
newDir = 2;
|
|
}else if(this.keysDown.has('s')){
|
|
newDir = 0;
|
|
}else if(this.keysDown.has('q')){
|
|
newDir = 8;
|
|
}else{
|
|
newDir = 1;
|
|
}
|
|
}else if(this.keysDown.has('d')){
|
|
if(this.keysDown.has('s')){
|
|
newDir = 4;
|
|
}else if(this.keysDown.has('q')){
|
|
newDir = 0;
|
|
}else{
|
|
newDir = 3;
|
|
}
|
|
}else if(this.keysDown.has('s')){
|
|
if(this.keysDown.has('q')){
|
|
newDir = 6;
|
|
}else{
|
|
newDir = 5;
|
|
}
|
|
}else if(this.keysDown.has('q')){
|
|
newDir = 7;
|
|
}
|
|
|
|
for (var i = players.length - 1; i >= 0; i--) {
|
|
if(players[i].id==playerId){
|
|
players[i].changeDirection(newDir);
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
})
|
|
}
|
|
} |