Limited the amount of data being sent
This commit is contained in:
parent
684108a405
commit
23c43fde6f
@ -3,10 +3,11 @@ var asdf = require("asdf-games");
|
|||||||
const { Container, TileSpriteXML, math } = asdf;
|
const { Container, TileSpriteXML, math } = asdf;
|
||||||
|
|
||||||
class Tank extends TileSpriteXML {
|
class Tank extends TileSpriteXML {
|
||||||
constructor(keys, mouse, texture, xml, colour) {
|
constructor(keys, mouse, texture, xml, colour, remote = false) {
|
||||||
super(texture, xml, xml.findIndex("name", `tankBody_${colour.toLowerCase()}.png`));
|
super(texture, xml, xml.findIndex("name", `tankBody_${colour.toLowerCase()}.png`));
|
||||||
this.keys = keys;
|
this.keys = keys;
|
||||||
this.mouse = mouse;
|
this.mouse = mouse;
|
||||||
|
this.remote = remote;
|
||||||
this.children = [];
|
this.children = [];
|
||||||
|
|
||||||
// Define body
|
// Define body
|
||||||
@ -17,7 +18,6 @@ class Tank extends TileSpriteXML {
|
|||||||
};
|
};
|
||||||
this.rotation = 0;
|
this.rotation = 0;
|
||||||
|
|
||||||
// TODO: Do not allow movement when this.locked = true
|
|
||||||
this.locked = false;
|
this.locked = false;
|
||||||
|
|
||||||
// Define barrel
|
// Define barrel
|
||||||
@ -33,6 +33,7 @@ class Tank extends TileSpriteXML {
|
|||||||
update(dt, t) {
|
update(dt, t) {
|
||||||
super.update(dt,t);
|
super.update(dt,t);
|
||||||
|
|
||||||
|
if (!this.remote && this.locked == false) {
|
||||||
// Make this.barrel follow the mouse
|
// Make this.barrel follow the mouse
|
||||||
var dx = (this.pos.x + this.barrel.pos.x) - this.mouse.pos.x;
|
var dx = (this.pos.x + this.barrel.pos.x) - this.mouse.pos.x;
|
||||||
var dy = (this.pos.y + this.barrel.pos.y) - this.mouse.pos.y;
|
var dy = (this.pos.y + this.barrel.pos.y) - this.mouse.pos.y;
|
||||||
@ -76,6 +77,7 @@ class Tank extends TileSpriteXML {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Tank;
|
module.exports = Tank;
|
@ -1,3 +1,4 @@
|
|||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
const { ipcRenderer } = require("electron");
|
const { ipcRenderer } = require("electron");
|
||||||
|
|
||||||
var asdf = require("asdf-games");
|
var asdf = require("asdf-games");
|
||||||
@ -16,7 +17,9 @@ const
|
|||||||
const window = { w: 740, h: 480 };
|
const window = { w: 740, h: 480 };
|
||||||
|
|
||||||
var game = new Game(window.w, window.h, true);
|
var game = new Game(window.w, window.h, true);
|
||||||
ipcRenderer.send("resize", window);
|
|
||||||
|
// TODO: Enable resize on release
|
||||||
|
// ipcRenderer.send("resize", window);
|
||||||
|
|
||||||
const controls = {
|
const controls = {
|
||||||
keys: new KeyControls(),
|
keys: new KeyControls(),
|
||||||
|
@ -38,8 +38,7 @@ class GameScreen extends Container {
|
|||||||
this.socket.on("identify", () => {
|
this.socket.on("identify", () => {
|
||||||
this.socket.emit("identification", {
|
this.socket.emit("identification", {
|
||||||
name: "Arn",
|
name: "Arn",
|
||||||
gamemode: 2,
|
gamemode: 2
|
||||||
controls: JSON.stringify(this.controls)
|
|
||||||
});
|
});
|
||||||
console.log(`Connected to server as ${this.socket.id}`);
|
console.log(`Connected to server as ${this.socket.id}`);
|
||||||
});
|
});
|
||||||
@ -52,33 +51,37 @@ class GameScreen extends Container {
|
|||||||
// Define local player
|
// Define local player
|
||||||
this.player = new Tank(this.controls.keys, this.controls.mouse, texture, xml, colours[Object.keys(this.players).length]);
|
this.player = new Tank(this.controls.keys, this.controls.mouse, texture, xml, colours[Object.keys(this.players).length]);
|
||||||
this.player.uuid = this.socket.id;
|
this.player.uuid = this.socket.id;
|
||||||
console.log(this.player);
|
|
||||||
this.add(this.player);
|
this.add(this.player);
|
||||||
|
|
||||||
// Store current state of controls for comparing
|
// Store current state of controls for comparing
|
||||||
this.controlsBuffer = JSON.stringify(this.controls);
|
this.dataBuffer = JSON.stringify({
|
||||||
|
pos: this.player.pos,
|
||||||
|
rotation: {
|
||||||
|
body: this.player.rotation,
|
||||||
|
barrel: this.player.children[0].rotation
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Handle player logins
|
// Handle player logins
|
||||||
this.socket.on("roomUpdate", (data) => {
|
this.socket.on("roomUpdate", (data) => {
|
||||||
// Add any player that is not the local player and is not already added
|
// Add any player that is not the local player and is not already added
|
||||||
for (const id in data) {
|
for (const id in data) {
|
||||||
if (id != this.socket.id && !(this.players[id])) {
|
if (id != this.socket.id && !(this.players[id])) {
|
||||||
this.players[id] = new Tank(new KeyControls(false), new MouseControls(false), texture, xml, colours[Object.keys(this.players).length]);
|
this.players[id] = new Tank(new KeyControls(false), new MouseControls(false), texture, xml, colours[Object.keys(this.players).length], true);
|
||||||
this.add(this.players[id]);
|
this.add(this.players[id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(this.players);
|
console.log(this.players);
|
||||||
// TODO: Remove players who left
|
// TODO: Remove players who left
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle player updates
|
// Handle player updates
|
||||||
this.socket.on("update", (data) => {
|
this.socket.on("update", (data) => {
|
||||||
// TODO: Fix barrel rotation
|
// TODO: Fix crashing related caused by this function
|
||||||
const player = this.players[data.uuid];
|
const player = this.players[data.uuid];
|
||||||
|
player.pos = data.player.pos;
|
||||||
player.rotation = data.player.rotation.body;
|
player.rotation = data.player.rotation.body;
|
||||||
player.barrel.rotation = data.player.rotation.barrel;
|
player.barrel.rotation = data.player.rotation.barrel;
|
||||||
player.pos = data.player.pos;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Lock player untill game starts
|
// TODO: Lock player untill game starts
|
||||||
@ -91,16 +94,19 @@ 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
|
|
||||||
if (JSON.stringify(this.controls) != this.controlsBuffer) {
|
// Only send when controls are updated and minify data sent
|
||||||
console.log("Sending updates");
|
var data = {
|
||||||
this.socket.emit("update", {
|
pos: this.player.pos,
|
||||||
rotation: {
|
rotation: {
|
||||||
body: this.player.rotation,
|
body: this.player.rotation,
|
||||||
barrel: this.player.barrel.rotation
|
barrel: this.player.children[0].rotation
|
||||||
},
|
}
|
||||||
pos: this.player.pos
|
};
|
||||||
});
|
if (JSON.stringify(data) != this.dataBuffer) {
|
||||||
|
console.log("Sending updates");
|
||||||
|
this.socket.emit("update", data);
|
||||||
|
this.dataBuffer = JSON.stringify(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user