burgers can hit zombies now
parent
bfe683e1ca
commit
832601e406
@ -1,20 +1,48 @@
|
||||
class Burger {
|
||||
constructor(x, y) {
|
||||
this.sprite = createSprite(x, y, 32, 32);
|
||||
this.sprite.velocity.y = -5;
|
||||
this.image = Images.burger;
|
||||
this.position = {x: x, y: y};
|
||||
this.velocity = {x: 0, y: -400};
|
||||
this.width = 32;
|
||||
this.height = 32;
|
||||
this.alive = true;
|
||||
}
|
||||
|
||||
update() {
|
||||
// this.sprite.velocity.x = this.joyX * 10;
|
||||
// this.sprite.velocity.y = this.joyY * -10;
|
||||
let dt = deltaTime / 1000;
|
||||
this.position.x += this.velocity.x * dt;
|
||||
this.position.y += this.velocity.y * dt;
|
||||
}
|
||||
|
||||
draw() {
|
||||
let corner = {
|
||||
x: this.position.x - this.width*0.5,
|
||||
y: this.position.y - this.height*0.5
|
||||
};
|
||||
|
||||
tint(Colors.Purple);
|
||||
image(this.image, this.sprite.position.x-16, this.sprite.position.y-16, 32, 32);
|
||||
strokeWeight(4);
|
||||
stroke('#FF0000');
|
||||
point(this.sprite.position.x, this.sprite.position.y);
|
||||
image(this.image, corner.x, corner.y, this.width, this.height);
|
||||
|
||||
if (debug) {
|
||||
strokeWeight(4);
|
||||
stroke('#FF0000');
|
||||
point(this.position.x, this.position.y);
|
||||
|
||||
strokeWeight(1);
|
||||
noFill();
|
||||
rect(corner.x, corner.y, this.width, this.height);
|
||||
}
|
||||
}
|
||||
|
||||
kill() {
|
||||
this.alive = false;
|
||||
}
|
||||
|
||||
isDead() {
|
||||
return !this.alive || this.outOfBounds();
|
||||
}
|
||||
}
|
||||
|
||||
Burger.prototype.bounds = bounds;
|
||||
Burger.prototype.outOfBounds = outOfBounds;
|
||||
Burger.prototype.overlaps = overlaps;
|
||||
|
@ -0,0 +1,63 @@
|
||||
function outOfBounds() {
|
||||
let halfWidth = this.width * 0.5;
|
||||
let halfHeight = this.height * 0.5;
|
||||
|
||||
if (this.position.x - halfWidth < 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.position.x + halfWidth > width) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.position.y - halfHeight > height) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (this.position.y + halfHeight < 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
function bounds() {
|
||||
let halfWidth = this.width * 0.5;
|
||||
let halfHeight = this.height * 0.5;
|
||||
|
||||
return {
|
||||
min: {
|
||||
x: this.position.x - halfWidth,
|
||||
y: this.position.y - halfHeight,
|
||||
},
|
||||
max: {
|
||||
x: this.position.x + halfHeight,
|
||||
y: this.position.y + halfHeight,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function overlaps(other) {
|
||||
let me = this.bounds();
|
||||
let them = other.bounds();
|
||||
|
||||
// i'm to the right of them
|
||||
if (me.min.x > them.max.x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// i'm to the left of them
|
||||
if (me.max.x < them.min.x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// i'm entirely above them
|
||||
if (me.max.y < them.min.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// i'm entirely below them
|
||||
if (me.min.y > them.max.y) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue