From d9e8e217c6df3dd63698fa0fe6bd4f53e4891006 Mon Sep 17 00:00:00 2001 From: Asraelite Date: Wed, 30 Mar 2016 19:58:56 +0100 Subject: [PATCH] add ship destruction --- server/game/room/world/body/body.js | 17 +++++++++++++++++ server/game/room/world/body/ship.js | 4 ++++ .../world/body/turret/discharge/discharge.js | 11 ++++++++--- server/game/room/world/index.js | 15 ++++++--------- server/game/room/world/spawner.js | 12 ++++++++++++ 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/server/game/room/world/body/body.js b/server/game/room/world/body/body.js index 39e88ee..a0f1261 100644 --- a/server/game/room/world/body/body.js +++ b/server/game/room/world/body/body.js @@ -54,6 +54,15 @@ class Body { destruct() { this.mounts.forEach(mount => mount.destruct()); this.world.physics.remove(this); + + this.destructType(); + } + + destructType() { + } + + destroy() { + this.world.removeBody(this); } applyDelta() { @@ -87,6 +96,14 @@ class Body { contact() { } + damage(value) { + this.health -= value; + + if (this.health <= 0) { + this.destroy(); + } + } + tick() { let pos = this.b2body.GetPosition(); let bounds = this.world.bounds; diff --git a/server/game/room/world/body/ship.js b/server/game/room/world/body/ship.js index e349c57..10c61a6 100644 --- a/server/game/room/world/body/ship.js +++ b/server/game/room/world/body/ship.js @@ -49,6 +49,10 @@ class Ship extends Body { }; } + destructType() { + this.player.ship = false; + } + updateInputs(packet) { this.aim.x = packet.aim[0]; this.aim.y = packet.aim[1]; diff --git a/server/game/room/world/body/turret/discharge/discharge.js b/server/game/room/world/body/turret/discharge/discharge.js index 65e8d4e..6c3ca16 100644 --- a/server/game/room/world/body/turret/discharge/discharge.js +++ b/server/game/room/world/body/turret/discharge/discharge.js @@ -10,7 +10,8 @@ class Discharge { this.lifetime = data.lifetime || 50; this.fixture = fixture; - this.world = this.fixture.body.world; + this.body = this.fixture.body; + this.world = this.body.world; this.id = this.world.room.generateId(); } @@ -19,9 +20,13 @@ class Discharge { this.world.removeDischarge(this); } - contact(body) { + contact(body, point) { if (body == this.body) return; + if (body.type == 'ship' && body.player.team != this.body.player.team) { + body.damage(0.1, point) + } + this.destroy(); } @@ -63,7 +68,7 @@ class Discharge { let contact = this.world.physics.raycast(start, end); if (contact) { - this.contact(contact.body); + this.contact(contact.body, contact.point); } if (this.lifetime-- <= 0) diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index c32a7ff..999ec86 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -37,15 +37,7 @@ class World { addPlayer(player) { this.players.add(player); - let pos = { - x: player.team == 'b' ? this.bounds.right - 5 : 5, - y: this.bounds.bottom / 2 - }; - let ship = new Ship(this, pos, player); - player.ship = ship; - this.addShip(ship); - - //this.test(); + this.spawner.spawnShip(player); } test() { @@ -173,6 +165,11 @@ class World { tick() { this.physics.step(); + this.players.forEach(player => { + if (!player.ship) + this.spawner.spawnShip(player); + }); + let tickBodies = (set, interval, forceInterval) => { set.forEach(body => { let force = this.tickCount % forceInterval == 0; diff --git a/server/game/room/world/spawner.js b/server/game/room/world/spawner.js index d85d0c2..0fe263a 100644 --- a/server/game/room/world/spawner.js +++ b/server/game/room/world/spawner.js @@ -4,6 +4,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/discharge/laser.js'); +const Ship = require('./body/ship.js'); class Spawner { constructor(world) { @@ -19,6 +20,17 @@ class Spawner { this.world.addAsteroid(asteroid); } + spawnShip(player) { + let pos = { + x: player.team == 'b' ? this.world.bounds.right - 5 : 5, + y: this.world.bounds.bottom / 2 + }; + + let ship = new Ship(this.world, pos, player); + player.ship = ship; + this.world.addShip(ship); + } + spawnMissile(ship) { let r = ship.pos.r; let ox = Math.cos(r) * 0.7;