add start of box2d physics
This commit is contained in:
parent
bb9a493450
commit
95e0f6b710
25 changed files with 97 additions and 6 deletions
|
@ -2,7 +2,9 @@
|
|||
|
||||
class Body {
|
||||
constructor() {
|
||||
|
||||
this.b2body = false;
|
||||
this.type = 'dynamic';
|
||||
this.health = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
13
server/game/room/world/fixture.js
Normal file
13
server/game/room/world/fixture.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
'use strict';
|
||||
|
||||
const Body = require('./body.js');
|
||||
|
||||
class Fixture extends Body {
|
||||
constructor(player) {
|
||||
super();
|
||||
|
||||
this.type = 'static';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Ship;
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
8
server/game/room/world/traits/defaults.json
Normal file
8
server/game/room/world/traits/defaults.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"spawnShip": {
|
||||
"build": {
|
||||
"hull": "01",
|
||||
"turrets": [0]
|
||||
}
|
||||
}
|
||||
}
|
12
server/game/room/world/traits/hulls.json
Normal file
12
server/game/room/world/traits/hulls.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"01": [
|
||||
[
|
||||
[1, 28],
|
||||
[30, 28],
|
||||
[30, 19],
|
||||
[18, 2],
|
||||
[13, 2],
|
||||
[1, 19]
|
||||
]
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue