add basic updating to client on physics

This commit is contained in:
Asraelite 2016-03-21 23:45:27 +00:00
parent 95e0f6b710
commit 1027d980c1
15 changed files with 160 additions and 13 deletions

View file

@ -1,10 +1,28 @@
'use strict';
const uuid = require('uuid');
class Body {
constructor() {
constructor(world) {
this.x = 0;
this.y = 0;
this.r = 0;
this.b2body = false;
this.type = 'dynamic';
this.health = 1;
this.world = world;
this.id = uuid.v4().slice(0, 8);
}
applyDelta() {
this.world.applyDelta(this.id, this.pack());
}
pack() {
let pos = this.b2body.GetPosition();
let rot = this.b2body.GetAngleRadians();
return [pos.x, pos.y, rot];
}
}

View file

@ -15,23 +15,45 @@ class World {
addPlayer(player) {
this.players.add(player);
let ship = new Ship(player);
let ship = new Ship(this, player);
player.ship = ship;
this.ships.set(player, ship);
this.bodies.add(ship);
this.physics.createBody(ship);
}
applyDelta(body, data) {
this.players.forEach(player => player.delta[body] = data);
}
removePlayer(player) {
removeBody(player.ship);
this.removeBody(player.ship);
this.ships.delete(player);
this.players.delete(player);
}
removeBody(body) {
// Remove from Box2d.
this.physics.toRemove.push(body);
this.bodies.delete(body);
this.ships.delete(body);
this.structures.delete(body);
this.asteroids.delete(body);
}
tick() {
start() {
this.interval = setInterval(_ => this.tick(this), 1 / 60);
}
stop() {
clearInterval(this.interval);
}
tick(self) {
self.physics.step();
if (Math.random() < 0.001) {
self.bodies.forEach(body => body.applyDelta());
}
}
}

View file

@ -11,6 +11,7 @@ const b2Vec2 = Box2D.b2Vec2;
class Physics {
constructor() {
this.world = new Box2D.b2World(new b2Vec2(0, 0), false);
this.toRemove = [];
}
createBody(body) {
@ -19,7 +20,7 @@ class Physics {
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.linearVelocity = new b2Vec2(body.xvel || 0.05, body.yvel || 0);
bodyDef.angularVelocity = body.rvel || 0;
bodyDef.type = body.type == 'static' ?
Box2D.b2Body.b2_staticBody : Box2D.b2Body.b2_dynamicBody;
@ -31,7 +32,7 @@ class Physics {
fixtureDef.restitution = 0;
for (var poly of body.structure) {
poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
poly = poly.map(vertex => new b2Vec2(vertex[0], vertex[1]));
fixtureDef.shape = new Box2D.b2PolygonShape();
fixtureDef.shape.SetAsArray(poly, poly.length);
b2body.CreateFixture(fixtureDef);
@ -42,6 +43,13 @@ class Physics {
step() {
this.world.Step(1, 5, 1 / 60);
for (var i = 0; i < this.toRemove.length; i++) {
this.world.DestroyBody(this.toRemove[i].b2body);
console.log(this.world.GetBodyList());
}
this.toRemove = [];
}
}

View file

@ -6,8 +6,8 @@ const hulls = require('./traits/hulls.json');
const Body = require('./body.js');
class Ship extends Body {
constructor(player, build) {
super();
constructor(world, player, build) {
super(world);
this.build = build || defaults.spawnShip.build;
this.player = player;