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
|
@ -17,6 +17,7 @@
|
||||||
"box2d-html5": "^0.1.230",
|
"box2d-html5": "^0.1.230",
|
||||||
"colors": "^1.1.2",
|
"colors": "^1.1.2",
|
||||||
"express": "^4.13.4",
|
"express": "^4.13.4",
|
||||||
|
"poly-decomp": "git://github.com/schteppe/poly-decomp.js.git",
|
||||||
"recursive-readdir": "^1.3.0",
|
"recursive-readdir": "^1.3.0",
|
||||||
"socket.io": "^1.4.5",
|
"socket.io": "^1.4.5",
|
||||||
"uglify-js": "^2.6.2",
|
"uglify-js": "^2.6.2",
|
||||||
|
|
|
@ -22,13 +22,17 @@ class Body {
|
||||||
|
|
||||||
applyForce(x, y, center) {
|
applyForce(x, y, center) {
|
||||||
let b = this.b2body;
|
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) {
|
applyTorque(f) {
|
||||||
this.b2body.ApplyTorque(f);
|
this.b2body.ApplyTorque(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contact() {
|
||||||
|
}
|
||||||
|
|
||||||
tick() {
|
tick() {
|
||||||
let pos = this.b2body.GetPosition();
|
let pos = this.b2body.GetPosition();
|
||||||
let bounds = this.world.bounds;
|
let bounds = this.world.bounds;
|
||||||
|
@ -74,6 +78,13 @@ class Body {
|
||||||
r: this.b2body.GetAngleRadians()
|
r: this.b2body.GetAngleRadians()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get vel() {
|
||||||
|
return {
|
||||||
|
xvel: this.b2body.GetLinearVelocity().x,
|
||||||
|
yvel: this.b2body.GetLinearVelocity().y
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Body;
|
module.exports = Body;
|
||||||
|
|
|
@ -65,6 +65,19 @@ class World {
|
||||||
this.players.forEach(player => player.delta[body] = data);
|
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() {
|
populate() {
|
||||||
for (var i = 0; i < 40; i++) {
|
for (var i = 0; i < 40; i++) {
|
||||||
let pos = {
|
let pos = {
|
||||||
|
|
|
@ -8,6 +8,8 @@ class Missile extends Body {
|
||||||
|
|
||||||
this.x = pos.x * 32;
|
this.x = pos.x * 32;
|
||||||
this.y = pos.y * 32;
|
this.y = pos.y * 32;
|
||||||
|
this.xvel = pos.xvel;
|
||||||
|
this.yvel = pos.yvel;
|
||||||
this.r = pos.r;
|
this.r = pos.r;
|
||||||
|
|
||||||
this.source = source;
|
this.source = source;
|
||||||
|
@ -18,8 +20,12 @@ class Missile extends Body {
|
||||||
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
this.frame = [[[0, 0], [10, 0], [10, 3], [0, 3]]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
contact(body) {
|
||||||
|
this.detonate();
|
||||||
|
}
|
||||||
|
|
||||||
detonate() {
|
detonate() {
|
||||||
// Blow up stuff.
|
this.world.explosion(this.pos, 10);
|
||||||
this.world.removeBody(this);
|
this.world.removeBody(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +34,7 @@ class Missile extends Body {
|
||||||
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
let x = Math.cos(this.b2body.GetAngleRadians()) * power;
|
||||||
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
let y = Math.sin(this.b2body.GetAngleRadians()) * power;
|
||||||
this.applyForce(x, y);
|
this.applyForce(x, y);
|
||||||
|
|
||||||
if(this.fuel-- <= 0)
|
if(this.fuel-- <= 0)
|
||||||
this.detonate();
|
this.detonate();
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,11 +21,14 @@ class Physics {
|
||||||
|
|
||||||
bodya.applyDelta();
|
bodya.applyDelta();
|
||||||
bodyb.applyDelta();
|
bodyb.applyDelta();
|
||||||
|
|
||||||
|
bodya.contact(bodyb);
|
||||||
|
bodyb.contact(bodya);
|
||||||
}
|
}
|
||||||
|
|
||||||
let listener = new Box2D.b2ContactListener();
|
let listener = new Box2D.b2ContactListener();
|
||||||
listener.BeginContact = onContact;
|
listener.BeginContact = onContact;
|
||||||
listener.EndContact = onContact;
|
//listener.EndContact = onContact;
|
||||||
|
|
||||||
this.world.SetContactListener(listener);
|
this.world.SetContactListener(listener);
|
||||||
}
|
}
|
||||||
|
@ -66,12 +69,36 @@ class Physics {
|
||||||
//if (body.type == 'ship') console.log(b2body.GetLocalCenter());
|
//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) {
|
remove(body) {
|
||||||
this.toRemove.push(body);
|
this.toRemove.push(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
step() {
|
step() {
|
||||||
this.world.Step(1, 5, 1 / 60);
|
this.world.Step(1, 5, 1 / 60);
|
||||||
|
|
||||||
for (var i = 0; i < this.toRemove.length; i++) {
|
for (var i = 0; i < this.toRemove.length; i++) {
|
||||||
this.world.DestroyBody(this.toRemove[i].b2body);
|
this.world.DestroyBody(this.toRemove[i].b2body);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,9 @@ class Ship extends Body {
|
||||||
constructor(world, pos, player, build) {
|
constructor(world, pos, player, build) {
|
||||||
super(world);
|
super(world);
|
||||||
|
|
||||||
|
this.x = pos.x || 0;
|
||||||
|
this.y = pos.y || 0;
|
||||||
|
|
||||||
build = build || {};
|
build = build || {};
|
||||||
this.class = build.ship || defaults.spawnShip.ship;
|
this.class = build.ship || defaults.spawnShip.ship;
|
||||||
this.turrets = build.turrets || defaults.spawnShip.turrets;
|
this.turrets = build.turrets || defaults.spawnShip.turrets;
|
||||||
|
|
|
@ -24,7 +24,9 @@ class Spawner {
|
||||||
let pos = {
|
let pos = {
|
||||||
x: ship.center.x + ox,
|
x: ship.center.x + ox,
|
||||||
y: ship.center.y + oy,
|
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);
|
let missile = new Missile(this.world, pos, ship);
|
||||||
this.world.addMissile(missile);
|
this.world.addMissile(missile);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue