From 44595f0af776ae2551ed66e20c1203e79d86b6ba Mon Sep 17 00:00:00 2001 From: Killian Marty Date: Sat, 2 Dec 2023 17:40:38 +0100 Subject: [PATCH] comments --- js/class.js | 1 - js/network.js | 27 ++++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/js/class.js b/js/class.js index e3937a9..1b0abd7 100644 --- a/js/class.js +++ b/js/class.js @@ -52,7 +52,6 @@ class Player if(this.dir!=0){ this.visibleDir = newDirection; } - //console.log("Should send: {type='update', dir=",this.dir,", x=",this.x,", y=",this.y,"}"); } update(squares,circles, dt)//update position diff --git a/js/network.js b/js/network.js index ddb6ff7..153395d 100644 --- a/js/network.js +++ b/js/network.js @@ -21,56 +21,53 @@ class Network{ this.playersToAdd.push(new Player(data.data.players[i].id, data.data.players[i].x, data.data.players[i].y, data.data.players[i].name, data.data.players[i].dir)) } break; + case 'update': this.playersToUpdate.push(data.data); break; + case "newplayer": this.playersToAdd.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir)); break; + case "removePlayer": this.playersToRemove.push(data.data.id); break; + default: + break; } } - connect(){ + connect(){ //create the WebSocket, initialize it and connect to the server this.socket = new WebSocket(this.adress); - this.socket.addEventListener('open', (e)=>{ - //connected to server - this.connected = true; + this.socket.addEventListener('open', (e)=>{ + this.connected = true; //connected to server }); this.socket.addEventListener('message', (e)=>{ this.message(JSON.parse(e.data)); }) } - getPlayerId(){ - while(this.playerId==null){ - //waiting for connection - }; - return this.playerId; - } - - update(obj){ + update(obj){ //send data to server in order to broadcast this.socket.send(JSON.stringify({ type: "update", data: obj })) } - getPlayersToAdd(){ + getPlayersToAdd(){ //returns the list of new players let tmp = this.playersToAdd; this.playersToAdd = []; return tmp; } - getPlayersToRemove(){ + getPlayersToRemove(){ //returns the list of player who have left the game let tmp = this.playersToRemove; this.playersToRemove = []; return tmp; } - getPlayersToUpdate(){ + getPlayersToUpdate(){ //return a list of all updates recieved from the server let tmp = this.playersToUpdate; this.playersToUpdate = []; return tmp;