add start of ship controls

This commit is contained in:
Asraelite 2016-03-22 00:42:48 +00:00
parent 1027d980c1
commit fcfe1e8790
10 changed files with 131 additions and 2 deletions

View file

@ -22,6 +22,10 @@ class Connection {
this.player.name = data.name.slice(0, 20) || 'Fish';
});
socket.on('move', data => {
this.player.move(data);
});
this.server.assignRoom(this.player);
}

View file

@ -17,6 +17,14 @@ class Player {
this.room.remove(this);
}
move(data) {
this.ship.move({
forward: data.forward || 0,
left: data.left || 0,
right: data.right || 0
});
}
sendUpdate() {
if (Object.keys(this.delta).length == 0) return;
this.connection.send('update', this.delta);

View file

@ -51,7 +51,7 @@ class World {
tick(self) {
self.physics.step();
if (Math.random() < 0.001) {
if (Math.random() < 0.01) {
self.bodies.forEach(body => body.applyDelta());
}
}

View file

@ -20,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.05, body.yvel || 0);
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;

View file

@ -5,6 +5,8 @@ const hulls = require('./traits/hulls.json');
const Body = require('./body.js');
const b2Vec2 = require('box2d-html5').b2Vec2;
class Ship extends Body {
constructor(world, player, build) {
super(world);
@ -13,6 +15,28 @@ class Ship extends Body {
this.player = player;
this.structure = hulls[this.build.hull];
}
move(data) {
let b = this.b2body;
for(var i in b) {
//if(typeof b[i] == 'function') console.log(i);
}
if (data.forward) {
//console.log('forward');
b.ApplyLinearImpulse(b2Vec2(0, 500), b.GetWorldCenter());
b.ApplyTorque(2000);
}
if (data.left) {
b.ApplyTorque(-20);
}
if (data.right) {
b.ApplyTorque(20);
}
}
}
module.exports = Ship;