Got multiplayer semi working
Sending controls over socket.io might not be the way to go There is some lag causing the players to not be lined up on all clients
This commit is contained in:
parent
80f1cff91d
commit
499f960ce7
9
launch.json
Normal file
9
launch.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "Launch via npm",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"runtimeExecutable": "npm",
|
||||||
|
"runtimeArgs": ["run-script", "start"],
|
||||||
|
"port": 9229
|
||||||
|
}
|
6
package-lock.json
generated
6
package-lock.json
generated
@ -377,9 +377,9 @@
|
|||||||
"integrity": "sha512-wGUIVQXuehL5TCqQun8OW81jGzAWycqzFF8lFp+GOM5BXLYj3bKNsYC4daB7n6XjCqxQA/qgTJ+8ANR3acjrog=="
|
"integrity": "sha512-wGUIVQXuehL5TCqQun8OW81jGzAWycqzFF8lFp+GOM5BXLYj3bKNsYC4daB7n6XjCqxQA/qgTJ+8ANR3acjrog=="
|
||||||
},
|
},
|
||||||
"asdf-games": {
|
"asdf-games": {
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.1.1.tgz",
|
||||||
"integrity": "sha512-GK+rS2vlQmiZG3fadAy+PRtVzTNPqkcr9RoihobQahe765odqLluZpMKEtmUk4y6uN9rDPBRVk3e0X2SjsA8Yw=="
|
"integrity": "sha512-gw9TNJlwd6WQSIEMnQ6UhLbEjevmXOONC5fi6CiwIxNRmgaMsO4WaGHxxGZMv2tQgadhV80rf/VL2x7d9ZFQhw=="
|
||||||
},
|
},
|
||||||
"astral-regex": {
|
"astral-regex": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
},
|
},
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"asdf-games": "^1.1.0",
|
"asdf-games": "^1.1.1",
|
||||||
"socket.io": "^2.3.0"
|
"socket.io": "^2.3.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -5,8 +5,8 @@ const { Container, TileSpriteXML, math } = asdf;
|
|||||||
const container = new Container();
|
const container = new Container();
|
||||||
|
|
||||||
class Body extends TileSpriteXML {
|
class Body extends TileSpriteXML {
|
||||||
constructor(keyboard, mouse, tanksTexture, tanksXml) {
|
constructor(keyboard, mouse, tanksTexture, tanksXml, colour) {
|
||||||
super(tanksTexture, tanksXml, tanksXml.findIndex("name", "tankBody_blue.png"));
|
super(tanksTexture, tanksXml, tanksXml.findIndex("name", `tankBody_${colour.toLowerCase()}.png`));
|
||||||
this.mouse = mouse;
|
this.mouse = mouse;
|
||||||
this.keyboard = keyboard;
|
this.keyboard = keyboard;
|
||||||
this.pos = { x: 250, y: 100 };
|
this.pos = { x: 250, y: 100 };
|
||||||
@ -51,8 +51,8 @@ class Body extends TileSpriteXML {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Barrel extends TileSpriteXML {
|
class Barrel extends TileSpriteXML {
|
||||||
constructor(body, mouse, tanksTexture, tanksXml) {
|
constructor(body, mouse, tanksTexture, tanksXml, colour) {
|
||||||
super(tanksTexture, tanksXml, tanksXml.findIndex("name", "tankBlue_barrel2_outline.png"));
|
super(tanksTexture, tanksXml, tanksXml.findIndex("name", `tank${colour}_barrel2_outline.png`));
|
||||||
this.mouse = mouse;
|
this.mouse = mouse;
|
||||||
this.body = body;
|
this.body = body;
|
||||||
this.pos = { x: 0, y: 0 };
|
this.pos = { x: 0, y: 0 };
|
||||||
@ -82,13 +82,14 @@ class Barrel extends TileSpriteXML {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Tank extends Container {
|
class Tank extends Container {
|
||||||
constructor(keyboard, mouse, texture, xml) {
|
constructor(keys, mouse, texture, xml, colour) {
|
||||||
super(container);
|
super(container);
|
||||||
var body = new Body(keyboard, mouse, texture, xml);
|
this.keys = keys;
|
||||||
var barrel = new Barrel(body, mouse, texture, xml);
|
this.mouse = mouse;
|
||||||
|
var body = new Body(this.keys, this.mouse, texture, xml, colour);
|
||||||
|
var barrel = new Barrel(body, this.mouse, texture, xml, colour);
|
||||||
this.add(body);
|
this.add(body);
|
||||||
this.add(barrel);
|
this.add(barrel);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ var io = require("socket.io-client");
|
|||||||
|
|
||||||
const asdf = require("asdf-games");
|
const asdf = require("asdf-games");
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
const { Container, Camera, Texture, SpriteSheetXML, TileMapXML } = asdf;
|
const { Container, Camera, Texture, SpriteSheetXML, TileMapXML, KeyControls, MouseControls } = asdf;
|
||||||
|
|
||||||
const texture = new Texture("../res/images/allSprites_default.png");
|
const texture = new Texture("../res/images/allSprites_default.png");
|
||||||
const xml = new SpriteSheetXML("../res/images/allSprites_default.xml");
|
const xml = new SpriteSheetXML("../res/images/allSprites_default.xml");
|
||||||
@ -12,7 +12,6 @@ const Tank = require("../entities/Tank.js");
|
|||||||
class GameScreen extends Container {
|
class GameScreen extends Container {
|
||||||
constructor(game, controls, onGameOver) {
|
constructor(game, controls, onGameOver) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.scene = this;
|
this.scene = this;
|
||||||
this.controls = controls;
|
this.controls = controls;
|
||||||
@ -20,6 +19,7 @@ class GameScreen extends Container {
|
|||||||
|
|
||||||
this.controls.keys.reset();
|
this.controls.keys.reset();
|
||||||
|
|
||||||
|
// Draw level
|
||||||
const level = [
|
const level = [
|
||||||
161, 160, 160, 161, 160, 159, 134, 135, 143, 134, 135, 135,
|
161, 160, 160, 161, 160, 159, 134, 135, 143, 134, 135, 135,
|
||||||
161, 160, 160, 161, 161, 159, 135, 134, 143, 135, 135, 134,
|
161, 160, 160, 161, 161, 159, 135, 134, 143, 135, 135, 134,
|
||||||
@ -33,27 +33,57 @@ class GameScreen extends Container {
|
|||||||
|
|
||||||
this.add(new TileMapXML(level, Math.ceil(this.game.w / 64), Math.ceil(this.game.h / 64), texture, xml));
|
this.add(new TileMapXML(level, Math.ceil(this.game.w / 64), Math.ceil(this.game.h / 64), texture, xml));
|
||||||
|
|
||||||
this.player = new Tank(this.controls.keys, this.controls.mouse, texture, xml);
|
// Connect to TanksJS-Server instance
|
||||||
this.add(this.player);
|
|
||||||
|
|
||||||
this.socket = io("http://localhost:3000");
|
this.socket = io("http://localhost:3000");
|
||||||
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}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.players = [];
|
// Define tank colours
|
||||||
|
const colours = ["Blue", "Red", "Green", "Yellow"];
|
||||||
|
|
||||||
|
this.players = {};
|
||||||
|
|
||||||
|
// Define local player
|
||||||
|
this.player = new Tank(this.controls.keys, this.controls.mouse, texture, xml, colours[Object.keys(this.players).length]);
|
||||||
|
this.player.uuid = this.socket.id;
|
||||||
|
console.log(this.player);
|
||||||
|
this.add(this.player);
|
||||||
|
|
||||||
|
// Store current state of controls for comparing
|
||||||
this.controlsBuffer = JSON.stringify(this.controls);
|
this.controlsBuffer = JSON.stringify(this.controls);
|
||||||
|
|
||||||
// Handle player logins
|
// Handle player logins
|
||||||
this.socket.on("roomUpdate", (data) => {
|
this.socket.on("roomUpdate", (data) => {
|
||||||
console.log(data);
|
// Add any player that is not the local player and is not already added
|
||||||
|
for (const id in data) {
|
||||||
|
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.add(this.players[id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(this.players);
|
||||||
|
// TODO: Remove players who left
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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: Lock player untill game starts
|
||||||
|
this.socket.on("gameStart", () => {
|
||||||
|
this.player.locked = false;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -61,24 +91,12 @@ class GameScreen extends Container {
|
|||||||
update(dt, t) {
|
update(dt, t) {
|
||||||
super.update(dt, t);
|
super.update(dt, t);
|
||||||
|
|
||||||
// TODO: Lock player untill game starts
|
|
||||||
this.socket.on("gameStart", () => {
|
|
||||||
this.player.locked = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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", JSON.stringify(this.controls));
|
||||||
this.controlsBuffer = JSON.stringify(this.controls);
|
this.controlsBuffer = JSON.stringify(this.controls);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Handle player updates
|
|
||||||
this.socket.on("update", (data) => {
|
|
||||||
console.log(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user