90 lines
No EOL
2.6 KiB
JavaScript
90 lines
No EOL
2.6 KiB
JavaScript
class Input {
|
|
constructor(id) {
|
|
this.keysDown = new Set()
|
|
this.dir = 0;
|
|
|
|
this.canvas = document.getElementById(id);
|
|
|
|
this.mouseX = 0
|
|
this.mouseY = 0
|
|
|
|
// Event listener pour la position de la souris
|
|
this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this))
|
|
|
|
this.canvas.addEventListener("click", function(event) {
|
|
console.log("Clic de la souris");
|
|
});
|
|
|
|
window.addEventListener("keydown", function(event) {
|
|
switch(event.key) {
|
|
case "ArrowUp":
|
|
console.log("Flèche du haut");
|
|
break;
|
|
case "ArrowDown":
|
|
console.log("Flèche du bas");
|
|
break;
|
|
case "ArrowLeft":
|
|
console.log("Flèche de gauche pressée");
|
|
break;
|
|
case "ArrowRight":
|
|
console.log("Flèche de droite pressée");
|
|
break;
|
|
}
|
|
});
|
|
|
|
window.addEventListener("keydown", (e)=>{
|
|
this.keysDown.add(e.key.toLowerCase())
|
|
this.updateDir();
|
|
})
|
|
|
|
window.addEventListener("keyup", (e)=>{
|
|
this.keysDown.delete(e.key.toLowerCase())
|
|
this.updateDir();
|
|
})
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
get getDirection() {
|
|
return this.dir;
|
|
}
|
|
|
|
handleMouseMove(event) {
|
|
let mX = event.clientX - this.canvas.getBoundingClientRect().left;
|
|
let mY = event.clientY - this.canvas.getBoundingClientRect().top;
|
|
this.mouseX = mX
|
|
this.mouseY = mY
|
|
}
|
|
|
|
calculateAngle(playerX, playerY) {
|
|
return Math.atan2(this.mouseY - playerY, this.mouseX - playerX) //Math.atan2(
|
|
}
|
|
} |