add discharge collision detection

This commit is contained in:
Asraelite 2016-03-30 19:35:05 +01:00
parent f87aa1446d
commit 44ddaacb15
3 changed files with 40 additions and 4 deletions

View file

@ -15,6 +15,14 @@ body
font-family FreePixel
font-size 16px
canvas
-webkit-touch-callout none
-webkit-user-select none
-khtml-user-select none
-moz-user-select none
-ms-user-select none
user-select: none
.small
font-family PixelArial
font-size 8px

View file

@ -8,15 +8,21 @@ class Discharge {
this.yvel = data.yvel;
this.r = data.r;
this.lifetime = data.lifetime || 100;
this.lifetime = data.lifetime || 50;
this.fixture = fixture;
this.world = this.fixture.body.world;
// Might just make it this.world later if it turns out I need it more.
this.id = this.fixture.body.world.room.generateId();
this.id = this.world.room.generateId();
}
destroy() {
this.fixture.body.world.removeDischarge(this);
this.world.removeDischarge(this);
}
contact(body) {
if (body == this.body) return;
this.destroy();
}
packDelta() {
@ -41,6 +47,25 @@ class Discharge {
}
tick() {
let start = {
x: this.x,
y: this.y
};
this.x += this.xvel;
this.y += this.yvel
let end = {
x: this.x,
y: this.y
};
let contact = this.world.physics.raycast(start, end);
if (contact) {
this.contact(contact.body);
}
if (this.lifetime-- <= 0)
this.destroy();
}

View file

@ -4,6 +4,9 @@
// so most changes done to it should be mirrored there to keep consistent
// physics between client and server.
// TODO: Put all physics constants into a JSON file that is sent to the client
// as well so that the top paragraph no longer applies.
// Note:
// b2Body.GetWorldPoint is broken or something and to get it to work, you
// must pass an object as the second argument. I don't think it matters what