GrandTabernacleAutoVI/public_html/js/input.js

101 lines
3.1 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
2023-12-09 13:19:04 +01:00
let bounds = this.canvas.getBoundingClientRect();
let mouseX = (e.clientX - bounds.x)*this.canvas.width/bounds.width;
let mouseY = (e.clientY - bounds.y)*this.canvas.height/bounds.height;
2023-12-08 13:50:49 +01:00
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-09 12:03:03 +01:00
2023-12-08 13:50:49 +01:00
let b = new Bullet(player.x,player.y,dx/norm,dy/norm,player.id);
bullets.push(b);
2023-12-09 10:58:00 +01:00
2023-12-08 18:29:33 +01:00
net.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)=>{
2023-12-09 20:46:58 +01:00
//blocks the action of the key (cf. Killian)
2023-12-11 18:49:13 +01:00
if(["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
2023-12-09 20:46:58 +01:00
e.preventDefault();
}
2023-11-29 16:21:20 +01:00
this.keysDown.add(e.key.toLowerCase())
this.updateDir();
})
window.addEventListener("keyup", (e)=>{
this.keysDown.delete(e.key.toLowerCase())
this.updateDir();
})
2023-12-11 18:49:13 +01:00
window.addEventListener("keypress", (e)=>{
if(e.key.toLowerCase()=="p"){
phone.changePosition();
}
})
2023-11-29 16:21:20 +01:00
}
updateDir(){
2023-12-08 18:29:33 +01:00
if(player==null)
2023-11-29 17:50:21 +01:00
return;
2023-12-09 12:03:03 +01:00
2023-11-29 18:01:11 +01:00
let oldDir=this.dir;
2023-12-09 12:03:03 +01:00
2023-11-29 18:01:11 +01:00
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-12-08 18:29:33 +01:00
player.changeDirection(this.dir);
2023-12-08 19:31:22 +01:00
net.update(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
}