add basic updating to client on physics
This commit is contained in:
parent
95e0f6b710
commit
1027d980c1
15 changed files with 160 additions and 13 deletions
|
@ -12,10 +12,13 @@ class GameServer {
|
|||
|
||||
assignRoom(player) {
|
||||
let room = Array.from(this.rooms.values()).sort((a, b) => {
|
||||
return a.players - b.players;
|
||||
return a.playerCount - b.playerCount;
|
||||
})[0];
|
||||
|
||||
if (!room || room.full) room = new Room();
|
||||
if (!room || room.full) {
|
||||
room = new Room();
|
||||
this.rooms.set(room.name, room);
|
||||
}
|
||||
|
||||
room.add(player);
|
||||
}
|
||||
|
|
|
@ -36,6 +36,14 @@ class Connection {
|
|||
});
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.player.disconnect();
|
||||
}
|
||||
|
||||
send(msg, data) {
|
||||
this.io.to(this.socket.id).emit(msg, data);
|
||||
}
|
||||
|
||||
tick() {
|
||||
this.chatCooldown -= 1;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ class GameNet {
|
|||
cons.set(id, new Connection(this, socket));
|
||||
|
||||
socket.on('disconnect', _ => {
|
||||
cons.get(id).disconnect();
|
||||
cons.delete(id);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -9,7 +9,18 @@ class Player {
|
|||
this.team = false;
|
||||
this.kickCount = 0;
|
||||
this.connection = connection;
|
||||
this.name = `Stupid $(fruit[Math.random() * fruit.length | 0])`;
|
||||
this.name = `Stupid ${fruit[Math.random() * fruit.length | 0]}`;
|
||||
this.delta = {};
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
this.room.remove(this);
|
||||
}
|
||||
|
||||
sendUpdate() {
|
||||
if (Object.keys(this.delta).length == 0) return;
|
||||
this.connection.send('update', this.delta);
|
||||
this.delta = {};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,9 +9,12 @@ class Room {
|
|||
this.teamB = new Set();
|
||||
this.world = new World();
|
||||
this.name = (Math.random() * 100000 | 0).toString(36);
|
||||
|
||||
this.start();
|
||||
}
|
||||
|
||||
add(player) {
|
||||
console.log(`${player.name} joined ${this.name}.`);
|
||||
player.room = this;
|
||||
player.connection.room = this.name;
|
||||
this.players.add(player);
|
||||
|
@ -19,6 +22,14 @@ class Room {
|
|||
this.world.addPlayer(player);
|
||||
}
|
||||
|
||||
remove(player) {
|
||||
console.log(`${player.name} left ${this.name}.`);
|
||||
this.players.delete(player);
|
||||
this.teamA.delete(player);
|
||||
this.teamB.delete(player);
|
||||
this.world.removePlayer(player);
|
||||
}
|
||||
|
||||
setTeam(player, team) {
|
||||
this.teamA.delete(player);
|
||||
this.teamB.delete(player);
|
||||
|
@ -26,6 +37,20 @@ class Room {
|
|||
player.team = team;
|
||||
}
|
||||
|
||||
update(self) {
|
||||
self.players.forEach(player => player.sendUpdate());
|
||||
}
|
||||
|
||||
start() {
|
||||
this.world.start();
|
||||
this.interval = setInterval(_ => this.update(this), 1 / 60);
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.world.stop();
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
get playerCount() {
|
||||
return this.players.size;
|
||||
}
|
||||
|
|
|
@ -1,10 +1,28 @@
|
|||
'use strict';
|
||||
|
||||
const uuid = require('uuid');
|
||||
|
||||
class Body {
|
||||
constructor() {
|
||||
constructor(world) {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.r = 0;
|
||||
this.b2body = false;
|
||||
this.type = 'dynamic';
|
||||
this.health = 1;
|
||||
this.world = world;
|
||||
this.id = uuid.v4().slice(0, 8);
|
||||
}
|
||||
|
||||
applyDelta() {
|
||||
this.world.applyDelta(this.id, this.pack());
|
||||
}
|
||||
|
||||
pack() {
|
||||
let pos = this.b2body.GetPosition();
|
||||
let rot = this.b2body.GetAngleRadians();
|
||||
|
||||
return [pos.x, pos.y, rot];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,23 +15,45 @@ class World {
|
|||
|
||||
addPlayer(player) {
|
||||
this.players.add(player);
|
||||
let ship = new Ship(player);
|
||||
let ship = new Ship(this, player);
|
||||
player.ship = ship;
|
||||
this.ships.set(player, ship);
|
||||
this.bodies.add(ship);
|
||||
this.physics.createBody(ship);
|
||||
}
|
||||
|
||||
applyDelta(body, data) {
|
||||
this.players.forEach(player => player.delta[body] = data);
|
||||
}
|
||||
|
||||
removePlayer(player) {
|
||||
removeBody(player.ship);
|
||||
this.removeBody(player.ship);
|
||||
this.ships.delete(player);
|
||||
this.players.delete(player);
|
||||
}
|
||||
|
||||
removeBody(body) {
|
||||
// Remove from Box2d.
|
||||
this.physics.toRemove.push(body);
|
||||
this.bodies.delete(body);
|
||||
this.ships.delete(body);
|
||||
this.structures.delete(body);
|
||||
this.asteroids.delete(body);
|
||||
}
|
||||
|
||||
tick() {
|
||||
start() {
|
||||
this.interval = setInterval(_ => this.tick(this), 1 / 60);
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
tick(self) {
|
||||
self.physics.step();
|
||||
|
||||
if (Math.random() < 0.001) {
|
||||
self.bodies.forEach(body => body.applyDelta());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ const b2Vec2 = Box2D.b2Vec2;
|
|||
class Physics {
|
||||
constructor() {
|
||||
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
|
||||
this.toRemove = [];
|
||||
}
|
||||
|
||||
createBody(body) {
|
||||
|
@ -19,7 +20,7 @@ class Physics {
|
|||
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
|
||||
bodyDef.fixedRotation = false;
|
||||
bodyDef.active = true;
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel || 0, body.yvel || 0);
|
||||
bodyDef.linearVelocity = new b2Vec2(body.xvel || 0.05, body.yvel || 0);
|
||||
bodyDef.angularVelocity = body.rvel || 0;
|
||||
bodyDef.type = body.type == 'static' ?
|
||||
Box2D.b2Body.b2_staticBody : Box2D.b2Body.b2_dynamicBody;
|
||||
|
@ -31,7 +32,7 @@ class Physics {
|
|||
fixtureDef.restitution = 0;
|
||||
|
||||
for (var poly of body.structure) {
|
||||
poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
|
||||
poly = poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
|
||||
fixtureDef.shape = new Box2D.b2PolygonShape();
|
||||
fixtureDef.shape.SetAsArray(poly, poly.length);
|
||||
b2body.CreateFixture(fixtureDef);
|
||||
|
@ -42,6 +43,13 @@ class Physics {
|
|||
|
||||
step() {
|
||||
this.world.Step(1, 5, 1 / 60);
|
||||
|
||||
for (var i = 0; i < this.toRemove.length; i++) {
|
||||
this.world.DestroyBody(this.toRemove[i].b2body);
|
||||
console.log(this.world.GetBodyList());
|
||||
}
|
||||
|
||||
this.toRemove = [];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,8 +6,8 @@ const hulls = require('./traits/hulls.json');
|
|||
const Body = require('./body.js');
|
||||
|
||||
class Ship extends Body {
|
||||
constructor(player, build) {
|
||||
super();
|
||||
constructor(world, player, build) {
|
||||
super(world);
|
||||
|
||||
this.build = build || defaults.spawnShip.build;
|
||||
this.player = player;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue