reduce update packets sent

This commit is contained in:
Asraelite 2016-03-30 12:50:59 +01:00
parent d6daed2e9b
commit ef2d067b38
15 changed files with 67 additions and 14 deletions

View file

@ -17,6 +17,7 @@ class Net {
});
this.socket.on('update', data => {
window.q = data;
game.world.update(data);
});

View file

@ -6,10 +6,10 @@ class Asteroid extends Body {
}
updateType(data) {
this.debug = data.debug;
//this.debug = data.debug;
}
tick() {
tickType() {
}
}

View file

@ -46,6 +46,7 @@ class Body {
this.r = values.r;
this.rvel = values.rvel;
this.updated = 10;
this.debug = values.debug;
this.updateType(values);
}
@ -55,6 +56,10 @@ class Body {
}
tick() {
this.tickType();
}
tickType() {
}

View file

@ -2,6 +2,9 @@
class Physics {
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.toRemove = [];
@ -31,7 +34,7 @@ class Physics {
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.06;
bodyDef.type = body.bodyType == 'structure' ?
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
bodyDef.allowSleep = false;
if (body.bodyType != 'asteroid') bodyDef.allowSleep = false;
var b2body = this.world.CreateBody(bodyDef);
var fixtureDef = new b2FixtureDef();

View file

@ -24,7 +24,7 @@ class Ship extends Body {
this.debug = data.debug;
}
tick() {
tickType() {
if (this.thrust.forward) {
var power = this.power.forward;
var x = Math.cos(this.getPos().r) * power;

View file

@ -41,6 +41,8 @@ class Body {
type: 'body',
fixtures: this.mounts.length
};
this.sleepTime = 0;
}
destruct() {
@ -88,6 +90,8 @@ class Body {
if(pos.y < bounds.top) this.applyForce(0, 0.03);
if(pos.y > bounds.bottom) this.applyForce(-0, -0.03);
this.sleepTime++;
this.tickType();
}
@ -135,6 +139,15 @@ class Body {
return {};
}
get awake() {
if (this.b2body.IsAwake()) {
this.sleepTime = 0;
return true;
} else {
return this.sleepTime < 50;
}
}
get com() {
return {
x: this.b2body.GetLocalCenter().x,

View file

@ -57,7 +57,8 @@ class Ship extends Body {
this.thrust.left = packet.thrust[1];
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) {
this.grapple.release();
} else if (packet.fire[0] && !this.grapple) {

View file

@ -4,8 +4,8 @@ const Fixture = require('./fixture.js');
const Laser = require('./shot/laser.js');
class Blaster extends Fixture {
constructor(hardpoint, data) {
super(hardpoint, data);
constructor(mount, data) {
super(mount, data);
}
fire() {

View file

@ -1,6 +1,6 @@
'use strict';
const traits = require('../../traits/turrets.json');
const traits = require('../../traits/fixtures.json');
class Fixture {
constructor(mount, data) {
@ -10,6 +10,8 @@ class Fixture {
let turretTraits = traits[data.type];
console.log(turretTraits);
this.rof = turretTraits.rateOfFire;
this.traversal = this.mount.traversal || false;

View file

@ -7,7 +7,7 @@ class Mount {
this.ship = ship;
this.type = data.type || 'turret';
this.fixture = fixture || false;
this.fixture = fixture || false//new Fixture(fixture);
this.size = data.size || 0;
this.position = {
x: data.pos[0],
@ -27,6 +27,10 @@ class Mount {
//this.fixture.destruct();
}
fire() {
console.log(this.fixture);
}
packDelta() {
return [this.traversal || 0];
}

View file

@ -1,9 +1,13 @@
'use strict';
class Shot {
constructor() {
constructor(pos) {
}
tick() {
}
}
module.exports = Shot;

View file

@ -162,16 +162,16 @@ class World {
tick() {
this.physics.step();
let tickBodies = (set, interval) => {
let tickBodies = (set, interval, canSleep) => {
set.forEach(body => {
if (this.tickCount % interval == 0)
if (this.tickCount % interval == 0 && body.awake)
body.applyDelta();
body.tick();
});
};
tickBodies(this.ships, 1);
tickBodies(this.asteroids, 4);
tickBodies(this.asteroids, 1);
tickBodies(this.projectiles, 1);
if (Date.now() - this.tpsStart > 5000) {

View file

@ -18,6 +18,9 @@ const b2Vec2 = Box2D.b2Vec2;
class Physics {
constructor() {
Box2D.b2_linearSleepTolerance = 0.002;
Box2D.b2_angularSleepTolerance = 0.002;
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
this.toRemove = [];
this.toWeld = [];

View file

@ -3,6 +3,7 @@
const Asteroid = require('./body/asteroid.js');
const Grapple = require('./body/projectile/grapple.js');
const Missile = require('./body/projectile/missile.js');
const Laser = require('./body/turret/shot/laser.js');
class Spawner {
constructor(world) {
@ -51,6 +52,22 @@ class Spawner {
this.world.addProjectile(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;