From 684108a405436822a093d08c528defb4f1ef91f0 Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Fri, 24 Apr 2020 12:30:54 +0200 Subject: [PATCH] Now sending player data over socket.io This is faster then controls but requires it a lot of updates --- src/entities/Tank.js | 5 +---- src/screens/game.js | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/entities/Tank.js b/src/entities/Tank.js index 99210a7..b54efbd 100644 --- a/src/entities/Tank.js +++ b/src/entities/Tank.js @@ -47,7 +47,7 @@ class Tank extends TileSpriteXML { this.barrel.rotation = atan + (0.5 * Math.PI) - this.rotation; // Set rotation back to zero if higher than 2 * Pi (same angle) - // TODO: Fix rotation, the barrel glitches a bit on 0 and 180 degrees + // TODO: Fix rotation, the barrel glitches a bit on 0 and 180 degreessa if (this.rotation >= (2 * Math.PI) || this.rotation <= -(2 * Math.PI)) { this.rotation = 0; } @@ -67,9 +67,6 @@ class Tank extends TileSpriteXML { // TODO: Make it easier to see what end of the tank is forward // Move - if (this.mouse.isDown) { - console.log(this.rotation); - } if (this.keys.y == 1) { this.pos.x += this.speed.move * Math.cos(this.rotation + 0.5 * Math.PI); this.pos.y += this.speed.move * Math.sin(this.rotation + 0.5 * Math.PI); diff --git a/src/screens/game.js b/src/screens/game.js index bb7fbbd..b8f7ff7 100644 --- a/src/screens/game.js +++ b/src/screens/game.js @@ -74,10 +74,11 @@ class GameScreen extends Container { // Handle player updates this.socket.on("update", (data) => { - var controls = JSON.parse(data.controls); - Object.assign(this.players[data.uuid].mouse, controls.mouse); - Object.assign(this.players[data.uuid].keys, controls.keys); - console.log(this.players[data.uuid]); + // TODO: Fix barrel rotation + const player = this.players[data.uuid]; + player.rotation = data.player.rotation.body; + player.barrel.rotation = data.player.rotation.barrel; + player.pos = data.player.pos; }); // TODO: Lock player untill game starts @@ -90,12 +91,16 @@ class GameScreen extends Container { update(dt, t) { super.update(dt, t); - - // Only send when controls are updated and minify data sent + // TODO: Only send when controls are updated and minify data sent if (JSON.stringify(this.controls) != this.controlsBuffer) { console.log("Sending updates"); - this.socket.emit("update", JSON.stringify(this.controls)); - this.controlsBuffer = JSON.stringify(this.controls); + this.socket.emit("update", { + rotation: { + body: this.player.rotation, + barrel: this.player.barrel.rotation + }, + pos: this.player.pos + }); } } }