diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-18 13:43:19 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-18 13:43:19 +0200 |
commit | 343bb6dff5ba6fca3d2bcfc55be7329ca38356bc (patch) | |
tree | adfa80c5567c84db906795764ab31a82bc9836cc /test-client/particles.ts | |
parent | db88d9e67841a287cc869ec9dd412be97d94b956 (diff) | |
download | hurrycurry-343bb6dff5ba6fca3d2bcfc55be7329ca38356bc.tar hurrycurry-343bb6dff5ba6fca3d2bcfc55be7329ca38356bc.tar.bz2 hurrycurry-343bb6dff5ba6fca3d2bcfc55be7329ca38356bc.tar.zst |
particle fun
Diffstat (limited to 'test-client/particles.ts')
-rw-r--r-- | test-client/particles.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/test-client/particles.ts b/test-client/particles.ts new file mode 100644 index 00000000..ba695cc0 --- /dev/null +++ b/test-client/particles.ts @@ -0,0 +1,60 @@ +import { ctx } from "./main.ts"; +import { V2 } from "./util.ts"; + +const particles = new Set<Particle>() +let particles_to_remove = new Set<Particle>() + +export function tick_particles(dt: number) { + particles.forEach(p => p.tick(dt) ? {} : particles_to_remove.add(p)) + particles_to_remove.forEach(p => particles.delete(p)) + particles_to_remove = new Set() +} +export function draw_particles() { + particles.forEach(p => p.draw()) +} +export function particle_count() { return particles.size } + +export function particle_splash(pos: V2) { + for (let i = 0; i < 64; i++) { + const p = new Particle() + p.px = pos.x + 0.5 + p.py = pos.y + 0.5 + const a = Math.random() * Math.PI * 2 + const r = Math.sqrt(Math.random()) * 5. + p.vx = Math.sin(a) * r + p.vy = Math.cos(a) * r + p.decay = 4. + p.tr = Math.sqrt(Math.random()) * 1. + p.c = `hsl(${Math.random() * 0.2 + 0.7}turn, 100%, 50%)` + p.r = Math.random() * 0.1 + 0.03 + particles.add(p) + } +} + +class Particle { + c = "red" + r = 0.05 + tr = 0 + px = 0 + py = 0 + vx = 0 + vy = 0 + decay = 1 + + tick(dt: number) { + this.tr -= dt + this.px += this.vx * dt; + this.py += this.vy * dt; + this.vx *= Math.exp(-dt * this.decay) + this.vy *= Math.exp(-dt * this.decay) + return this.tr > 0. + } + draw() { + ctx.fillStyle = this.c + ctx.globalAlpha = Math.min(1, this.tr * 5.) + ctx.beginPath() + ctx.arc(this.px, this.py, this.r, 0, Math.PI * 2) + ctx.fill() + } +} + |