diff --git a/package.json b/package.json index 269e46c..a285cea 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "author": "Markus Scully ", "license": "ISC", "dependencies": { - "box2dweb": "^2.1.0-b", + "box2d-html5": "^0.1.230", "colors": "^1.1.2", "express": "^4.13.4", "recursive-readdir": "^1.3.0", diff --git a/public/img/ships/01/hull.png b/public/img/ships/01/hull.png new file mode 100644 index 0000000..cbba37b Binary files /dev/null and b/public/img/ships/01/hull.png differ diff --git a/public/img/ships/01/teama.png b/public/img/ships/01/teama.png new file mode 100644 index 0000000..3ca0fdd Binary files /dev/null and b/public/img/ships/01/teama.png differ diff --git a/public/img/ships/01/teamb.png b/public/img/ships/01/teamb.png new file mode 100644 index 0000000..8247bd8 Binary files /dev/null and b/public/img/ships/01/teamb.png differ diff --git a/public/img/ships/01/thrust0.png b/public/img/ships/01/thrust0.png new file mode 100644 index 0000000..7ade62b Binary files /dev/null and b/public/img/ships/01/thrust0.png differ diff --git a/public/img/ships/01/thrust1.png b/public/img/ships/01/thrust1.png new file mode 100644 index 0000000..9148d91 Binary files /dev/null and b/public/img/ships/01/thrust1.png differ diff --git a/public/img/ships/01/thrust2.png b/public/img/ships/01/thrust2.png new file mode 100644 index 0000000..c930c6b Binary files /dev/null and b/public/img/ships/01/thrust2.png differ diff --git a/public/img/ships/01/thrust3.png b/public/img/ships/01/thrust3.png new file mode 100644 index 0000000..a504c30 Binary files /dev/null and b/public/img/ships/01/thrust3.png differ diff --git a/public/img/ships/01/thrust4.png b/public/img/ships/01/thrust4.png new file mode 100644 index 0000000..b2b8f36 Binary files /dev/null and b/public/img/ships/01/thrust4.png differ diff --git a/public/img/ships/01/thrust5.png b/public/img/ships/01/thrust5.png new file mode 100644 index 0000000..8f72cba Binary files /dev/null and b/public/img/ships/01/thrust5.png differ diff --git a/public/img/ships/01/thrust6.png b/public/img/ships/01/thrust6.png new file mode 100644 index 0000000..3bfae91 Binary files /dev/null and b/public/img/ships/01/thrust6.png differ diff --git a/public/img/ships/01/thrust7.png b/public/img/ships/01/thrust7.png new file mode 100644 index 0000000..397ee8a Binary files /dev/null and b/public/img/ships/01/thrust7.png differ diff --git a/public/img/ships/01/thrust8.png b/public/img/ships/01/thrust8.png new file mode 100644 index 0000000..49af356 Binary files /dev/null and b/public/img/ships/01/thrust8.png differ diff --git a/public/img/ships/01/thrust9.png b/public/img/ships/01/thrust9.png new file mode 100644 index 0000000..ed75767 Binary files /dev/null and b/public/img/ships/01/thrust9.png differ diff --git a/public/img/turrets/01large.png b/public/img/turrets/01large.png new file mode 100644 index 0000000..eb77f5c Binary files /dev/null and b/public/img/turrets/01large.png differ diff --git a/public/img/turrets/01medium.png b/public/img/turrets/01medium.png new file mode 100644 index 0000000..547e227 Binary files /dev/null and b/public/img/turrets/01medium.png differ diff --git a/public/img/turrets/01small.png b/public/img/turrets/01small.png new file mode 100644 index 0000000..de89bce Binary files /dev/null and b/public/img/turrets/01small.png differ diff --git a/server/game/room/world/body.js b/server/game/room/world/body.js index e6405c3..a237b9c 100644 --- a/server/game/room/world/body.js +++ b/server/game/room/world/body.js @@ -2,7 +2,9 @@ class Body { constructor() { - + this.b2body = false; + this.type = 'dynamic'; + this.health = 1; } } diff --git a/server/game/room/world/fixture.js b/server/game/room/world/fixture.js new file mode 100644 index 0000000..313eeb7 --- /dev/null +++ b/server/game/room/world/fixture.js @@ -0,0 +1,13 @@ +'use strict'; + +const Body = require('./body.js'); + +class Fixture extends Body { + constructor(player) { + super(); + + this.type = 'static'; + } +} + +module.exports = Ship; diff --git a/server/game/room/world/index.js b/server/game/room/world/index.js index 7f0477d..d6bf7d6 100644 --- a/server/game/room/world/index.js +++ b/server/game/room/world/index.js @@ -1,10 +1,11 @@ 'use strict'; +const Physics = require('./physics.js'); const Ship = require('./ship.js'); class World { constructor() { - this.box2d = false; + this.physics = new Physics(); this.bodies = new Set(); this.structures = new Set(); this.asteroids = new Set(); @@ -14,7 +15,9 @@ class World { addPlayer(player) { this.players.add(player); - this.ships.set(player, new Ship(player)); + let ship = new Ship(player); + this.ships.set(player, ship); + this.physics.createBody(ship); } removePlayer(player) { diff --git a/server/game/room/world/physics.js b/server/game/room/world/physics.js index e69de29..7bcf64d 100644 --- a/server/game/room/world/physics.js +++ b/server/game/room/world/physics.js @@ -0,0 +1,48 @@ +'use strict'; + +// This file is very similar, but not identical to its client counterpart +// so most changes done to it should be mirrored there to keep consistent +// physics between client and server. + +const Box2D = require('box2d-html5'); + +const b2Vec2 = Box2D.b2Vec2; + +class Physics { + constructor() { + this.world = new Box2D.b2World(new b2Vec2(0, 0), false); + } + + createBody(body) { + let bodyDef = new Box2D.b2BodyDef(); + bodyDef.userData = body; + bodyDef.position = new b2Vec2(body.x || 0, body.y || 0); + bodyDef.fixedRotation = false; + bodyDef.active = true; + bodyDef.linearVelocity = new b2Vec2(body.xvel || 0, body.yvel || 0); + bodyDef.angularVelocity = body.rvel || 0; + bodyDef.type = body.type == 'static' ? + Box2D.b2Body.b2_staticBody : Box2D.b2Body.b2_dynamicBody; + let b2body = this.world.CreateBody(bodyDef); + + let fixtureDef = new Box2D.b2FixtureDef(); + fixtureDef.density = 10; + fixtureDef.friction = 1; + fixtureDef.restitution = 0; + + for (var poly of body.structure) { + poly.map(vertex => new b2Vec2(vertex[0], vertex[1])); + fixtureDef.shape = new Box2D.b2PolygonShape(); + fixtureDef.shape.SetAsArray(poly, poly.length); + b2body.CreateFixture(fixtureDef); + } + + body.b2body = b2body; + } + + step() { + this.world.Step(1, 5, 1 / 60); + } +} + +module.exports = Physics; diff --git a/server/game/room/world/ship.js b/server/game/room/world/ship.js index 5b18ec8..9ea679c 100644 --- a/server/game/room/world/ship.js +++ b/server/game/room/world/ship.js @@ -1,12 +1,17 @@ 'use strict'; +const defaults = require('./traits/defaults.json'); +const hulls = require('./traits/hulls.json'); + const Body = require('./body.js'); class Ship extends Body { - constructor(player) { + constructor(player, build) { super(); - + + this.build = build || defaults.spawnShip.build; this.player = player; + this.structure = hulls[this.build.hull]; } } diff --git a/server/game/room/world/structure.js b/server/game/room/world/structure.js deleted file mode 100644 index e69de29..0000000 diff --git a/server/game/room/world/traits/defaults.json b/server/game/room/world/traits/defaults.json new file mode 100644 index 0000000..00e3d96 --- /dev/null +++ b/server/game/room/world/traits/defaults.json @@ -0,0 +1,8 @@ +{ + "spawnShip": { + "build": { + "hull": "01", + "turrets": [0] + } + } +} diff --git a/server/game/room/world/traits/hulls.json b/server/game/room/world/traits/hulls.json new file mode 100644 index 0000000..8d254e5 --- /dev/null +++ b/server/game/room/world/traits/hulls.json @@ -0,0 +1,12 @@ +{ + "01": [ + [ + [1, 28], + [30, 28], + [30, 19], + [18, 2], + [13, 2], + [1, 19] + ] + ] +}