reduce update packets sent
This commit is contained in:
parent
d6daed2e9b
commit
ef2d067b38
15 changed files with 67 additions and 14 deletions
|
@ -17,6 +17,7 @@ class Net {
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('update', data => {
|
this.socket.on('update', data => {
|
||||||
|
window.q = data;
|
||||||
game.world.update(data);
|
game.world.update(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@ class Asteroid extends Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
updateType(data) {
|
updateType(data) {
|
||||||
this.debug = data.debug;
|
//this.debug = data.debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tickType() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ class Body {
|
||||||
this.r = values.r;
|
this.r = values.r;
|
||||||
this.rvel = values.rvel;
|
this.rvel = values.rvel;
|
||||||
this.updated = 10;
|
this.updated = 10;
|
||||||
|
this.debug = values.debug;
|
||||||
|
|
||||||
this.updateType(values);
|
this.updateType(values);
|
||||||
}
|
}
|
||||||
|
@ -55,6 +56,10 @@ class Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
|
this.tickType();
|
||||||
|
}
|
||||||
|
|
||||||
|
tickType() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
class Physics {
|
class Physics {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
Box2D.Common.b2Settings.b2_linearSleepTolerance = 0.01;
|
||||||
|
Box2D.Common.b2Settings.b2_angularSleepTolerance = 0.01;
|
||||||
|
Box2D.Common.b2Settings.b2_timeToSleep = 0;
|
||||||
|
|
||||||
this.world = new b2World(new b2Vec2(0, 0));
|
this.world = new b2World(new b2Vec2(0, 0));
|
||||||
this.toRemove = [];
|
this.toRemove = [];
|
||||||
|
@ -31,7 +34,7 @@ class Physics {
|
||||||
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.06;
|
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.06;
|
||||||
bodyDef.type = body.bodyType == 'structure' ?
|
bodyDef.type = body.bodyType == 'structure' ?
|
||||||
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
|
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
|
||||||
bodyDef.allowSleep = false;
|
if (body.bodyType != 'asteroid') bodyDef.allowSleep = false;
|
||||||
var b2body = this.world.CreateBody(bodyDef);
|
var b2body = this.world.CreateBody(bodyDef);
|
||||||
|
|
||||||
var fixtureDef = new b2FixtureDef();
|
var fixtureDef = new b2FixtureDef();
|
||||||
|
|
|
@ -24,7 +24,7 @@ class Ship extends Body {
|
||||||
this.debug = data.debug;
|
this.debug = data.debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
tick() {
|
tickType() {
|
||||||
if (this.thrust.forward) {
|
if (this.thrust.forward) {
|
||||||
var power = this.power.forward;
|
var power = this.power.forward;
|
||||||
var x = Math.cos(this.getPos().r) * power;
|
var x = Math.cos(this.getPos().r) * power;
|
||||||
|
|
|
@ -41,6 +41,8 @@ class Body {
|
||||||
type: 'body',
|
type: 'body',
|
||||||
fixtures: this.mounts.length
|
fixtures: this.mounts.length
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.sleepTime = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
destruct() {
|
destruct() {
|
||||||
|
@ -88,6 +90,8 @@ class Body {
|
||||||
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
if(pos.y < bounds.top) this.applyForce(0, 0.03);
|
||||||
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
|
||||||
|
|
||||||
|
this.sleepTime++;
|
||||||
|
|
||||||
this.tickType();
|
this.tickType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,6 +139,15 @@ class Body {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get awake() {
|
||||||
|
if (this.b2body.IsAwake()) {
|
||||||
|
this.sleepTime = 0;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return this.sleepTime < 50;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get com() {
|
get com() {
|
||||||
return {
|
return {
|
||||||
x: this.b2body.GetLocalCenter().x,
|
x: this.b2body.GetLocalCenter().x,
|
||||||
|
|
|
@ -57,7 +57,8 @@ class Ship extends Body {
|
||||||
this.thrust.left = packet.thrust[1];
|
this.thrust.left = packet.thrust[1];
|
||||||
this.thrust.right = packet.thrust[2];
|
this.thrust.right = packet.thrust[2];
|
||||||
|
|
||||||
if (packet.fire[1]) this.launchMissile();
|
packet.fire.forEach((m, i) => m ? this.mounts[i].fire(m) : 0);
|
||||||
|
|
||||||
if (packet.fire[0] && this.grapple) {
|
if (packet.fire[0] && this.grapple) {
|
||||||
this.grapple.release();
|
this.grapple.release();
|
||||||
} else if (packet.fire[0] && !this.grapple) {
|
} else if (packet.fire[0] && !this.grapple) {
|
||||||
|
|
|
@ -4,8 +4,8 @@ const Fixture = require('./fixture.js');
|
||||||
const Laser = require('./shot/laser.js');
|
const Laser = require('./shot/laser.js');
|
||||||
|
|
||||||
class Blaster extends Fixture {
|
class Blaster extends Fixture {
|
||||||
constructor(hardpoint, data) {
|
constructor(mount, data) {
|
||||||
super(hardpoint, data);
|
super(mount, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fire() {
|
fire() {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const traits = require('../../traits/turrets.json');
|
const traits = require('../../traits/fixtures.json');
|
||||||
|
|
||||||
class Fixture {
|
class Fixture {
|
||||||
constructor(mount, data) {
|
constructor(mount, data) {
|
||||||
|
@ -10,6 +10,8 @@ class Fixture {
|
||||||
|
|
||||||
let turretTraits = traits[data.type];
|
let turretTraits = traits[data.type];
|
||||||
|
|
||||||
|
console.log(turretTraits);
|
||||||
|
|
||||||
this.rof = turretTraits.rateOfFire;
|
this.rof = turretTraits.rateOfFire;
|
||||||
|
|
||||||
this.traversal = this.mount.traversal || false;
|
this.traversal = this.mount.traversal || false;
|
||||||
|
|
|
@ -7,7 +7,7 @@ class Mount {
|
||||||
this.ship = ship;
|
this.ship = ship;
|
||||||
|
|
||||||
this.type = data.type || 'turret';
|
this.type = data.type || 'turret';
|
||||||
this.fixture = fixture || false;
|
this.fixture = fixture || false//new Fixture(fixture);
|
||||||
this.size = data.size || 0;
|
this.size = data.size || 0;
|
||||||
this.position = {
|
this.position = {
|
||||||
x: data.pos[0],
|
x: data.pos[0],
|
||||||
|
@ -27,6 +27,10 @@ class Mount {
|
||||||
//this.fixture.destruct();
|
//this.fixture.destruct();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fire() {
|
||||||
|
console.log(this.fixture);
|
||||||
|
}
|
||||||
|
|
||||||
packDelta() {
|
packDelta() {
|
||||||
return [this.traversal || 0];
|
return [this.traversal || 0];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
class Shot {
|
class Shot {
|
||||||
constructor() {
|
constructor(pos) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
tick() {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,16 +162,16 @@ class World {
|
||||||
tick() {
|
tick() {
|
||||||
this.physics.step();
|
this.physics.step();
|
||||||
|
|
||||||
let tickBodies = (set, interval) => {
|
let tickBodies = (set, interval, canSleep) => {
|
||||||
set.forEach(body => {
|
set.forEach(body => {
|
||||||
if (this.tickCount % interval == 0)
|
if (this.tickCount % interval == 0 && body.awake)
|
||||||
body.applyDelta();
|
body.applyDelta();
|
||||||
body.tick();
|
body.tick();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
tickBodies(this.ships, 1);
|
tickBodies(this.ships, 1);
|
||||||
tickBodies(this.asteroids, 4);
|
tickBodies(this.asteroids, 1);
|
||||||
tickBodies(this.projectiles, 1);
|
tickBodies(this.projectiles, 1);
|
||||||
|
|
||||||
if (Date.now() - this.tpsStart > 5000) {
|
if (Date.now() - this.tpsStart > 5000) {
|
||||||
|
|
|
@ -18,6 +18,9 @@ const b2Vec2 = Box2D.b2Vec2;
|
||||||
|
|
||||||
class Physics {
|
class Physics {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
Box2D.b2_linearSleepTolerance = 0.002;
|
||||||
|
Box2D.b2_angularSleepTolerance = 0.002;
|
||||||
|
|
||||||
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
|
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
|
||||||
this.toRemove = [];
|
this.toRemove = [];
|
||||||
this.toWeld = [];
|
this.toWeld = [];
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
const Asteroid = require('./body/asteroid.js');
|
const Asteroid = require('./body/asteroid.js');
|
||||||
const Grapple = require('./body/projectile/grapple.js');
|
const Grapple = require('./body/projectile/grapple.js');
|
||||||
const Missile = require('./body/projectile/missile.js');
|
const Missile = require('./body/projectile/missile.js');
|
||||||
|
const Laser = require('./body/turret/shot/laser.js');
|
||||||
|
|
||||||
class Spawner {
|
class Spawner {
|
||||||
constructor(world) {
|
constructor(world) {
|
||||||
|
@ -51,6 +52,22 @@ class Spawner {
|
||||||
this.world.addProjectile(grapple);
|
this.world.addProjectile(grapple);
|
||||||
return grapple;
|
return grapple;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
spawnLaser(ship) {
|
||||||
|
let r = ship.pos.r;
|
||||||
|
let ox = Math.cos(r) * 0.7;
|
||||||
|
let oy = Math.sin(r) * 0.7;
|
||||||
|
let pos = {
|
||||||
|
x: ship.center.x + ox,
|
||||||
|
y: ship.center.y + oy,
|
||||||
|
r: r,
|
||||||
|
xvel: ship.vel.x,
|
||||||
|
yvel: ship.vel.y
|
||||||
|
};
|
||||||
|
let missile = new Missile(this.world, pos, ship);
|
||||||
|
this.world.addProjectile(missile);
|
||||||
|
return missile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Spawner;
|
module.exports = Spawner;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue