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

View file

@ -17,34 +17,19 @@ class Input {
this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this)); this.canvas.addEventListener("mousemove", this.handleMouseMove.bind(this));
this.canvas.addEventListener("click", (e) => { this.canvas.addEventListener("click", (e) => {
if(this.player==null || this.bullets==null) if(this.player==null || this.bullets==null){
return; return;
}
let dx = this.mouseX-this.player.x; let dx = this.mouseX-this.player.x;
let dy = this.mouseY-this.player.y; let dy = this.mouseY-this.player.y;
let norm = Math.sqrt(dx*dx+dy*dy); 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); let b = new Bullet(this.player.x,this.player.y,dx/norm,dy/norm,this.player.id);
this.bullets.push(b); this.bullets.push(b);
this.renderer.addBullet(b); this.renderer.addBullet(b);
this.network.newBullet(b.x,b.y,b.dx,b.dy,b.parentId); 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)=>{ window.addEventListener("keydown", (e)=>{
this.keysDown.add(e.key.toLowerCase()) this.keysDown.add(e.key.toLowerCase())
this.updateDir(); this.updateDir();
@ -102,11 +87,11 @@ class Input {
handleMouseMove(event) { handleMouseMove(event) {
let mX = event.clientX - this.canvas.getBoundingClientRect().left; let mX = event.clientX - this.canvas.getBoundingClientRect().left;
let mY = event.clientY - this.canvas.getBoundingClientRect().top; let mY = event.clientY - this.canvas.getBoundingClientRect().top;
this.mouseX = mX this.mouseX = mX;
this.mouseY = mY this.mouseY = mY;
} }
calculateAngle(playerX, playerY) { 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_){ die(id){
this.socket.send(JSON.stringify({type:"died",data:{id:id_}})); this.socket.send(JSON.stringify({type:"died",data:{id: id}}));
} }
update(obj){ //send data to server in order to broadcast 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 getPlayersToAdd(){ //returns the list of new players