Files
retro-asteroids/pizza.js
2025-06-25 23:28:10 -05:00

71 lines
2.1 KiB
JavaScript

class Pizza {
constructor(pos, r) {
if (pos) {
this.pos = pos.copy();
this.r = r ? r / 2 : random(40, 60);
} else {
this.pos = createVector(random(width), random(height));
this.r = random(40, 60);
while (dist(this.pos.x, this.pos.y, ship.pos.x, ship.pos.y) < 100) {
this.pos = createVector(random(width), random(height));
}
}
this.baseVel = p5.Vector.random2D();
this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.total = floor(random(8, 12)); // For potential future use
this.offset = [];
for (let i = 0; i < this.total; i++) {
this.offset[i] = random(-this.r * 0.3, this.r * 0.3);
}
}
update() {
this.speed = window.kidMode ? random(0.5, 1.5) : random(1, 3);
this.vel = this.baseVel.copy().mult(this.speed);
this.pos.add(this.vel);
}
edges() {
if (this.pos.x > width + this.r) this.pos.x = -this.r;
else if (this.pos.x < -this.r) this.pos.x = width + this.r;
if (this.pos.y > height + this.r) this.pos.y = -this.r;
else if (this.pos.y < -this.r) this.pos.y = height + this.r;
}
hits(bullet) {
let d = dist(this.pos.x, this.pos.y, bullet.pos.x, bullet.pos.y);
return d < this.r;
}
breakup() {
let newHamburgers = [];
if (this.r > 20) {
newHamburgers.push(new Hamburger(this.pos, this.r));
newHamburgers.push(new Hamburger(this.pos, this.r));
}
return newHamburgers;
}
show() {
push();
translate(this.pos.x, this.pos.y);
// Rotate to align with velocity
let angle = atan2(this.vel.y, this.vel.x);
rotate(angle);
// Draw pizza image (20 points)
if (pizzaImg) {
imageMode(CENTER);
let scaleFactor = (this.r * 2) / pizzaImg.width; // Scale to match radius
scale(scaleFactor);
image(pizzaImg, 0, 0);
} else {
// Fallback if image fails to load
fill(255, 100, 100); // Brighter red for pizza
stroke(255);
ellipse(0, 0, this.r * 2);
console.log('Pizza image not loaded, using fallback');
}
pop();
}
}