2023-11-17 19:25:26 +01:00
|
|
|
class Network{
|
2023-12-08 13:50:49 +01:00
|
|
|
constructor(adress){
|
2023-11-17 19:25:26 +01:00
|
|
|
this.adress = adress;
|
|
|
|
this.connected = false;
|
2023-11-29 16:20:47 +01:00
|
|
|
this.playerId = null;
|
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-12-08 13:50:49 +01:00
|
|
|
for (let i = 0; i<data.data.players.length; i++) {
|
|
|
|
let p = data.data.players[i];
|
|
|
|
if(p.id==this.playerId)
|
2023-12-12 13:53:55 +01:00
|
|
|
player=new Player(p.id,p.x,p.y,0,p.name,p.dir);
|
2023-11-29 17:25:40 +01:00
|
|
|
else
|
2023-12-12 13:53:55 +01:00
|
|
|
players.push(new Player(p.id,p.x,p.y,0,p.name,p.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-12-08 13:50:49 +01:00
|
|
|
updatePlayer(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-12-12 13:53:55 +01:00
|
|
|
players.push(new Player(data.data.id, data.data.x, data.data.y, 0, 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":
|
2023-12-08 13:50:49 +01:00
|
|
|
removePlayer(data.data.id);
|
2023-11-29 18:00:22 +01:00
|
|
|
break;
|
2023-12-08 13:50:49 +01:00
|
|
|
|
2023-12-06 09:50:34 +01:00
|
|
|
case "newBullet":
|
2023-12-12 13:53:55 +01:00
|
|
|
bullets.push(new Bullet(data.data.x,data.data.y,data.z,data.data.dx,data.data.dy,data.data.id, this.sound));
|
2023-12-06 09:50:34 +01:00
|
|
|
break;
|
2023-12-08 13:50:49 +01:00
|
|
|
|
2023-12-07 09:07:06 +01:00
|
|
|
case "died":
|
2023-12-07 14:06:11 +01:00
|
|
|
console.log("player",data.data.id,"was killed by",data.data.killerId);
|
2023-12-08 13:50:49 +01:00
|
|
|
addKill(data.data.id,data.data.killerId);
|
2023-12-07 14:06:11 +01:00
|
|
|
break;
|
2023-12-08 13:50:49 +01:00
|
|
|
|
2023-12-02 17:40:38 +01:00
|
|
|
default:
|
2023-12-08 13:50:49 +01:00
|
|
|
console.log("received unknown data:",data);
|
2023-12-02 17:40:38 +01:00
|
|
|
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-12 13:53:55 +01:00
|
|
|
newBullet(x,y,z,dx,dy,parentId)
|
2023-12-06 09:50:34 +01:00
|
|
|
{
|
2023-12-12 13:53:55 +01:00
|
|
|
this.socket.send(JSON.stringify({type:"newBullet",data:{x: x,y: y,z: z,dx: dx,dy: dy,id:parentId}}));
|
2023-11-19 19:56:29 +01:00
|
|
|
}
|
2023-11-17 19:25:26 +01:00
|
|
|
}
|