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

@ -18,11 +18,13 @@ class Game {
this.connected = false;
this.state = 'connecting';
this.pingMode = 'fast';
this.input = new Input();
this.net = new Net();
this.world = new World();
this.renderer = new Renderer();
this.player = new Player();
}
tick() {
@ -30,9 +32,10 @@ class Game {
var ship = this.world ? this.world.playerShip : false;
if(ship) {
ship.move = [87, 65, 68].map(k => this.input.keys.held[k] || false);
ship.updateMove();
if(this.player.ship) {
let delta = this.player.packDelta();
if (delta)
game.net.sendUpdate(delta);
}
this.input.clear();

View file

@ -23,15 +23,14 @@ class Net {
this.socket.on('world', function(data) {
game.world.clear();
game.world.playerShipId = data.playerShipId;
game.world.bounds = data.bounds;
for (var i in data.bodies) {
game.world.add(data.bodies[i]);
}
game.world.setPlayerShip(data.playerShipId);
});
this.socket.on('create', function(data) {
console.log(data.id);
game.world.add(data);
});
@ -40,12 +39,8 @@ class Net {
});
};
update(move) {
this.socket.emit('move', {
forward: move[0],
left: move[1],
right: move[2]
});
sendUpdate(inputs) {
this.socket.emit('inputs', inputs);
}
send(msg, data) {

View file

@ -0,0 +1,19 @@
class Player {
constructor(name, team, ship) {
this.name = name;
this.team = team;
this.ship = ship;
this.lastInputs = [];
}
packDelta() {
// W, A, D, Space
let inputs = [87, 65, 68];
inputs = inputs.map(k => game.input.keys.held[k] || false);
inputs[3] = game.input.keys.pressed[32] || false;
let delta = this.lastInputs == inputs ? false : inputs;
this.lastInputs = inputs;
return delta;
}
}

View file

@ -0,0 +1,25 @@
Renderer.prototype.renderBody = (pallet, body) => {
var pos = body.getPos();
var x = pos.x * SCALE;
var y = pos.y * SCALE;
var vx = -game.world.getCenter().x;
var vy = -game.world.getCenter().y;
pallet.view(x + vx, y + vy, false, pos.r);
var context = pallet.context;
var points = body.frame[0];
context.beginPath();
context.moveTo(points[0][0], points[0][1]);
for (var i = 1; i < points.length; i++) {
context.lineTo(points[i][0], points[i][1]);
}
context.closePath();
context.lineWidth = 0.5;
context.strokeStyle = '#fff';
context.fillStyle = '#200';
context.fill();
context.stroke();
pallet.restore();
};

View file

@ -66,6 +66,7 @@ class Renderer {
} else if (body.bodyType == 'asteroid') {
this.renderAsteroid(pallet, body);
} else {
this.renderBody(pallet, body);
// Render structures, projectiles etc..
}
}

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();