add basic missiles

This commit is contained in:
Asraelite 2016-03-24 18:42:11 +00:00
parent 21a30ad212
commit ac089f3e8e
19 changed files with 215 additions and 85 deletions

View file

@ -0,0 +1,14 @@
class Missile extends Body {
constructor(data) {
super(data);
this.bodyType = 'missile';
}
updateType() {
}
tick() {
}
}

View file

@ -19,12 +19,14 @@ class Physics {
let bodyDef = new b2BodyDef();
bodyDef.userData = body;
bodyDef.position = new b2Vec2(body.x || 0, body.y || 0);
bodyDef.angle = body.r || 0;
bodyDef.fixedRotation = false;
bodyDef.active = true;
bodyDef.linearVelocity = new b2Vec2(0, 0);
bodyDef.angularVelocity = 0;
bodyDef.linearDamping = body.bodyType == 'ship' ? 0.01 : 0.003;
bodyDef.angularDamping = body.bodyType == 'ship' ? 0.01 : 0.003;
bodyDef.bullet = body.type == 'missile';
bodyDef.linearDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
bodyDef.type = body.bodyType == 'structure' ?
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
bodyDef.allowSleep = false;

View file

@ -1,7 +0,0 @@
class Player {
constructor(name, team, ship) {
this.name = name;
this.team = team;
this.ship = ship;
}
}

View file

@ -5,7 +5,6 @@ class Ship extends Body {
this.team = data.team;
this.name = data.name;
this.hull = '01';
this.move = [];
this.thrust = {};
this.power = data.power;
this.mounts = data.mounts;
@ -19,13 +18,6 @@ class Ship extends Body {
this.bodyType = 'ship';
}
updateMove() {
if (JSON.stringify(this.move) != JSON.stringify(this.lastMove) || true) {
game.net.update(this.move);
this.lastMove = Array.apply(0, this.move); // Bloody Javascript.
}
}
updateType(data) {
this.thrust = {
forward: data[6]
@ -33,18 +25,18 @@ class Ship extends Body {
}
tick() {
if (this.move[0]) {
if (this.thrust.forward) {
var power = this.power.forward;
var x = Math.cos(this.getPos().r) * power;
var y = Math.sin(this.getPos().r) * power;
this.applyForce(x, y);
}
if (this.move[1]) {
if (this.thrust.left) {
this.applyTorque(-this.power.rotation);
}
if (this.move[2]) {
if (this.thrust.right) {
this.applyTorque(this.power.rotation);
}
}

View file

@ -4,7 +4,6 @@ class World {
constructor() {
this.bodies = {};
this.playerShip = false;
this.playerShipId = false;
this.physics = new Physics();
this.bounds = {
@ -37,6 +36,7 @@ class World {
if (data.type == 'asteroid') body = new Asteroid(data);
if (data.type == 'ship') body = new Ship(data);
if (data.type == 'structure') body = new Structure(data);
if (data.type == 'missile') body = new Missile(data);
//if(data.type == 'ship') console.log(body);
@ -59,8 +59,6 @@ class World {
};
update(data) {
this.playerShip = this.bodies[this.playerShipId];
for (var id in data) {
if (!this.bodies[id]) {
game.net.send('requestBodyData', id);
@ -74,6 +72,11 @@ class World {
}
};
setPlayerShip(id) {
this.playerShip = this.bodies[id];
game.player.ship = this.playerShip;
}
tick() {
this.physics.step();