xxxxxxxxxx
104
class Firework {
constructor() {
this.detonator = undefined;
this.exploded = false;
this.done = false;
}
update() {
if (!this.exploded) {
this.missile.applyForce(gravity);
this.missile.update();
if (this.missile.vel.y >= 0) {
this.exploded = true;
this.explode();
}
} else if (!this.done) {
this.detonator.update();
if (this.detonator.done) {
this.done = true;
}
}
}
show() {
if (!this.exploded) {
this.missile.show();
} else {
this.detonator.show();
}
}
}
class GenericFirework extends Firework {
constructor() {
super();
this.missile = new Missile(random(width * 0.05, width * 0.95));
}
explode() {
let d = random(detonatorClasses);
this.detonator = new d(this.missile.pos.x, this.missile.pos.y);
random(sounds).play(0.4);
}
}
class LetterFirework extends Firework {
constructor(x,letter) {
super();
this.letter = letter;
this.missile = new Missile(x);
this.missile.vel = createVector(0,startVel* random(0.98,1.02));
if (letter == letters.Heart) {
this.missile.vel = createVector(0,startVel* 0.78);
}
this.missile.acc = createVector(0,0);
}
explode() {
this.detonator = new DetonatorLetter(
this.missile.pos.x,
this.missile.pos.y,
this.letter
);
random(sounds).play(0.4);
}
}
class Missile {
constructor(x) {
this.pos = createVector(x, height);
this.vel = createVector(0, startVel * random(0.9, 1.1));
this.acc = createVector(random(-0.5, 0.5), 0);
this.hist = [];
this.histlen = floor(random(8, 18));
}
applyForce(force) {
this.acc.add(force);
}
update() {
let x = this.pos.x;
let y = this.pos.y;
this.hist.push(createVector(x, y));
if (this.hist.length > this.histlen) {
this.hist.splice(0, 1);
}
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
show() {
for (let h of this.hist) {
for (let i = 0; i < 2; i++) {
strokeWeight(random(1, 2));
stroke(random(5, 15), 80, random(65,100), 90);
point(h.x + random(-1.5, 1.5), h.y + random(0, 5));
}
}
}
}