2023-11-17 19:25:26 +01:00
|
|
|
class Network{
|
2023-11-29 16:12:00 +01:00
|
|
|
constructor(adress){
|
2023-11-17 19:25:26 +01:00
|
|
|
this.adress = adress;
|
|
|
|
this.connected = false;
|
2023-11-29 16:12:00 +01:00
|
|
|
|
2023-11-29 16:20:47 +01:00
|
|
|
this.playerId = null;
|
2023-11-29 17:25:40 +01:00
|
|
|
this.clientPlayer=null;
|
2023-11-29 16:12:00 +01:00
|
|
|
this.playersToAdd = [];
|
|
|
|
this.playersToRemove = [];
|
|
|
|
this.playersToUpdate = [];
|
2023-12-06 09:50:34 +01:00
|
|
|
this.bulletsToAdd = [];
|
2023-11-17 19:25:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
message(data){
|
|
|
|
switch(data.type){
|
|
|
|
case 'connect':
|
2023-11-29 16:12:00 +01:00
|
|
|
this.playerId = data.data.playerId;
|
2023-11-19 18:38:27 +01:00
|
|
|
for (var i = data.data.players.length - 1; i >= 0; i--) {
|
2023-11-29 17:25:40 +01:00
|
|
|
if(data.data.players[i].id==this.playerId)
|
2023-11-29 17:50:21 +01:00
|
|
|
this.clientPlayer=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);
|
2023-11-29 17:25:40 +01:00
|
|
|
else
|
|
|
|
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))
|
2023-11-18 13:38:29 +01:00
|
|
|
}
|
2023-11-17 19:25:26 +01:00
|
|
|
break;
|
2023-12-02 17:40:38 +01:00
|
|
|
|
2023-11-17 19:25:26 +01:00
|
|
|
case 'update':
|
2023-11-29 16:12:00 +01:00
|
|
|
this.playersToUpdate.push(data.data);
|
2023-11-18 13:38:29 +01:00
|
|
|
break;
|
2023-12-02 17:40:38 +01:00
|
|
|
|
2023-11-18 13:38:29 +01:00
|
|
|
case "newplayer":
|
2023-11-29 16:12:00 +01:00
|
|
|
this.playersToAdd.push(new Player(data.data.id, data.data.x, data.data.y, data.data.name, data.data.dir));
|
2023-11-17 19:25:26 +01:00
|
|
|
break;
|
2023-12-02 17:40:38 +01:00
|
|
|
|
2023-11-29 18:00:22 +01:00
|
|
|
case "removePlayer":
|
|
|
|
this.playersToRemove.push(data.data.id);
|
|
|
|
break;
|
2023-12-06 09:50:34 +01:00
|
|
|
case "newBullet":
|
2023-12-06 10:40:08 +01:00
|
|
|
this.bulletsToAdd.push(new Bullet(data.data.x,data.data.y,data.data.dx,data.data.dy,data.data.id));
|
2023-12-06 09:50:34 +01:00
|
|
|
break;
|
2023-12-07 09:07:06 +01:00
|
|
|
case "died":
|
|
|
|
console.log(data.data);
|
|
|
|
console.log("someone died");
|
2023-12-02 17:40:38 +01:00
|
|
|
default:
|
|
|
|
break;
|
2023-11-17 19:25:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
connect(){ //create the WebSocket, initialize it and connect to the server
|
2023-11-17 19:25:26 +01:00
|
|
|
this.socket = new WebSocket(this.adress);
|
2023-12-02 17:40:38 +01:00
|
|
|
this.socket.addEventListener('open', (e)=>{
|
|
|
|
this.connected = true; //connected to server
|
2023-11-17 19:25:26 +01:00
|
|
|
});
|
|
|
|
this.socket.addEventListener('message', (e)=>{
|
|
|
|
this.message(JSON.parse(e.data));
|
|
|
|
})
|
|
|
|
}
|
2023-11-19 19:56:29 +01:00
|
|
|
|
2023-12-07 09:21:49 +01:00
|
|
|
died(id, killerId){
|
|
|
|
this.socket.send(JSON.stringify({type:"died",data:{id: id, killerId: killerId}}));
|
2023-12-06 10:53:02 +01:00
|
|
|
}
|
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
update(obj){ //send data to server in order to broadcast
|
2023-11-19 19:56:29 +01:00
|
|
|
this.socket.send(JSON.stringify({
|
|
|
|
type: "update",
|
|
|
|
data: obj
|
2023-12-06 09:50:34 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-12-06 19:49:59 +01:00
|
|
|
newBullet(x,y,dx,dy,parentId)
|
2023-12-06 09:50:34 +01:00
|
|
|
{
|
2023-12-06 19:49:59 +01:00
|
|
|
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x,y: y,dx: dx,dy: dy,id:parentId}}));
|
2023-11-19 19:56:29 +01:00
|
|
|
}
|
2023-11-29 16:12:00 +01:00
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
getPlayersToAdd(){ //returns the list of new players
|
2023-11-29 16:12:00 +01:00
|
|
|
let tmp = this.playersToAdd;
|
|
|
|
this.playersToAdd = [];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-12-06 09:50:34 +01:00
|
|
|
getBulletsToAdd(){ //returns the list of new players
|
|
|
|
let tmp = this.bulletsToAdd;
|
|
|
|
this.bulletsToAdd = [];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
getPlayersToRemove(){ //returns the list of player who have left the game
|
2023-11-29 16:12:00 +01:00
|
|
|
let tmp = this.playersToRemove;
|
|
|
|
this.playersToRemove = [];
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
getPlayersToUpdate(){ //return a list of all updates recieved from the server
|
2023-11-29 16:12:00 +01:00
|
|
|
let tmp = this.playersToUpdate;
|
|
|
|
this.playersToUpdate = [];
|
|
|
|
return tmp;
|
|
|
|
}
|
2023-11-17 19:25:26 +01:00
|
|
|
}
|