improve multiplayer
This commit is contained in:
parent
0aa259b874
commit
2fffe574c1
10 changed files with 76 additions and 19 deletions
|
@ -25,12 +25,16 @@ class Player {
|
|||
});
|
||||
}
|
||||
|
||||
send(msg, data) {
|
||||
this.connection.send(msg, data);
|
||||
}
|
||||
|
||||
sendWorld(data) {
|
||||
this.connection.send('world', data);
|
||||
}
|
||||
|
||||
sendUpdate() {
|
||||
if (Object.keys(this.delta).length == 0) return;
|
||||
if (Object.keys(this.delta).length == 0 || Math.random() < 0) return;
|
||||
this.connection.send('update', this.delta);
|
||||
this.delta = {};
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ class Room {
|
|||
this.players = new Set();
|
||||
this.teamA = new Set();
|
||||
this.teamB = new Set();
|
||||
this.world = new World();
|
||||
this.world = new World(this);
|
||||
this.name = (Math.random() * 100000 | 0).toString(36);
|
||||
|
||||
this.start();
|
||||
|
@ -42,6 +42,10 @@ class Room {
|
|||
self.players.forEach(player => player.sendUpdate());
|
||||
}
|
||||
|
||||
broadcast(msg, data) {
|
||||
this.players.forEach(player => player.send(msg, data));
|
||||
}
|
||||
|
||||
sendWorld(player) {
|
||||
let data = {
|
||||
playerShipId: player.ship.id,
|
||||
|
|
|
@ -5,13 +5,14 @@ const Physics = require('./physics.js');
|
|||
const Ship = require('./ship.js');
|
||||
|
||||
class World {
|
||||
constructor() {
|
||||
constructor(room) {
|
||||
this.physics = new Physics();
|
||||
this.bodies = new Set();
|
||||
this.structures = new Set();
|
||||
this.asteroids = new Set();
|
||||
this.ships = new Map();
|
||||
this.players = new Set();
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
addPlayer(player) {
|
||||
|
@ -27,14 +28,18 @@ class World {
|
|||
|
||||
addShip(ship) {
|
||||
this.ships.set(ship.player, ship);
|
||||
this.bodies.add(ship);
|
||||
this.physics.createBody(ship);
|
||||
this.addBody(ship);
|
||||
}
|
||||
|
||||
addAsteroid(asteroid) {
|
||||
this.asteroids.add(asteroid);
|
||||
this.bodies.add(asteroid);
|
||||
this.physics.createBody(asteroid);
|
||||
this.addBody(asteroid);
|
||||
}
|
||||
|
||||
addBody(body) {
|
||||
this.bodies.add(body);
|
||||
this.physics.createBody(body);
|
||||
this.room.broadcast('create', body.packFull());
|
||||
}
|
||||
|
||||
applyDelta(body, data) {
|
||||
|
|
|
@ -47,6 +47,12 @@ class Ship extends Body {
|
|||
if (data.right) {
|
||||
this.applyTorque(this.power.rotation);
|
||||
}
|
||||
|
||||
this.thrust = {
|
||||
forward: data.forward,
|
||||
left: data.left,
|
||||
right: data.right
|
||||
};
|
||||
}
|
||||
|
||||
packTypeDelta() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue