[READ-ONLY] Mirror of https://github.com/excaliburjs/sample-fireworks. Example making fireworks with GPU Particles in Excalibur excaliburjs.com/sample-fireworks/
excalibur excaliburjs sample
0

Configure Feed

Select the types of activity you want to include in your feed.

update fireworks

Erik Onarheim (Jan 17, 2025, 1:38 PM -0600) e5d133bd dc351063

+33 -15
+20 -8
src/firework.ts
··· 7 7 explosion2: GpuParticleEmitter; 8 8 life: number; 9 9 body: BodyComponent; 10 + originalPos: Vector; 11 + inProgress: boolean; 10 12 11 13 constructor(pos: Vector, life: number, random: Random) { 12 14 super({ name: "Firework" }) 13 15 this.random = random; 16 + this.originalPos = pos.clone(); 14 17 this.pos = pos; 15 18 this.acc = vec(0, 800); 16 19 ··· 87 90 } 88 91 89 92 launch() { 93 + if (this.inProgress) return; 94 + this.inProgress = true; 95 + this.pos = this.originalPos; 96 + 90 97 coroutine(this.scene!.engine, (function*(this: Firework) { 91 98 this.vel = vec(this.random.floating(-200, 200), this.random.floating(-800, -1000)); 92 99 this.trail.isEmitting = true; 93 - while (this.life > 0) { 100 + let hasExploded = false; 101 + let life = this.life; 102 + while (life > 0) { 94 103 const elapsed = yield; 95 - this.life -= elapsed; 96 - if (this.vel.y >= 0) { 97 - break; 104 + life -= elapsed; 105 + if (this.vel.y >= 0 && !hasExploded) { 106 + hasExploded = true; 107 + this.trail.isEmitting = false; 108 + this.explosion.emitParticles(500); 109 + this.explosion2.emitParticles(500); 98 110 } 99 111 } 100 - this.trail.isEmitting = false; 101 - this.explosion.emitParticles(500); 102 - this.explosion2.emitParticles(500); 103 - 112 + this.trail.clearParticles(); 113 + this.explosion.clearParticles(); 114 + this.explosion2.clearParticles(); 115 + this.inProgress = false; 104 116 } as CoroutineGenerator).bind(this)) 105 117 } 106 118
+13 -7
src/level.ts
··· 12 12 // Scene.onInitialize is where we recommend you perform the composition for your game 13 13 const player = new Player(); 14 14 this.add(player); // Actors need to be added to a scene to be drawn 15 - const firework = new Firework(vec(400, 600), 2000, random); 16 - this.add(firework); 17 - firework.launch(); 18 15 19 - this.input.pointers.on('down', () => { 20 - const firework = new Firework(vec(400, 600), 2000, random); 16 + const fireworks: Firework[] = []; 17 + for (let i = 0; i < 20; i++) { 18 + const firework = new Firework(vec(400, 600), 4000, random); 19 + fireworks.push(firework); 21 20 this.add(firework); 22 - firework.launch(); 23 - }); 21 + } 22 + let currentFireworkIndex = 0; 23 + const launch = () => { 24 + fireworks[currentFireworkIndex].launch(); 25 + currentFireworkIndex = (currentFireworkIndex + 1) % fireworks.length; 26 + }; 27 + 28 + this.input.pointers.on('down', launch); 29 + this.input.keyboard.on('press', launch); 24 30 } 25 31 26 32 override onPreLoad(loader: DefaultLoader): void {