cleaning code

This commit is contained in:
Killian Marty 2023-12-06 19:49:59 +01:00
parent 7b7da9b4eb
commit 01dccf5a20
3 changed files with 13 additions and 34 deletions

View file

@ -50,9 +50,6 @@ function update()
function addPlayers()
{
let playersToAdd = Net.getPlayersToAdd();
if (playersToAdd.length==0)
return;
console.log(playersToAdd);
playersToAdd.forEach((p) => {
console.log("New player: ",p.id);
players.push(p);
@ -63,8 +60,6 @@ function addPlayers()
function remPlayers()
{
let playerToRemove = Net.getPlayersToRemove();
if(playerToRemove.length==0)
return;
for(let i=0;i<playerToRemove.length;i++)
{
let id = playerToRemove[i];
@ -84,8 +79,6 @@ function remPlayers()
function addBullets()
{
let bulletsToAdd = Net.getBulletsToAdd();
if (bulletsToAdd.length==0)
return;
bulletsToAdd.forEach((b) => {
bullets.push(b);
Renderer.addBullet(b);
@ -115,11 +108,12 @@ function game() {
playerId=Net.playerId;
player=Net.clientPlayer;
players=Net.getPlayersToAdd();
//Inp.player=player; //pour connecter les input au joueur client
Renderer.AddPlayer(player)
console.log("Connected as id ",playerId);
Inp.player=player;
Inp.bullets=bullets;
Renderer.AddPlayer(player)
players.forEach((p) => {
Renderer.AddPlayer(p)
});

View file

@ -17,34 +17,19 @@ class Input {
this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this));
this.canvas.addEventListener("click", (e) => {
if(this.player==null || this.bullets==null)
if(this.player==null || this.bullets==null){
return;
}
let dx = this.mouseX-this.player.x;
let dy = this.mouseY-this.player.y;
let norm = Math.sqrt(dx*dx+dy*dy);
let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm,this.player.id);
this.bullets.push(b);
this.renderer.addBullet(b);
this.network.newBullet(b.x,b.y,b.dx,b.dy,b.parentId);
});
/*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();
@ -102,11 +87,11 @@ class Input {
handleMouseMove(event) {
let mX = event.clientX - this.canvas.getBoundingClientRect().left;
let mY = event.clientY - this.canvas.getBoundingClientRect().top;
this.mouseX = mX
this.mouseY = mY
this.mouseX = mX;
this.mouseY = mY;
}
calculateAngle(playerX, playerY) {
return Math.atan2(this.mouseY - playerY, this.mouseX - playerX) //Math.atan2(
return Math.atan2(this.mouseY - playerY, this.mouseX - playerX);
}
}

View file

@ -52,8 +52,8 @@ class Network{
})
}
die(id_){
this.socket.send(JSON.stringify({type:"died",data:{id:id_}}));
die(id){
this.socket.send(JSON.stringify({type:"died",data:{id: id}}));
}
update(obj){ //send data to server in order to broadcast
@ -63,9 +63,9 @@ class Network{
}));
}
newBullet(x_,y_,dx_,dy_,parentId)
newBullet(x,y,dx,dy,parentId)
{
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x_,y: y_,dx: dx_,dy: dy_,id:parentId}}));
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x,y: y,dx: dx,dy: dy,id:parentId}}));
}
getPlayersToAdd(){ //returns the list of new players