From 4aba4d8f4d77e6144a15a407deb08cbc7979a554 Mon Sep 17 00:00:00 2001 From: Marty Killian Date: Wed, 29 Nov 2023 16:12:00 +0100 Subject: [PATCH] ntework update --- js/network.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/js/network.js b/js/network.js index 4551b12..19e256e 100644 --- a/js/network.js +++ b/js/network.js @@ -1,31 +1,27 @@ class Network{ - constructor(adress, Renderer){ + constructor(adress){ this.adress = adress; this.connected = false; - this.Renderer = Renderer + + this.playerId; + this.playersToAdd = []; + this.playersToRemove = []; + this.playersToUpdate = []; } message(data){ switch(data.type){ case 'connect': - playerId = data.data.playerId; + this.playerId = data.data.playerId; for (var i = data.data.players.length - 1; i >= 0; i--) { - players.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)) - - this.Renderer.AddPlayer(players[players.length - 1]) + 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': - for (var i = players.length - 1; i >= 0; i--) { - if(players[i].id==data.data.id){ - players[i].retrieveServerInfo(data.data.id, data.data.x, data.data.y, data.data.dir); - break; - } - } + this.playersToUpdate.push(data.data); break; case "newplayer": - players.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir)); - this.Renderer.AddPlayer(players[players.length - 1]) + this.playersToAdd.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir)); break; } @@ -48,4 +44,22 @@ class Network{ data: obj })) } + + getPlayersToAdd(){ + let tmp = this.playersToAdd; + this.playersToAdd = []; + return tmp; + } + + getPlayersToRemove(){ + let tmp = this.playersToRemove; + this.playersToRemove = []; + return tmp; + } + + getPlayersToUpdate(){ + let tmp = this.playersToUpdate; + this.playersToUpdate = []; + return tmp; + } } \ No newline at end of file