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

126 lines
3.2 KiB
JavaScript

class Ship {
constructor(safeSpawn = false) {
if (safeSpawn) {
this.pos = createVector(random(width), random(height));
let safe = false;
while (!safe) {
safe = true;
for (let asteroid of asteroids) {
if (dist(this.pos.x, this.pos.y, asteroid.pos.x, asteroid.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
for (let pizza of pizzas) {
if (dist(this.pos.x, this.pos.y, pizza.pos.x, pizza.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
for (let hamburger of hamburgers) {
if (dist(this.pos.x, this.pos.y, hamburger.pos.x, hamburger.pos.y) < 100) {
safe = false;
this.pos = createVector(random(width), random(height));
break;
}
}
}
} else {
this.pos = createVector(width / 2, height / 2);
}
this.r = 20;
this.heading = 0;
this.rotation = 0;
this.vel = createVector(0, 0);
this.isBoosting = false;
this.shieldActive = false;
this.shieldStart = 0;
this.quadShotActive = false;
this.quadShotStart = 0;
}
boosting(b) {
this.isBoosting = b;
}
update() {
if (this.isBoosting) {
this.boost();
}
this.pos.add(this.vel);
this.vel.mult(0.99);
this.heading += this.rotation;
// Check shield duration
if (this.shieldActive && millis() - this.shieldStart > 10000) {
this.shieldActive = false;
}
// Check quad shot duration
if (this.quadShotActive && millis() - this.quadShotStart > 10000) {
this.quadShotActive = false;
}
}
boost() {
let force = p5.Vector.fromAngle(this.heading);
force.mult(0.1);
this.vel.add(force);
}
hits(obj) {
let d = dist(this.pos.x, this.pos.y, obj.pos.x, obj.pos.y);
return d < this.r + obj.r;
}
activateShield() {
this.shieldActive = true;
this.shieldStart = millis();
}
activateQuadShot() {
this.quadShotActive = true;
this.quadShotStart = millis();
}
setRotation(a) {
this.rotation = a;
}
show() {
push();
translate(this.pos.x, this.pos.y);
rotate(this.heading + PI / 2);
noFill();
stroke(255);
if (this.quadShotActive) {
fill(0, 255, 0); // Green fill for quad-shot
let quadTimeLeft = 10000 - (millis() - this.quadShotStart);
if (quadTimeLeft < 2000) {
let alpha = 255 * (1 + sin(millis() * 0.01)) / 2;
fill(0, 255, 0, alpha);
}
}
triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
if (this.shieldActive) {
noFill();
stroke(0, 255, 255);
let shieldTimeLeft = 10000 - (millis() - this.shieldStart);
if (shieldTimeLeft < 2000) {
let scaleFactor = 1 + 0.2 * sin(millis() * 0.01);
scale(scaleFactor);
}
ellipse(0, 0, this.r * 2.5);
}
pop();
}
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;
}
}