add non-functioning explosions
I'm tired and box2d ray casting is stupid so this'll have to do for today
This commit is contained in:
parent
ac089f3e8e
commit
bf8226481d
7 changed files with 68 additions and 5 deletions
|
@ -22,13 +22,17 @@ class Body {
|
|||
|
||||
applyForce(x, y, center) {
|
||||
let b = this.b2body;
|
||||
b.ApplyForce(new b2Vec2(x, y), b.GetWorldCenter());
|
||||
let c = center ? new b2Vec2(center.x, center.y) : b.GetWorldCenter();
|
||||
b.ApplyForce(new b2Vec2(x, y), c);
|
||||
}
|
||||
|
||||
applyTorque(f) {
|
||||
this.b2body.ApplyTorque(f);
|
||||
}
|
||||
|
||||
contact() {
|
||||
}
|
||||
|
||||
tick() {
|
||||
let pos = this.b2body.GetPosition();
|
||||
let bounds = this.world.bounds;
|
||||
|
@ -74,6 +78,13 @@ class Body {
|
|||
r: this.b2body.GetAngleRadians()
|
||||
};
|
||||
}
|
||||
|
||||
get vel() {
|
||||
return {
|
||||
xvel: this.b2body.GetLinearVelocity().x,
|
||||
yvel: this.b2body.GetLinearVelocity().y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Body;
|
||||
|
|
|
@ -65,6 +65,19 @@ class World {
|
|||
this.players.forEach(player => player.delta[body] = data);
|
||||
}
|
||||
|
||||
explosion(pos, power) {
|
||||
var rays = Array(50).fill().map((_, i) => {
|
||||
let a = Math.PI * i / 25;
|
||||
let x = pos.x + Math.cos(a) * Math.sqrt(power);
|
||||
let y = pos.y + Math.sin(a) * Math.sqrt(power);
|
||||
return this.physics.raycast(pos, { x : x, y: y }, (body, point, dis) => {
|
||||
dis = Math.max(dis, 0.5);
|
||||
let force = power * (1 / dis) * (1 / dis);
|
||||
body.applyForce(x * force, y * force, point);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
populate() {
|
||||
for (var i = 0; i < 40; i++) {
|
||||
let pos = {
|
||||
|
|
|
@ -8,6 +8,8 @@ class Missile extends Body {
|
|||
|
||||
this.x = pos.x * 32;
|
||||
this.y = pos.y * 32;
|
||||
this.xvel = pos.xvel;
|
||||
this.yvel = pos.yvel;
|
||||
this.r = pos.r;
|
||||
|
||||
this.source = source;
|
||||
|
@ -18,8 +20,12 @@ class Missile extends Body {
|
|||
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
||||
}
|
||||
|
||||
contact(body) {
|
||||
this.detonate();
|
||||
}
|
||||
|
||||
detonate() {
|
||||
// Blow up stuff.
|
||||
this.world.explosion(this.pos, 10);
|
||||
this.world.removeBody(this);
|
||||
}
|
||||
|
||||
|
@ -28,7 +34,7 @@ class Missile extends Body {
|
|||
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||
this.applyForce(x, y);
|
||||
|
||||
|
||||
if(this.fuel-- <= 0)
|
||||
this.detonate();
|
||||
}
|
||||
|
|
|
@ -21,11 +21,14 @@ class Physics {
|
|||
|
||||
bodya.applyDelta();
|
||||
bodyb.applyDelta();
|
||||
|
||||
bodya.contact(bodyb);
|
||||
bodyb.contact(bodya);
|
||||
}
|
||||
|
||||
let listener = new Box2D.b2ContactListener();
|
||||
listener.BeginContact = onContact;
|
||||
listener.EndContact = onContact;
|
||||
//listener.EndContact = onContact;
|
||||
|
||||
this.world.SetContactListener(listener);
|
||||
}
|
||||
|
@ -66,12 +69,36 @@ class Physics {
|
|||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
||||
}
|
||||
|
||||
raycast(start, end, callback) {
|
||||
let p1 = new b2Vec2(start.x, start.y);
|
||||
let p2 = new b2Vec2(end.x, end.x);
|
||||
let dx = p1.x - p2.x;
|
||||
let dy = p1.y - p2.y;
|
||||
let dis = Math.sqrt(dx * dx + dy * dy);
|
||||
let closest = {};
|
||||
let i = 0;
|
||||
this.world.RayCast((fixture, point, normal, fraction) => {
|
||||
let body = fixture.GetBody().GetUserData();;
|
||||
if (fraction <= closest.fraction) {
|
||||
closest = {
|
||||
body: body,
|
||||
fraction: fraction,
|
||||
point: point
|
||||
}
|
||||
}
|
||||
//console.log(fixture.GetNext());
|
||||
callback(body, point, dis * fraction);
|
||||
return fraction;
|
||||
}, p1, p2);
|
||||
}
|
||||
|
||||
remove(body) {
|
||||
this.toRemove.push(body);
|
||||
}
|
||||
|
||||
step() {
|
||||
this.world.Step(1, 5, 1 / 60);
|
||||
|
||||
for (var i = 0; i < this.toRemove.length; i++) {
|
||||
this.world.DestroyBody(this.toRemove[i].b2body);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,9 @@ class Ship extends Body {
|
|||
constructor(world, pos, player, build) {
|
||||
super(world);
|
||||
|
||||
this.x = pos.x || 0;
|
||||
this.y = pos.y || 0;
|
||||
|
||||
build = build || {};
|
||||
this.class = build.ship || defaults.spawnShip.ship;
|
||||
this.turrets = build.turrets || defaults.spawnShip.turrets;
|
||||
|
|
|
@ -24,7 +24,9 @@ class Spawner {
|
|||
let pos = {
|
||||
x: ship.center.x + ox,
|
||||
y: ship.center.y + oy,
|
||||
r: r
|
||||
r: r,
|
||||
xvel: ship.vel.x,
|
||||
yvel: ship.vel.y
|
||||
};
|
||||
let missile = new Missile(this.world, pos, ship);
|
||||
this.world.addMissile(missile);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue