reduce update packets sent
This commit is contained in:
parent
d6daed2e9b
commit
ef2d067b38
15 changed files with 67 additions and 14 deletions
|
@ -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,
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
'use strict';
|
||||
|
||||
class Shot {
|
||||
constructor() {
|
||||
constructor(pos) {
|
||||
|
||||
}
|
||||
|
||||
tick() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Shot;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 = [];
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue