GrandTabernacleAutoVI/public_html/js/input.js

87 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-11-15 09:09:13 +01:00
class Input {
2023-12-08 13:50:49 +01:00
constructor(idCanvas) {
2023-11-29 16:21:20 +01:00
this.keysDown = new Set()
this.dir = 0;
2023-12-06 09:50:34 +01:00
2023-12-08 13:50:49 +01:00
this.canvas = document.getElementById(idCanvas);
2023-11-15 09:09:13 +01:00
2023-12-06 09:50:34 +01:00
this.canvas.addEventListener("click", (e) => {
2023-12-08 13:50:49 +01:00
if(player==null || bullets==null){
2023-12-06 09:50:34 +01:00
return;
2023-12-06 19:49:59 +01:00
}
2023-12-08 13:50:49 +01:00
let mouseX = e.clientX - this.canvas.getBoundingClientRect().left;
let mouseY = e.clientY - this.canvas.getBoundingClientRect().top;
if(player.x>=2000 && player.y>=2000) {
mouseX+=2000;
mouseY+=2000;
2023-12-07 22:31:50 +01:00
}
2023-12-08 13:50:49 +01:00
let dx = mouseX-player.x;
let dy = mouseY-player.y;
2023-12-07 22:31:50 +01:00
2023-12-06 09:50:34 +01:00
let norm = Math.sqrt(dx*dx+dy*dy);
2023-12-08 13:50:49 +01:00
let b = new Bullet(player.x,player.y,dx/norm,dy/norm,player.id);
2023-12-06 19:49:59 +01:00
2023-12-08 13:50:49 +01:00
bullets.push(b);
renderer.addBullet(b);
network.newBullet(b.x,b.y,b.dx,b.dy,b.parentId);
2023-11-15 09:09:13 +01:00
});
2023-11-29 16:21:20 +01:00
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(){
2023-11-29 17:50:21 +01:00
if(this.player==null)
return;
2023-11-29 18:01:11 +01:00
let oldDir=this.dir;
this.dir=0;
2023-12-06 08:58:18 +01:00
if(this.keysDown.has('z') || this.keysDown.has('arrowup')){
if(this.keysDown.has('d') || this.keysDown.has('arrowright')){
2023-11-29 16:21:20 +01:00
this.dir = 2;
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){
2023-11-29 16:21:20 +01:00
this.dir = 0;
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){
2023-11-29 16:21:20 +01:00
this.dir = 8;
}else{
this.dir = 1;
}
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('d') || this.keysDown.has('arrowright')){
if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){
2023-11-29 16:21:20 +01:00
this.dir = 4;
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){
2023-11-29 16:21:20 +01:00
this.dir = 0;
}else{
this.dir = 3;
}
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){
if(this.keysDown.has('q') || this.keysDown.has('arrowLeft')){
2023-11-29 16:21:20 +01:00
this.dir = 6;
}else{
this.dir = 5;
}
2023-12-06 08:58:18 +01:00
}else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){
2023-11-29 16:21:20 +01:00
this.dir = 7;
}
2023-11-29 18:01:11 +01:00
if(oldDir!=this.dir)
{
2023-11-29 18:36:28 +01:00
this.player.changeDirection(this.dir);
this.network.update(this.player);
2023-11-29 18:01:11 +01:00
}
2023-11-29 16:21:20 +01:00
}
2023-12-08 13:50:49 +01:00
/*
2023-11-15 09:09:13 +01:00
calculateAngle(playerX, playerY) {
2023-12-06 19:49:59 +01:00
return Math.atan2(this.mouseY - playerY, this.mouseX - playerX);
2023-11-15 09:09:13 +01:00
}
2023-12-08 13:50:49 +01:00
*/
2023-11-15 09:09:13 +01:00
}