import { ctx } from "./main.ts"; import { V2 } from "./util.ts"; const particles = new Set() let particles_to_remove = new Set() 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, hue: number) { 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.1 + hue}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() } }