improve grappling hook physics and rendering

This commit is contained in:
Asraelite 2016-03-27 02:22:49 +01:00
parent 4753f879e5
commit 663305bd23
11 changed files with 129 additions and 55 deletions

View file

@ -4,14 +4,17 @@ class Effect {
this[i] = data[i];
}
this.pos.x;
this.pos.y;
this.particles = new Set();
this.pallet = game.renderer.pallet;
if (this.type == 'explosion') {
this.createExplosion();
} else if (this.type == 'rope') {
this.createRope();
this.pos = {
x: 0,
y: 0
};
} else {
}
@ -44,10 +47,29 @@ class Effect {
p.tick();
});
if (this.particles.size == 0) {
if (this.particles.size == 0 && !this.keepAlive) {
game.renderer.effects.delete(this);
}
rope: if (this.type == 'rope') {
let bd = game.world.bodies;
if (!bd[this.bodyA.id] || !bd[this.bodyB.id]) {
this.keepAlive = false;
break rope;
}
let p1 = this.posA;
let p2 = this.posB;
let posA = this.bodyA.b2body.GetWorldPoint(new b2Vec2(p1.x, p1.y));
let posB = this.bodyB.b2body.GetWorldPoint(new b2Vec2(p2.x, p2.y));
let context = this.pallet.context;
context.beginPath();
context.moveTo(posA.x * SCALE, posA.y * SCALE);
context.lineTo(posB.x * SCALE, posB.y * SCALE);
context.strokeStyle = '#555';
context.stroke();
}
this.pallet.restore();
}
@ -59,4 +81,12 @@ class Effect {
let b = 'sizzle';
this.generateParticles(0, 0, 1, num, colors, [1, 2], b, 50, 3);
}
createRope() {
this.bodyA = game.world.bodies[this.bodyA];
this.bodyB = game.world.bodies[this.bodyB];
if (!this.bodyA || !this.bodyB) return;
this.keepAlive = true;
}
}

View file

@ -25,6 +25,10 @@ Renderer.prototype.renderShip = (pallet, ship) => {
pallet.image(img, 0, 0, 0);
pallet.image(ship.thrust.forward ? thr8 : thr0, 0, 0, 0);
if (ship.debug) {
pallet.square('#f00', ship.debug.x * SCALE, ship.debug.y * SCALE, 2);
}
pallet.restore();
//pallet.text(ship.name, x + vx | 0, y + vy | 0, '#fff', 'FreePixel', 16, 'center', 'bottom');

View file

@ -28,7 +28,7 @@ class Physics {
bodyDef.angularVelocity = 0;
bodyDef.bullet = body.type == 'missile';
bodyDef.linearDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.01;
bodyDef.angularDamping = body.bodyType == 'asteroid' ? 0.003 : 0.04;
bodyDef.type = body.bodyType == 'structure' ?
b2Body.b2_staticBody : b2Body.b2_dynamicBody;
bodyDef.allowSleep = false;
@ -76,6 +76,7 @@ class Physics {
var r = 0.1;
var body = game.world.bodies[i];
var pos = body.getPos();
if (Math.abs(body.r - pos.r) > 0.3) pos.r = body.r;
var x = (body.x * r + pos.x) / (r + 1);
var y = (body.y * r + pos.y) / (r + 1);
var r = (body.r * r + pos.r) / (r + 1);

View file

@ -22,6 +22,8 @@ class Ship extends Body {
this.thrust = {
forward: data[6]
}
this.debug = data[9];
}
tick() {