diff options
Diffstat (limited to 'src/entities/borderParticles.js')
-rw-r--r-- | src/entities/borderParticles.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/entities/borderParticles.js b/src/entities/borderParticles.js new file mode 100644 index 0000000..a8d7036 --- /dev/null +++ b/src/entities/borderParticles.js @@ -0,0 +1,55 @@ +game.createBorderParticles = ({colors, maxAmount, minAmount, minLife, maxLife, minRadius, maxRadius, maxSpeed}) => { + const particles = game.Entity(); + let particleSpecs = Array(randomInRange(minAmount, maxAmount)).fill(0).map(() => { + const particle = { + x: Math.random(), + y: Math.random(), + dx: Math.random() * maxSpeed - maxSpeed / 2, + dy: Math.random() * maxSpeed - maxSpeed / 2, + radius: randomInRange(minRadius, maxRadius), + color: colors[randomInRange(0, colors.length-1)], + lifetime: randomInRange(minLife, maxLife), + elapsed: 0, + }; + switch (Math.floor(Math.random() * 4)) { + case 0: + particle.y = 0; + particle.dy = -Math.abs(particle.dy); + break; + case 1: + particle.x = 1; + particle.dx = Math.abs(particle.dx); + break; + case 2: + particle.y = 1; + particle.dy = Math.abs(particle.dy); + break; + case 3: + particle.x = 0; + particle.dx = -Math.abs(particle.dx); + break; + } + return particle; + }); + particles.addComponent(game.components.LoadPriority({priority: 1})); + particles.addComponent(game.components.Alive()); + particles.sprite = game.graphics.Sprite({ + drawFunction: (elapsedTime, {x, y, width, height}, context) => { + particleSpecs.map((spec) => spec.elapsed += elapsedTime); + particleSpecs = particleSpecs.filter((spec) => spec.lifetime > spec.elapsed); + if (particleSpecs.length === 0) { + particles.removeComponent("alive"); + } + particleSpecs.map((spec) => { + const position = {x: (spec.x * width) + x + spec.dx * spec.elapsed, y: (spec.y * height) + y + spec.dy * spec.elapsed}; + const fill = context.fillStyle; + context.fillStyle = spec.color; + context.beginPath(); + context.arc(position.x, position.y, spec.radius, 0, 2 * Math.PI); + context.fill(); + context.fillStyle = fill; + }); + } + }) + return particles; +}
\ No newline at end of file |