91 lines
2.3 KiB
JavaScript
91 lines
2.3 KiB
JavaScript
class Ship {
|
|
constructor() {
|
|
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.shieldEndTime = 0;
|
|
this.quadShotActive = false;
|
|
this.quadShotEndTime = 0;
|
|
}
|
|
|
|
update() {
|
|
if (this.isBoosting) {
|
|
this.boost();
|
|
}
|
|
this.pos.add(this.vel);
|
|
this.vel.mult(0.99); // Friction
|
|
this.heading += this.rotation;
|
|
if (this.shieldActive && millis() > this.shieldEndTime) {
|
|
this.shieldActive = false;
|
|
}
|
|
if (this.quadShotActive && millis() > this.quadShotEndTime) {
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
setRotation(a) {
|
|
this.rotation = a;
|
|
}
|
|
|
|
boosting(b) {
|
|
this.isBoosting = b;
|
|
}
|
|
|
|
activateShield() {
|
|
this.shieldActive = true;
|
|
this.shieldEndTime = millis() + 10000; // 10 seconds
|
|
}
|
|
|
|
activateQuadShot() {
|
|
this.quadShotActive = true;
|
|
this.quadShotEndTime = millis() + 10000; // 10 seconds
|
|
}
|
|
|
|
show() {
|
|
push();
|
|
translate(this.pos.x, this.pos.y);
|
|
rotate(this.heading + PI / 2);
|
|
if (this.shieldActive) {
|
|
// Pulsing shield effect in last 2 seconds
|
|
let timeLeft = (this.shieldEndTime - millis()) / 1000;
|
|
let scaleFactor = this.shieldActive && timeLeft < 2 ? 1 + 0.1 * sin(millis() / 100) : 1;
|
|
scale(scaleFactor);
|
|
noFill();
|
|
stroke(0, 255, 255); // Cyan shield
|
|
ellipse(0, 0, this.r * 2.5);
|
|
}
|
|
// Ship fill: green if quad-shot active, else black
|
|
if (this.quadShotActive) {
|
|
let timeLeft = (this.quadShotEndTime - millis()) / 1000;
|
|
let alpha = this.quadShotActive && timeLeft < 2 ? map(sin(millis() / 100), -1, 1, 100, 255) : 255;
|
|
fill(0, 255, 0, alpha);
|
|
} else {
|
|
fill(0);
|
|
}
|
|
stroke(255);
|
|
triangle(-this.r, this.r, this.r, this.r, 0, -this.r);
|
|
pop();
|
|
}
|
|
} |