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;
// 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);

View File

@ -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
});
}
}
}