45 lines
887 B
JavaScript
45 lines
887 B
JavaScript
class Hitbox {
|
|
|
|
static hitboxes = [];
|
|
|
|
constructor (x, y, w, h) {
|
|
this.x = x;
|
|
this.y = y;
|
|
this.w = w;
|
|
this.h = h;
|
|
|
|
Hitbox.hitboxes.push(this);
|
|
}
|
|
|
|
collides(hb) {
|
|
return rectsCollide(this.x, this.y, this.w, this.h, hb.x, hb.y, hb.w, hb.h);
|
|
}
|
|
|
|
collidesAny() {
|
|
for (let i = 0; i < Hitbox.hitboxes.length; i++) {
|
|
if (Hitbox.hitboxes[i] != null && Hitbox.hitboxes[i] != this && this.collides(Hitbox.hitboxes[i]))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
pointInBound(x, y) {
|
|
return x >= this.x && y >= this.y && x < this.x + this.w && y < this.y + this.h;
|
|
}
|
|
|
|
delete() {
|
|
for (let i = 0; i < Hitbox.hitboxes.length; i++) {
|
|
if (Hitbox.hitboxes[i] == this) {
|
|
Hitbox.hitboxes.splice(i, 1);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
static tempCollidesAny(x, y, w, h) {
|
|
let hb = new Hitbox(x, y, w, h);
|
|
let collides = hb.collidesAny();
|
|
hb.delete();
|
|
return collides;
|
|
}
|
|
} |