From 10a8111565aca1a2ee68a77d64d087935542eba8 Mon Sep 17 00:00:00 2001 From: Atsuyo-INSA Date: Wed, 29 Nov 2023 17:25:40 +0100 Subject: [PATCH] Client is now linked succesfully --- js/class.js | 3 +-- js/game.js | 30 +++++++++++++++++++++++++----- js/keyboard.js | 14 ++++++++++++++ js/network.js | 6 +++++- 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/js/class.js b/js/class.js index b9e0adc..8e3b846 100644 --- a/js/class.js +++ b/js/class.js @@ -52,7 +52,6 @@ class Player if(this.dir!=0){ this.visibleDir = newDirection; } - Net.update(this); } update(squares,circles)//update position @@ -189,6 +188,6 @@ class Circle collide(point) { - return ((point.x-this.x)**2+(point.y-this.y)**2<=r**2); + return ((point.x-this.x)**2+(point.y-this.y)**2<=this.r**2); } } \ No newline at end of file diff --git a/js/game.js b/js/game.js index 8362430..54b5a84 100644 --- a/js/game.js +++ b/js/game.js @@ -3,21 +3,41 @@ let Net = new Network("ws://129.151.227.50:8080/", Renderer); //let ClientKeyboard = new Keyboard() -let playerId; //id of client player +let playerId = null; //id of client player let players = []; +let player = null; Inp = new Input("canvas"); Net.connect(); //connect to server, create a player, and retrieve all players info //ClientKeyboard.init(); +playerId=Net.playerId; +player=Net.clientPlayer; +players=Net.getPlayersToAdd(); + + function game() { - for (var i = players.length - 1; i >= 0; i--) { - players[i].update([],[]); + if(playerId==null) + { + if (Net.playerId!=null) + { + playerId=Net.playerId; + player=Net.clientPlayer; + players=Net.getPlayersToAdd(); + //Inp.player=player; //pour connecter les input au joueur client + console.log("Connected as id ",playerId); + } + } + else + { + for (var i = players.length - 1; i >= 0; i--) { + players[i].update([],[]);//squares, circles + } + //console.log(Inp.calculateAngle(500,500)) + Renderer.ReloadAff(); } - //console.log(Inp.calculateAngle(500,500)) - Renderer.ReloadAff(); } setInterval(game); \ No newline at end of file diff --git a/js/keyboard.js b/js/keyboard.js index d033e4b..5a3d8d8 100644 --- a/js/keyboard.js +++ b/js/keyboard.js @@ -1,3 +1,17 @@ +///////////// +///////////// +///////////// +// DEAD CLASS !!! +// DO NOT USE + + + + + + + + + class Keyboard{ constructor(){ this.keysDown = new Set() diff --git a/js/network.js b/js/network.js index 620295a..15bcead 100644 --- a/js/network.js +++ b/js/network.js @@ -4,6 +4,7 @@ class Network{ this.connected = false; this.playerId = null; + this.clientPlayer=null; this.playersToAdd = []; this.playersToRemove = []; this.playersToUpdate = []; @@ -14,7 +15,10 @@ class Network{ case 'connect': this.playerId = data.data.playerId; for (var i = data.data.players.length - 1; i >= 0; i--) { - 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)) + if(data.data.players[i].id==this.playerId) + this.clientPlayer=data.data.players[i]; + 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)) } break; case 'update':