using System; using System.Collections.Generic; using GladiatorIdle.Gfx; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace GladiatorIdle.Arena { internal enum PKind { Spark, Dust, Mote, Ring, Chip } /// Funken (gestreckt, additiv), Sand/Staub (weich), Schwebstaub im Licht, Ringe, Splitter. internal sealed class Particles { struct P { public PKind Kind; public Vector2 Pos, Vel; public float Age, Life, Size, Grow, Gravity, Drag, Rot, Spin; public Color Color; } readonly List

_list = new(2048); readonly Random _rng = new(); public bool Calm; float R(float a, float b) => a + (float)_rng.NextDouble() * (b - a); public void Sparks(Vector2 at, int n, Color c, float dir = 0, float spread = MathF.PI, float speed = 900) { if (Calm) n /= 3; for (int i = 0; i < n; i++) { float a = dir + R(-spread, spread), v = R(0.3f, 1f) * speed; _list.Add(new P { Kind = PKind.Spark, Pos = at, Vel = new Vector2(MathF.Cos(a), MathF.Sin(a)) * v - new Vector2(0, R(100, 300)), Life = R(0.18f, 0.45f), Size = R(2.5f, 5f), Gravity = 1600, Drag = 2.5f, Color = c }); } } public void Dust(Vector2 at, int n, float spread = 60, float rise = 60, float size = 60) { if (Calm) n /= 2; for (int i = 0; i < n; i++) _list.Add(new P { Kind = PKind.Dust, Pos = at + new Vector2(R(-spread, spread), R(-8, 4)), Vel = new Vector2(R(-120, 120), -R(10, rise)), Life = R(0.5f, 1.1f), Size = R(0.6f, 1f) * size, Grow = size * 1.2f, Drag = 2.2f, Color = new Color(150, 120, 85) * 0.55f }); } public void Chips(Vector2 at, int n, Color c, int facing) { if (Calm) n /= 2; for (int i = 0; i < n; i++) _list.Add(new P { Kind = PKind.Chip, Pos = at, Vel = new Vector2(facing * R(150, 650), -R(250, 750)), Life = R(0.5f, 0.9f), Size = R(4, 9), Gravity = 2000, Drag = 0.6f, Rot = R(0, 6), Spin = R(-20, 20), Color = c }); } public void Ring(Vector2 at, Color c, float size = 420, float life = 0.35f) => _list.Add(new P { Kind = PKind.Ring, Pos = at, Life = life, Size = 40, Grow = size / life, Color = c }); /// Schwebstaub im Licht: langsam, flackernd; hält die Anzahl um . public void Motes(int count, float w, float h) { int have = 0; foreach (var p in _list) if (p.Kind == PKind.Mote) have++; for (int i = have; i < count; i++) _list.Add(new P { Kind = PKind.Mote, Pos = new Vector2(R(-200, w + 200), R(80, h - 120)), Vel = new Vector2(R(-12, 12), R(-14, -4)), Life = R(5, 11), Size = R(3, 7), Rot = R(0, 6), Color = new Color(255, 215, 150) * 0.5f }); } public void Update(float dt) { for (int i = _list.Count - 1; i >= 0; i--) { var p = _list[i]; p.Age += dt; if (p.Age >= p.Life) { _list[i] = _list[^1]; _list.RemoveAt(_list.Count - 1); continue; } p.Vel.Y += p.Gravity * dt; p.Vel *= MathF.Exp(-p.Drag * dt); p.Pos += p.Vel * dt; p.Size += p.Grow * dt; p.Rot += p.Spin * dt; if (p.Kind == PKind.Chip && p.Pos.Y > Stage.FloorY) { p.Pos.Y = Stage.FloorY; p.Vel.Y *= -0.3f; p.Vel.X *= 0.5f; p.Spin *= 0.5f; } _list[i] = p; } } /// Weiche Teile (Staub, Schwebstaub, Splitter) mit Alpha-Mischung. public void DrawSoft(SpriteBatch sb) { foreach (var p in _list) { float k = p.Age / p.Life; switch (p.Kind) { case PKind.Dust: Draw.Glow(sb, p.Pos, new Vector2(p.Size * 1.4f, p.Size), p.Color * (1 - k)); break; case PKind.Chip: sb.Draw(Assets.Pixel, p.Pos, null, p.Color * MathF.Min(1, (1 - k) * 3), p.Rot, new Vector2(0.5f), new Vector2(p.Size, p.Size * 0.6f), SpriteEffects.None, 0); break; } } } /// Leuchtende Teile (Funken, Ringe, Schwebstaub) additiv. public void DrawAdd(SpriteBatch sb) { foreach (var p in _list) { float k = p.Age / p.Life; switch (p.Kind) { case PKind.Spark: float len = MathF.Max(6, p.Vel.Length() * 0.035f); float rot = MathF.Atan2(p.Vel.Y, p.Vel.X); sb.Draw(Assets.Pixel, p.Pos, null, p.Color * (1 - k), rot, new Vector2(1, 0.5f), new Vector2(len, p.Size), SpriteEffects.None, 0); Draw.Glow(sb, p.Pos, new Vector2(p.Size * 6), p.Color * (0.35f * (1 - k))); break; case PKind.Ring: // Ring aus vielen Punkten int n = 48; for (int i = 0; i < n; i++) { float a = i * MathF.Tau / n; Draw.Glow(sb, p.Pos + new Vector2(MathF.Cos(a), MathF.Sin(a) * 0.55f) * p.Size, new Vector2(26), p.Color * ((1 - k) * 0.9f)); } break; case PKind.Mote: float tw = 0.5f + 0.5f * MathF.Sin(p.Age * 2.5f + p.Rot); float fade = MathF.Min(1, MathF.Min(p.Age, p.Life - p.Age)); Draw.Glow(sb, p.Pos, new Vector2(p.Size * 2), p.Color * (tw * fade)); break; } } } } }