reduce update packet size by ~70%
This commit is contained in:
parent
1af386d9f5
commit
0906441246
20 changed files with 219 additions and 143 deletions
|
@ -76,11 +76,14 @@
|
|||
"Salal Berry",
|
||||
"Salak",
|
||||
"Satsuma",
|
||||
"Soursop",
|
||||
"Star Fruit",
|
||||
"Strawberry",
|
||||
"Tamarillo",
|
||||
"Tamarind",
|
||||
"Ugli Fruit"
|
||||
"Ugli Fruit",
|
||||
"Yuzu",
|
||||
"Ziziphus"
|
||||
],
|
||||
"adjectives": {
|
||||
"a": [
|
||||
|
@ -159,7 +162,8 @@
|
|||
"Perfect",
|
||||
"Pitiful",
|
||||
"Paranoid",
|
||||
"Pink"
|
||||
"Pink",
|
||||
"Porous"
|
||||
],
|
||||
"q": [
|
||||
"Quivering",
|
||||
|
@ -172,7 +176,8 @@
|
|||
"s": [
|
||||
"Stupid",
|
||||
"Silly",
|
||||
"Smart"
|
||||
"Smart",
|
||||
"Slimy"
|
||||
],
|
||||
"t": [
|
||||
"Terrific",
|
||||
|
@ -195,10 +200,12 @@
|
|||
"Xenophobic"
|
||||
],
|
||||
"y": [
|
||||
"Yellow"
|
||||
"Yellow",
|
||||
"Useful"
|
||||
],
|
||||
"z": [
|
||||
"Zany"
|
||||
"Zany",
|
||||
"Zealous"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ class Player {
|
|||
this.lastAction = Date.now();
|
||||
this.connection = connection;
|
||||
this.name = this.randomName();
|
||||
this.delta = {};
|
||||
this.delta = [];
|
||||
|
||||
this.chatCooldown = 0;
|
||||
}
|
||||
|
@ -20,6 +20,10 @@ class Player {
|
|||
this.room.remove(this);
|
||||
}
|
||||
|
||||
applyDelta(data) {
|
||||
this.delta = this.delta.concat(data);
|
||||
}
|
||||
|
||||
updateInputs(data) {
|
||||
this.ship.updateInputs(data);
|
||||
this.lastAction = Date.now();
|
||||
|
@ -49,9 +53,9 @@ class Player {
|
|||
}
|
||||
|
||||
sendUpdate() {
|
||||
if (Object.keys(this.delta).length == 0) return;
|
||||
if (this.delta.length == 0) return;
|
||||
this.connection.send('update', this.delta);
|
||||
this.delta = {};
|
||||
this.delta = [];
|
||||
}
|
||||
|
||||
tick() {
|
||||
|
|
|
@ -13,6 +13,14 @@ class Room {
|
|||
this.teamB = new Set();
|
||||
this.world = new World(this);
|
||||
this.name = (Math.random() * 100000 | 0).toString(36);
|
||||
this.tps = 60;
|
||||
|
||||
this.idGenerator = (function*() {
|
||||
let i = 0;
|
||||
|
||||
while (true)
|
||||
yield i++;
|
||||
})();
|
||||
|
||||
this.gameServer = gameServer;
|
||||
this.io = this.gameServer.net.io;
|
||||
|
@ -46,6 +54,10 @@ class Room {
|
|||
this.message('roomLeave', player.name, 'team' + player.team);
|
||||
}
|
||||
|
||||
generateId() {
|
||||
return this.idGenerator.next().value;
|
||||
}
|
||||
|
||||
setTeam(player, team) {
|
||||
this.teamA.delete(player);
|
||||
this.teamB.delete(player);
|
||||
|
@ -58,9 +70,9 @@ class Room {
|
|||
player.connection.drop();
|
||||
}
|
||||
|
||||
update(self) {
|
||||
//if (this.world.tickCount % 100 == 0)
|
||||
self.players.forEach(player => {
|
||||
update() {
|
||||
this.world.tick();
|
||||
this.players.forEach(player => {
|
||||
player.sendUpdate();
|
||||
if (Date.now() - player.lastAction > 10000) {
|
||||
//this.kick(player);
|
||||
|
@ -75,7 +87,7 @@ class Room {
|
|||
|
||||
chat(player, message) {
|
||||
wingbase.log(`${this.name}/${player.name}: ${message}`);
|
||||
|
||||
|
||||
this.chatCooldown++;
|
||||
this.io.to(this.name).emit('chat', {
|
||||
type: 'player',
|
||||
|
@ -104,6 +116,7 @@ class Room {
|
|||
let data = {
|
||||
playerShipId: player.ship.id,
|
||||
bounds: this.world.bounds,
|
||||
tps: this.tps,
|
||||
bodies: Array.from(this.world.bodies).map(b => b.packFull())
|
||||
};
|
||||
|
||||
|
@ -113,7 +126,8 @@ class Room {
|
|||
start() {
|
||||
this.world.populate();
|
||||
this.world.start();
|
||||
this.interval = setInterval(_ => this.update(this), 1 / 60);
|
||||
let wait = 1 / this.tps * 1000;
|
||||
this.interval = setInterval(this.update.bind(this), wait);
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
|
|
@ -4,17 +4,19 @@ const Body = require('./body.js');
|
|||
|
||||
class Asteroid extends Body {
|
||||
constructor(world, pos, size) {
|
||||
super(world);
|
||||
|
||||
this.x = pos.x;
|
||||
this.y = pos.y;
|
||||
super(world, pos);
|
||||
|
||||
this.debug = 0;
|
||||
|
||||
this.size = size;
|
||||
|
||||
this.type = 'asteroid';
|
||||
this.size = size;
|
||||
this.frame = this.randomFrame();
|
||||
|
||||
|
||||
this.interface.order.push.apply(this.interface.order, [
|
||||
'debug'
|
||||
]);
|
||||
this.interface.type = 'asteroid';
|
||||
}
|
||||
|
||||
randomFrame() {
|
||||
|
@ -34,13 +36,8 @@ class Asteroid extends Body {
|
|||
return [this.debug];
|
||||
}
|
||||
|
||||
packFull() {
|
||||
return {
|
||||
type: 'asteroid',
|
||||
id: this.id,
|
||||
frame: this.frame,
|
||||
delta: this.packDelta()
|
||||
}
|
||||
packTypeFull() {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,19 +2,37 @@
|
|||
|
||||
const uuid = require('uuid');
|
||||
|
||||
const Mount = require('./turret/mount.js');
|
||||
|
||||
const b2Vec2 = require('box2d-html5').b2Vec2;
|
||||
|
||||
class Body {
|
||||
constructor(world) {
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.r = 0;
|
||||
this.b2body = false;
|
||||
this.type = 'asteroid';
|
||||
this.mounts = [];
|
||||
this.health = 1;
|
||||
constructor(world, data) {
|
||||
data = data || {};
|
||||
this.world = world;
|
||||
this.id = uuid.v4().slice(0, 8);
|
||||
this.id = this.world.room.generateId();
|
||||
this.type = 'body';
|
||||
this.b2body = false;
|
||||
|
||||
this.mounts = data.mounts || [];
|
||||
this.health = data.health || 1;
|
||||
this.mounts = this.mounts.map(m => new Mount(this, m));
|
||||
|
||||
let fixtures = data.fixtures || [];
|
||||
this.fixtures = this.mounts.map((m, i) => fixtures[i] || 0);
|
||||
|
||||
this.interface = {
|
||||
order: [
|
||||
'x',
|
||||
'y',
|
||||
'xvel',
|
||||
'yvel',
|
||||
'r',
|
||||
'rvel'
|
||||
],
|
||||
type: 'body',
|
||||
fixtures: this.fixtures.length
|
||||
};
|
||||
}
|
||||
|
||||
destruct() {
|
||||
|
@ -23,7 +41,7 @@ class Body {
|
|||
}
|
||||
|
||||
applyDelta() {
|
||||
this.world.applyDelta(this.id, this.packDelta());
|
||||
this.world.applyDelta(this.packDelta());
|
||||
}
|
||||
|
||||
applyForce(x, y, center) {
|
||||
|
@ -74,8 +92,13 @@ class Body {
|
|||
let rot = this.b2body.GetAngleRadians();
|
||||
let rvel = this.b2body.GetAngularVelocity();
|
||||
|
||||
// Simple array to save bandwidth.
|
||||
return [pos.x, pos.y, vel.x, vel.y, rot, rvel].concat(this.packTypeDelta());
|
||||
let values = [this.id, pos.x, pos.y, vel.x, vel.y, rot, rvel];
|
||||
values = values.concat(this.packTypeDelta());
|
||||
this.mounts.forEach(m => {
|
||||
values = values.concat(m.packDelta());
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
packTypeDelta() {
|
||||
|
@ -83,11 +106,24 @@ class Body {
|
|||
}
|
||||
|
||||
packFull() {
|
||||
return {
|
||||
type: 'body',
|
||||
let packet = {
|
||||
type: this.type,
|
||||
id: this.id,
|
||||
delta: this.packDelta()
|
||||
frame: this.frame,
|
||||
fixtures: this.mounts.map(m => m.packFull()),
|
||||
delta: this.packDelta(),
|
||||
interface: this.interface
|
||||
}
|
||||
|
||||
let typePacket = this.packTypeFull();
|
||||
for (let i in typePacket)
|
||||
packet[i] = typePacket[i];
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
packTypeFull() {
|
||||
return {};
|
||||
}
|
||||
|
||||
get com() {
|
||||
|
|
|
@ -5,13 +5,9 @@ const Rope = require('../../copula/rope.js');
|
|||
|
||||
class Grapple extends Projectile {
|
||||
constructor(world, pos, source) {
|
||||
super(world);
|
||||
// pos.x *= 32, pos.y *= 32, idk why
|
||||
super(world, pos);
|
||||
|
||||
this.x = pos.x * 32;
|
||||
this.y = pos.y * 32;
|
||||
this.xvel = pos.xvel;
|
||||
this.yvel = pos.yvel;
|
||||
this.r = pos.r;
|
||||
this.xvel += Math.cos(this.r) * 0.25;
|
||||
this.yvel += Math.sin(this.r) * 0.25;
|
||||
|
||||
|
@ -64,14 +60,8 @@ class Grapple extends Projectile {
|
|||
return [];
|
||||
}
|
||||
|
||||
packFull() {
|
||||
return {
|
||||
type: 'grapple',
|
||||
id: this.id,
|
||||
source: this.source.id,
|
||||
frame: this.frame,
|
||||
delta: this.packDelta()
|
||||
};
|
||||
packProjectileFull() {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -51,14 +51,8 @@ class Missile extends Projectile {
|
|||
return [];
|
||||
}
|
||||
|
||||
packFull() {
|
||||
return {
|
||||
type: 'missile',
|
||||
id: this.id,
|
||||
source: this.source.id,
|
||||
frame: this.frame,
|
||||
delta: this.packDelta()
|
||||
};
|
||||
packProjectileFull() {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,21 @@ class Projectile extends Body {
|
|||
}
|
||||
|
||||
connect() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
packTypeDelta() {
|
||||
return [];
|
||||
}
|
||||
|
||||
packProjectileFull() {
|
||||
return {};
|
||||
}
|
||||
|
||||
packTypeFull() {
|
||||
let packet = this.packProjectileFull();
|
||||
packet.source = this.source.id;
|
||||
return packet;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,13 +4,12 @@ const defaults = require('../traits/defaults.json');
|
|||
const shipTraits = require('../traits/ships.json');
|
||||
|
||||
const Body = require('./body.js');
|
||||
const Mount = require('./turret/mount.js');
|
||||
|
||||
class Ship extends Body {
|
||||
constructor(world, pos, player, build) {
|
||||
super(world);
|
||||
|
||||
build = build || defaults.spawnShip;
|
||||
let traits = shipTraits[build.ship];
|
||||
super(world, traits, build);
|
||||
|
||||
// Body data.
|
||||
this.x = pos.x || 0;
|
||||
|
@ -23,19 +22,18 @@ class Ship extends Body {
|
|||
this.grapple = false;
|
||||
|
||||
// Traits.
|
||||
let traits = shipTraits[this.class];
|
||||
this.traits = traits;
|
||||
this.frame = traits.frame;
|
||||
this.power = traits.power;
|
||||
this.size = traits.size;
|
||||
|
||||
// Mounts
|
||||
traits.mounts.forEach((data, i) => {
|
||||
let mount = new Mount(this, data);
|
||||
this.mounts.push(mount);
|
||||
});
|
||||
|
||||
this.turrets = build.turrets || [];
|
||||
// Delta interface.
|
||||
this.interface.order.push.apply(this.interface.order, [
|
||||
'thrustForward',
|
||||
'thrustLeft',
|
||||
'thrustRight'
|
||||
]);
|
||||
this.interface.type = 'ship';
|
||||
|
||||
this.thrust = {
|
||||
forward: 0,
|
||||
|
@ -56,9 +54,9 @@ class Ship extends Body {
|
|||
release: data[7]
|
||||
};
|
||||
|
||||
this.thrust.forward = this.inputs.forward;
|
||||
this.thrust.left = this.inputs.left;
|
||||
this.thrust.right = this.inputs.right;
|
||||
this.thrust.forward = +this.inputs.forward;
|
||||
this.thrust.left = +this.inputs.left;
|
||||
this.thrust.right = +this.inputs.right;
|
||||
|
||||
if (this.inputs.missile) this.launchMissile();
|
||||
if (this.inputs.grapple) {
|
||||
|
@ -100,22 +98,23 @@ class Ship extends Body {
|
|||
|
||||
packTypeDelta() {
|
||||
let t = this.thrust;
|
||||
|
||||
return [t.forward, t.left, t.right, this.debug || false];
|
||||
return [t.forward, t.left, t.right];
|
||||
}
|
||||
|
||||
packFull() {
|
||||
getTypeDeltaInterface() {
|
||||
return [
|
||||
'thrustForward',
|
||||
'thrustLeft',
|
||||
'thrustRight'
|
||||
];
|
||||
}
|
||||
|
||||
packTypeFull() {
|
||||
return {
|
||||
type: 'ship',
|
||||
id: this.id,
|
||||
team: this.player.team,
|
||||
name: this.player.name,
|
||||
frame: this.frame,
|
||||
power: this.power,
|
||||
mounts: this.mounts.map(m => m.packFull()),
|
||||
turrets: this.turrets.map(t => t.packFull()),
|
||||
size: this.size,
|
||||
delta: this.packDelta()
|
||||
size: this.size
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,9 @@ class Mount {
|
|||
this.traversal = data.traversal ? {
|
||||
cw: data.bounds[0],
|
||||
ccw: data.bounds[1]
|
||||
} : false;
|
||||
} : 0;
|
||||
|
||||
this.updateDeltaInterface();
|
||||
}
|
||||
|
||||
destruct() {
|
||||
|
@ -25,11 +27,20 @@ class Mount {
|
|||
this.fixture.destruct();
|
||||
}
|
||||
|
||||
packDelta() {
|
||||
return [this.traversal || 0];
|
||||
}
|
||||
|
||||
updateDeltaInterface() {
|
||||
this.deltaInterface = this.fixture ? ['traversal'] : [];
|
||||
}
|
||||
|
||||
packFull() {
|
||||
return {
|
||||
x: this.position.x,
|
||||
y: this.position.y
|
||||
}
|
||||
y: this.position.y,
|
||||
turret: this.turret ? this.turret.type : 0
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,12 +11,12 @@ class World {
|
|||
this.physics = new Physics();
|
||||
this.spawner = new Spawner(this);
|
||||
this.bodies = new Set();
|
||||
this.copulae = new Set();
|
||||
this.structures = new Set();
|
||||
this.asteroids = new Set();
|
||||
this.copulae = new Set();
|
||||
this.players = new Set();
|
||||
this.projectiles = new Set();
|
||||
this.ships = new Map();
|
||||
this.players = new Set();
|
||||
this.structures = new Set();
|
||||
this.room = room;
|
||||
this.tps = 0;
|
||||
this.tpsCount = 0;
|
||||
|
@ -63,18 +63,18 @@ class World {
|
|||
}
|
||||
|
||||
addAsteroid(asteroid) {
|
||||
this.asteroids.add(asteroid);
|
||||
this.addBody(asteroid);
|
||||
}
|
||||
|
||||
addProjectile(projectile) {
|
||||
this.projectiles.add(projectile);
|
||||
this.addBody(projectile);
|
||||
projectile.connect();
|
||||
}
|
||||
|
||||
addBody(body) {
|
||||
this.bodies.add(body);
|
||||
if (body.type == 'asteroid') this.asteroids.add(body);
|
||||
if (body.type == 'structure') this.structures.add(body);
|
||||
this.physics.createBody(body);
|
||||
this.room.broadcast('create', body.packFull());
|
||||
}
|
||||
|
@ -85,8 +85,9 @@ class World {
|
|||
this.room.broadcast('effect', copula.packFull());
|
||||
}
|
||||
|
||||
applyDelta(body, data) {
|
||||
this.players.forEach(player => player.delta[body] = data);
|
||||
applyDelta(data) {
|
||||
data = data.map(v => +(v.toFixed(3)));
|
||||
this.players.forEach(player => player.applyDelta(data));
|
||||
}
|
||||
|
||||
explosion(pos, power) {
|
||||
|
@ -126,9 +127,9 @@ class World {
|
|||
removeBody(body) {
|
||||
body.destruct();
|
||||
this.bodies.delete(body);
|
||||
this.ships.delete(body);
|
||||
this.structures.delete(body);
|
||||
this.asteroids.delete(body);
|
||||
this.structures.delete(body);
|
||||
this.ships.delete(body);
|
||||
this.projectiles.delete(body);
|
||||
this.room.broadcast('destroy', body.id);
|
||||
}
|
||||
|
@ -143,15 +144,15 @@ class World {
|
|||
}
|
||||
|
||||
start() {
|
||||
this.interval = setInterval(_ => this.tick(this), 1000 / 60);
|
||||
|
||||
}
|
||||
|
||||
stop() {
|
||||
clearInterval(this.interval);
|
||||
|
||||
}
|
||||
|
||||
tick(self) {
|
||||
self.physics.step();
|
||||
tick() {
|
||||
this.physics.step();
|
||||
|
||||
let tickBodies = (set, interval) => {
|
||||
set.forEach(body => {
|
||||
|
@ -161,9 +162,9 @@ class World {
|
|||
});
|
||||
};
|
||||
|
||||
tickBodies(self.ships, 1);
|
||||
tickBodies(self.asteroids, 4);
|
||||
tickBodies(self.projectiles, 1);
|
||||
tickBodies(this.ships, 1);
|
||||
tickBodies(this.asteroids, 4);
|
||||
tickBodies(this.projectiles, 1);
|
||||
|
||||
if (Date.now() - this.tpsStart > 5000) {
|
||||
this.tps = this.tpsCount / 5 | 0;
|
||||
|
|
|
@ -30,7 +30,6 @@ class Spawner {
|
|||
yvel: ship.vel.y
|
||||
};
|
||||
let missile = new Missile(this.world, pos, ship);
|
||||
this.world.addProjectile(missile);
|
||||
return missile;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue