Now sending player data over socket.io

This is faster then controls but requires it a lot of updates
This commit is contained in:
Arne van Iterson 2020-04-24 12:30:54 +02:00
parent 7fc8c6faa3
commit 684108a405
2 changed files with 14 additions and 12 deletions

View File

@ -47,7 +47,7 @@ class Tank extends TileSpriteXML {
this.barrel.rotation = atan + (0.5 * Math.PI) - this.rotation; this.barrel.rotation = atan + (0.5 * Math.PI) - this.rotation;
// Set rotation back to zero if higher than 2 * Pi (same angle) // 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)) { if (this.rotation >= (2 * Math.PI) || this.rotation <= -(2 * Math.PI)) {
this.rotation = 0; this.rotation = 0;
} }
@ -67,9 +67,6 @@ class Tank extends TileSpriteXML {
// TODO: Make it easier to see what end of the tank is forward // TODO: Make it easier to see what end of the tank is forward
// Move // Move
if (this.mouse.isDown) {
console.log(this.rotation);
}
if (this.keys.y == 1) { if (this.keys.y == 1) {
this.pos.x += this.speed.move * Math.cos(this.rotation + 0.5 * Math.PI); 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); this.pos.y += this.speed.move * Math.sin(this.rotation + 0.5 * Math.PI);

View File

@ -74,10 +74,11 @@ class GameScreen extends Container {
// Handle player updates // Handle player updates
this.socket.on("update", (data) => { this.socket.on("update", (data) => {
var controls = JSON.parse(data.controls); // TODO: Fix barrel rotation
Object.assign(this.players[data.uuid].mouse, controls.mouse); const player = this.players[data.uuid];
Object.assign(this.players[data.uuid].keys, controls.keys); player.rotation = data.player.rotation.body;
console.log(this.players[data.uuid]); player.barrel.rotation = data.player.rotation.barrel;
player.pos = data.player.pos;
}); });
// TODO: Lock player untill game starts // TODO: Lock player untill game starts
@ -90,12 +91,16 @@ class GameScreen extends Container {
update(dt, t) { update(dt, t) {
super.update(dt, t); super.update(dt, t);
// TODO: Only send when controls are updated and minify data sent
// Only send when controls are updated and minify data sent
if (JSON.stringify(this.controls) != this.controlsBuffer) { if (JSON.stringify(this.controls) != this.controlsBuffer) {
console.log("Sending updates"); console.log("Sending updates");
this.socket.emit("update", JSON.stringify(this.controls)); this.socket.emit("update", {
this.controlsBuffer = JSON.stringify(this.controls); rotation: {
body: this.player.rotation,
barrel: this.player.barrel.rotation
},
pos: this.player.pos
});
} }
} }
} }