From ef2d067b38462cc69561cb0d25fc66d9964ba7e9 Mon Sep 17 00:00:00 2001 From: Asraelite Date: Wed, 30 Mar 2016 12:50:59 +0100 Subject: [PATCH] reduce update packets sent --- public/static/js/wingbase/net.js | 1 + public/static/js/wingbase/world/asteroid.js | 6 +++--- public/static/js/wingbase/world/body.js | 5 +++++ public/static/js/wingbase/world/physics.js | 5 ++++- public/static/js/wingbase/world/ship.js | 2 +- server/game/room/world/body/body.js | 13 +++++++++++++ server/game/room/world/body/ship.js | 3 ++- server/game/room/world/body/turret/blaster.js | 4 ++-- server/game/room/world/body/turret/fixture.js | 4 +++- server/game/room/world/body/turret/mount.js | 6 +++++- server/game/room/world/body/turret/shot/shot.js | 6 +++++- server/game/room/world/index.js | 6 +++--- server/game/room/world/physics.js | 3 +++ server/game/room/world/spawner.js | 17 +++++++++++++++++ .../traits/{turrets.json => fixtures.json} | 0 15 files changed, 67 insertions(+), 14 deletions(-) rename server/game/room/world/traits/{turrets.json => fixtures.json} (100%) diff --git a/public/static/js/wingbase/net.js b/public/static/js/wingbase/net.js index 1b512a6..ed26652 100644 --- a/public/static/js/wingbase/net.js +++ b/public/static/js/wingbase/net.js @@ -17,6 +17,7 @@ class Net { }); this.socket.on('update', data => { + window.q = data; game.world.update(data); }); diff --git a/public/static/js/wingbase/world/asteroid.js b/public/static/js/wingbase/world/asteroid.js index f8a47fa..b2286d4 100644 --- a/public/static/js/wingbase/world/asteroid.js +++ b/public/static/js/wingbase/world/asteroid.js @@ -6,10 +6,10 @@ class Asteroid extends Body { } updateType(data) { - this.debug = data.debug; + //this.debug = data.debug; } - tick() { - + tickType() { + } } diff --git a/public/static/js/wingbase/world/body.js b/public/static/js/wingbase/world/body.js index ece3d02..032b0d1 100644 --- a/public/static/js/wingbase/world/body.js +++ b/public/static/js/wingbase/world/body.js @@ -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() { } diff --git a/public/static/js/wingbase/world/physics.js b/public/static/js/wingbase/world/physics.js index 01928d2..f739abb 100644 --- a/public/static/js/wingbase/world/physics.js +++ b/public/static/js/wingbase/world/physics.js @@ -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(); diff --git a/public/static/js/wingbase/world/ship.js b/public/static/js/wingbase/world/ship.js index 18418d5..d4ee414 100644 --- a/public/static/js/wingbase/world/ship.js +++ b/public/static/js/wingbase/world/ship.js @@ -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; diff --git a/server/game/room/world/body/body.js b/server/game/room/world/body/body.js index 3c56139..5a4ca1a 100644 --- a/server/game/room/world/body/body.js +++ b/server/game/room/world/body/body.js @@ -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, diff --git a/server/game/room/world/body/ship.js b/server/game/room/world/body/ship.js index f65d12a..a6a1c9d 100644 --- a/server/game/room/world/body/ship.js +++ b/server/game/room/world/body/ship.js @@ -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) { diff --git a/server/game/room/world/body/turret/blaster.js b/server/game/room/world/body/turret/blaster.js index 925daef..816a190 100644 --- a/server/game/room/world/body/turret/blaster.js +++ b/server/game/room/world/body/turret/blaster.js @@ -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() { diff --git a/server/game/room/world/body/turret/fixture.js b/server/game/room/world/body/turret/fixture.js index 1c13a37..7cd1c4d 100644 --- a/server/game/room/world/body/turret/fixture.js +++ b/server/game/room/world/body/turret/fixture.js @@ -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; diff --git a/server/game/room/world/body/turret/mount.js b/server/game/room/world/body/turret/mount.js index 7cab48b..d7d4fdc 100644 --- a/server/game/room/world/body/turret/mount.js +++ b/server/game/room/world/body/turret/mount.js @@ -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]; } diff --git a/server/game/room/world/body/turret/shot/shot.js b/server/game/room/world/body/turret/shot/shot.js index 6af3373..04f2d74 100644 --- a/server/game/room/world/body/turret/shot/shot.js +++ b/server/game/room/world/body/turret/shot/shot.js @@ -1,9 +1,13 @@ 'use strict'; class Shot { - constructor() { + constructor(pos) { } + + tick() { + + } } module.exports = Shot; diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index 9850e69..7157129 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -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) { diff --git a/server/game/room/world/physics.js b/server/game/room/world/physics.js index d782002..ec0b209 100644 --- a/server/game/room/world/physics.js +++ b/server/game/room/world/physics.js @@ -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 = []; diff --git a/server/game/room/world/spawner.js b/server/game/room/world/spawner.js index 731db72..1b1911f 100644 --- a/server/game/room/world/spawner.js +++ b/server/game/room/world/spawner.js @@ -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; diff --git a/server/game/room/world/traits/turrets.json b/server/game/room/world/traits/fixtures.json similarity index 100% rename from server/game/room/world/traits/turrets.json rename to server/game/room/world/traits/fixtures.json