Really cool game

This commit is contained in:
Eric Ratliff
2025-06-25 22:14:38 -05:00
commit 694cc903c8
7 changed files with 425 additions and 0 deletions

68
asteroid.js Normal file
View File

@@ -0,0 +1,68 @@
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.vel = p5.Vector.random2D();
this.vel.mult(random(1, 3));
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;
}
update() {
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();
}
}