ntework update

This commit is contained in:
Marty Killian 2023-11-29 16:12:00 +01:00
parent 1df5bda344
commit 4aba4d8f4d

View file

@ -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;
}
}