72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
class Asteroid {
|
|
constructor(pos, r, isGolden = random() < 0.1) {
|
|
if (pos) {
|
|
this.pos = pos.copy();
|
|
this.r = r ? r / 2 : random(30, 50);
|
|
} else {
|
|
this.pos = createVector(random(width), random(height));
|
|
this.r = random(30, 50);
|
|
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.2, 1.5) : random(1, 3);
|
|
this.vel = this.baseVel.copy().mult(this.speed);
|
|
this.total = floor(random(5, 15));
|
|
this.offset = [];
|
|
for (let i = 0; i < this.total; i++) {
|
|
this.offset[i] = random(-this.r * 0.5, this.r * 0.5);
|
|
}
|
|
this.isGolden = isGolden;
|
|
this.hitsLeft = isGolden ? 3 : 1;
|
|
console.log(`Spawned asteroid, isGolden: ${isGolden}, hitsLeft: ${this.hitsLeft}, speed: ${this.speed}`);
|
|
}
|
|
|
|
update() {
|
|
this.speed = window.kidMode ? random(0.2, 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 newA = [];
|
|
this.hitsLeft--;
|
|
if (this.hitsLeft > 0) {
|
|
newA.push(this);
|
|
} else if (this.r > 20) {
|
|
newA.push(new Asteroid(this.pos, this.r, this.isGolden));
|
|
newA.push(new Asteroid(this.pos, this.r, this.isGolden));
|
|
}
|
|
return newA;
|
|
}
|
|
|
|
show() {
|
|
push();
|
|
translate(this.pos.x, this.pos.y);
|
|
noFill();
|
|
stroke(this.isGolden ? color(255, 215, 0) : 255); // Golden or white
|
|
beginShape();
|
|
for (let i = 0; i < this.total; i++) {
|
|
let angle = map(i, 0, this.total, 0, TWO_PI);
|
|
let r = this.r + this.offset[i];
|
|
let x = r * cos(angle);
|
|
let y = r * sin(angle);
|
|
vertex(x, y);
|
|
}
|
|
endShape(CLOSE);
|
|
pop();
|
|
}
|
|
} |