add start of grappling hook

This commit is contained in:
Asraelite 2016-03-26 20:23:05 +00:00
parent ff0ac094cf
commit 4753f879e5
14 changed files with 192 additions and 69 deletions

View file

@ -4,7 +4,17 @@ class Input {
x: 0,
y: 0,
held: {},
pressed: {}
pressed: {},
get wx() {
let x = this.x + game.world.center.x;
return (x - game.renderer.canvas.width / 2) / SCALE;
},
get wy() {
let y = this.y + game.world.center.y;
return (y - game.renderer.canvas.height / 2) / SCALE;
}
};
this.keys = {
@ -12,11 +22,15 @@ class Input {
pressed: {}
};
this.mouse
document.addEventListener('mousemove', this.mouseMove.bind(this));
document.addEventListener('mousedown', this.mouseDown.bind(this));
document.addEventListener('mouseup', this.mouseUp.bind(this));
document.addEventListener('keydown', this.keyDown.bind(this));
document.addEventListener('keyup', this.keyUp.bind(this));
document.addEventListener('contextmenu', e => e.preventDefault());
}
mouseMove() {

View file

@ -12,6 +12,10 @@ class Player {
let inputs = [87, 65, 68];
inputs = inputs.map(k => game.input.keys.held[k] || false);
inputs[3] = game.input.keys.pressed[32] || false;
inputs[4] = game.input.mouse.wx;
inputs[5] = game.input.mouse.wy;
inputs[6] = game.input.mouse.held[3];
inputs[7] = game.input.mouse.pressed[1];
let delta = this.lastInputs == inputs ? false : inputs;
this.lastInputs = inputs;
return delta;

View file

@ -8,18 +8,20 @@ Renderer.prototype.renderBody = (pallet, body) => {
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]);
var polys = body.frame;
for (var points of polys) {
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();
}
context.closePath();
context.lineWidth = 0.5;
context.strokeStyle = '#fff';
context.fillStyle = '#200';
context.fill();
context.stroke();
pallet.restore();
};

View file

@ -54,4 +54,8 @@ class Body {
updateType() {
}
tick() {
}
}

View file

@ -14,21 +14,9 @@ class World {
}
}
// Deprecated
getCenter() {
if (!this.playerShip) return { x: 0, y: 0 };
var x = this.playerShip.getPos().x * SCALE;
var y = this.playerShip.getPos().y * SCALE;
var comx = this.playerShip.com.x * SCALE;
var comy = this.playerShip.com.y * SCALE;
var r = this.playerShip.getPos().r;
var d = Math.sqrt(comx * comx + comy * comy);
var a = Math.atan2(comy, comx);
x += Math.cos(a + r) * d;
y += Math.sin(a + r) * d;
return { x: x, y: y };
return this.center;
};
add(data) {
@ -37,6 +25,7 @@ class World {
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 (!body) body = new Body(data);
this.bodies[body.id] = body;
this.physics.createBody(body);
@ -80,4 +69,21 @@ class World {
this.bodies[i].tick();
}
};
get center() {
if (!this.playerShip) return { x: 0, y: 0 };
let x = this.playerShip.getPos().x * SCALE;
let y = this.playerShip.getPos().y * SCALE;
let comx = this.playerShip.com.x * SCALE;
let comy = this.playerShip.com.y * SCALE;
let r = this.playerShip.getPos().r;
let d = Math.sqrt(comx * comx + comy * comy);
let a = Math.atan2(comy, comx);
x += Math.cos(a + r) * d;
y += Math.sin(a + r) * d;
return { x: x, y: y };
}
}