diff --git a/package.json b/package.json index ae17244..445a30b 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "box2d-html5": "^0.1.230", "colors": "^1.1.2", "express": "^4.13.4", + "poly-decomp": "git://github.com/schteppe/poly-decomp.js.git", "recursive-readdir": "^1.3.0", "socket.io": "^1.4.5", "uglify-js": "^2.6.2", diff --git a/server/game/room/world/body.js b/server/game/room/world/body.js index 2e12490..530c2f0 100644 --- a/server/game/room/world/body.js +++ b/server/game/room/world/body.js @@ -22,13 +22,17 @@ class Body { applyForce(x, y, center) { let b = this.b2body; - b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter()); + let c = center ? new b2Vec2(center.x, center.y) : b.GetWorldCenter(); + b.ApplyForce(new b2Vec2(x, y), c); } applyTorque(f) { this.b2body.ApplyTorque(f); } + contact() { + } + tick() { let pos = this.b2body.GetPosition(); let bounds = this.world.bounds; @@ -74,6 +78,13 @@ class Body { r: this.b2body.GetAngleRadians() }; } + + get vel() { + return { + xvel: this.b2body.GetLinearVelocity().x, + yvel: this.b2body.GetLinearVelocity().y + } + } } module.exports = Body; diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index b947989..349491d 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -65,6 +65,19 @@ class World { this.players.forEach(player => player.delta[body] = data); } + explosion(pos, power) { + var rays = Array(50).fill().map((_, i) => { + let a = Math.PI * i / 25; + let x = pos.x + Math.cos(a) * Math.sqrt(power); + let y = pos.y + Math.sin(a) * Math.sqrt(power); + return this.physics.raycast(pos, { x : x, y: y }, (body, point, dis) => { + dis = Math.max(dis, 0.5); + let force = power * (1 / dis) * (1 / dis); + body.applyForce(x * force, y * force, point); + }); + }); + } + populate() { for (var i = 0; i < 40; i++) { let pos = { diff --git a/server/game/room/world/missile.js b/server/game/room/world/missile.js index be57bec..c0678c8 100644 --- a/server/game/room/world/missile.js +++ b/server/game/room/world/missile.js @@ -8,6 +8,8 @@ class Missile extends Body { this.x = pos.x * 32; this.y = pos.y * 32; + this.xvel = pos.xvel; + this.yvel = pos.yvel; this.r = pos.r; this.source = source; @@ -18,8 +20,12 @@ class Missile extends Body { this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]]; } + contact(body) { + this.detonate(); + } + detonate() { - // Blow up stuff. + this.world.explosion(this.pos, 10); this.world.removeBody(this); } @@ -28,7 +34,7 @@ class Missile extends Body { let x = Math.cos(this.b2body.GetAngleRadians()) * power; let y = Math.sin(this.b2body.GetAngleRadians()) * power; this.applyForce(x, y); - + if(this.fuel-- <= 0) this.detonate(); } diff --git a/server/game/room/world/physics.js b/server/game/room/world/physics.js index 1db2004..950ae80 100644 --- a/server/game/room/world/physics.js +++ b/server/game/room/world/physics.js @@ -21,11 +21,14 @@ class Physics { bodya.applyDelta(); bodyb.applyDelta(); + + bodya.contact(bodyb); + bodyb.contact(bodya); } let listener = new Box2D.b2ContactListener(); listener.BeginContact = onContact; - listener.EndContact = onContact; + //listener.EndContact = onContact; this.world.SetContactListener(listener); } @@ -66,12 +69,36 @@ class Physics { //if (body.type == 'ship') console.log(b2body.GetLocalCenter()); } + raycast(start, end, callback) { + let p1 = new b2Vec2(start.x, start.y); + let p2 = new b2Vec2(end.x, end.x); + let dx = p1.x - p2.x; + let dy = p1.y - p2.y; + let dis = Math.sqrt(dx * dx + dy * dy); + let closest = {}; + let i = 0; + this.world.RayCast((fixture, point, normal, fraction) => { + let body = fixture.GetBody().GetUserData();; + if (fraction <= closest.fraction) { + closest = { + body: body, + fraction: fraction, + point: point + } + } + //console.log(fixture.GetNext()); + callback(body, point, dis * fraction); + return fraction; + }, p1, p2); + } + remove(body) { this.toRemove.push(body); } step() { this.world.Step(1, 5, 1 / 60); + for (var i = 0; i < this.toRemove.length; i++) { this.world.DestroyBody(this.toRemove[i].b2body); } diff --git a/server/game/room/world/ship.js b/server/game/room/world/ship.js index dd8fcbe..b087b80 100644 --- a/server/game/room/world/ship.js +++ b/server/game/room/world/ship.js @@ -9,6 +9,9 @@ class Ship extends Body { constructor(world, pos, player, build) { super(world); + this.x = pos.x || 0; + this.y = pos.y || 0; + build = build || {}; this.class = build.ship || defaults.spawnShip.ship; this.turrets = build.turrets || defaults.spawnShip.turrets; diff --git a/server/game/room/world/spawner.js b/server/game/room/world/spawner.js index 1408781..18bce36 100644 --- a/server/game/room/world/spawner.js +++ b/server/game/room/world/spawner.js @@ -24,7 +24,9 @@ class Spawner { let pos = { x: ship.center.x + ox, y: ship.center.y + oy, - r: r + r: r, + xvel: ship.vel.x, + yvel: ship.vel.y }; let missile = new Missile(this.world, pos, ship); this.world.addMissile(missile);