using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace TowerDevil { // One non-interactive canvas mesh for short-lived trails, sparks and shock waves. [RequireComponent(typeof(CanvasRenderer))] public sealed class BattleEffects : MaskableGraphic { enum Shape { Forge, Gather, Fireball, Bolt, Arrow, Frost, Impact, Claw, Muzzle } struct Effect { public Shape shape; public Vector2 from, to; public Color tint; public float age, duration, size; } readonly List active = new List(64); static readonly Color Brass = new Color32(255, 186, 66, 255), Fire = new Color32(255, 103, 38, 255), Ice = new Color32(112, 225, 255, 255), Steel = new Color32(202, 222, 242, 255), Claw = new Color32(255, 85, 60, 255); internal void Advance(float dt) { if (active.Count == 0) return; for (int i = active.Count - 1; i >= 0; i--) { var e = active[i]; e.age += dt; if (e.age >= e.duration) active.RemoveAt(i); else active[i] = e; } SetVerticesDirty(); } void Add(Shape shape, Vector2 from, Vector2 to, Color tint, float size, float duration, float delay = 0) { active.Add(new Effect { shape = shape, from = from, to = to, tint = tint, size = size, duration = duration, age = -delay }); SetVerticesDirty(); } internal void Gather(Vector2 from, Vector2 to, Color color) => Add(Shape.Gather, from, to, color, 1, .27f); internal void Forge(Vector2 at, Color color, float size) => Add(Shape.Forge, at, at, color, size, .47f); static Color Tint(Kind k) => k == Kind.Frost ? Ice : k == Kind.Cannon ? Fire : k == Kind.Arrow ? Brass : Steel; internal static float Flight(Kind k, float distance) => k == Kind.Frost ? .09f : Mathf.Clamp(distance / (k == Kind.Cannon ? 1160f : 1600f), .09f, .38f); internal void Shoot(Kind k, Vector2 start, Vector2 end) { var tint = Tint(k); if (k == Kind.Frost) { Add(Shape.Frost, start, start, tint, 55, .44f); return; } Vector2 dir = (end - start).normalized; Add(Shape.Muzzle, start, start + dir, tint, k == Kind.Cannon ? 25 : 14, .14f); Add(k == Kind.Cannon ? Shape.Fireball : k == Kind.Ballista ? Shape.Bolt : Shape.Arrow, start, end, tint, 1, Flight(k, Vector2.Distance(start, end))); } internal void Hit(Kind k, Vector2 at, float delay) => Add(Shape.Impact, at, at, Tint(k), k == Kind.Cannon ? 36 : k == Kind.Frost ? 28 : 22, .29f, delay); internal void Strike(Vector2 at, bool boss, float delay) => Add(Shape.Claw, at, at, Claw, boss? 72: 44, .32f, delay); protected override void OnPopulateMesh(VertexHelper mesh) { mesh.Clear(); foreach (var e in active) { if (e.age < 0) continue; float t = Mathf.Clamp01(e.age / e.duration); float fade = 1 - t, outwards = 1 - fade * fade; Vector2 p = e.from; float radius = e.size; switch (e.shape) { case Shape.Gather: p = Vector2.Lerp(e.from, e.to, t * t); Line(mesh, Vector2.Lerp(e.from, e.to, Mathf.Max(0, t * t - .19f)), p, 3 * fade + 1, e.tint); Glow(mesh, p, 11, Alpha(e.tint, .8f)); break; case Shape.Forge: case Shape.Impact: Glow(mesh, p, radius * (1 + .4f * t), Alpha(e.tint, fade * .78f)); Ring(mesh, p, radius * (.18f + outwards), 1 + 4 * fade, Alpha(e.tint, fade)); Sparks(mesh, p, radius, t, e.tint, e.shape == Shape.Forge ? 13 : 8); if (e.shape == Shape.Forge) Star(mesh, p, radius * fade * .82f, Alpha(Color.white, fade)); break; case Shape.Frost: Ring(mesh, p, radius * (.1f + outwards), 1 + 4 * fade, Alpha(Ice, fade)); for (int i = 0; i < 6; i++) { Vector2 d = Direction(i * Mathf.PI / 3); Vector2 branch = p + d * radius * outwards * .58f; Vector2 tip = p + d * radius * outwards; var ice = Alpha(Ice, fade); Line(mesh, p, tip, 3 * fade + 1, ice); Vector2 side = new Vector2(-d.y, d.x) * radius *outwards * .15f; Line(mesh, branch, branch + d * 9 + side, 2, ice); Line(mesh, branch, branch + d * 9 - side, 2, ice); } break; case Shape.Claw: for (int i = -1; i <= 1; i++) { var offset = new Vector2(i * 12, i * 2); var a = p + offset + new Vector2(-.45f, .52f) * radius; var b = p + offset + new Vector2(.42f, -.5f) * radius; var end = Vector2.Lerp(a, b, Mathf.Min(1, t * 4)); Line(mesh, a, end, 7 * fade, Alpha(Claw, fade)); Line(mesh, a, end, 2 * fade, Alpha(Color.white, fade)); } Sparks(mesh, p, radius * .85f, t, Fire, 8); break; case Shape.Muzzle: Vector2 aim = (e.to - p).normalized; Glow(mesh, p, radius * (1 + t), Alpha(e.tint, fade)); Star(mesh, p + aim * radius * .32f, radius * fade, Alpha(Color.white, fade)); break; default: Projectile(mesh, e, t); break; } } } static Vector2 Along(Effect e, float t) { Vector2 p = Vector2.Lerp(e.from, e.to, t); if (e.shape == Shape.Arrow || e.shape == Shape.Fireball) p.y += 4 * t * (1 - t) * Mathf.Min(e.shape == Shape.Arrow ? 48 : 24, Vector2.Distance(e.from, e.to) * .18f); return p; } static void Projectile(VertexHelper mesh, Effect e, float t) { Vector2 p = Along(e, t), d = (Along(e, Mathf.Min(1, t + .02f)) - Along(e, Mathf.Max(0, t - .02f))).normalized; Vector2 n = new Vector2(-d.y, d.x); if (e.shape == Shape.Fireball) { for (int i = 4; i >= 0; i--) Glow(mesh, Along(e, Mathf.Max(0, t - i * .045f)), 14 - i, Alpha(Fire, .87f - i * .12f)); Glow(mesh, p, 8, Color.white); return; } bool bolt = e.shape == Shape.Bolt; var tail = p - d * (bolt ? 38 : 24); Line(mesh, tail - d * 14, p, bolt ? 7 : 4, Alpha(e.tint, .27f)); Line(mesh, tail, p, bolt ? 3.5f : 2.5f, e.tint); Triangle(mesh, p + d * 8, p - d * 5 + n * 4, p - d * 5 - n * 4, Color.white); Line(mesh, tail + d * 8, tail + n * 4, 2, e.tint); Line(mesh, tail + d * 8, tail - n * 4, 2, e.tint); } static Color Alpha(Color c, float alpha) { c.a = Mathf.Clamp01(alpha); return c; } static Vector2 Direction(float a) => new Vector2(Mathf.Cos(a), Mathf.Sin(a)); static void Sparks(VertexHelper m, Vector2 p, float radius, float t, Color tint, int count) { for (int i = 0; i < count; i++) { Vector2 d = Direction(i * 2.39996f + .43f); float speed = .78f + (i % 4) * .21f; Vector2 at = p + d * radius * speed * (1 - (1 - t) * (1 - t)) + Vector2.down * (t * t * 12); Line(m, at - d * (1 - t) * 10, at, 1 + (1 - t) * 2, Alpha(tint, 1 - t)); } } static void Star(VertexHelper m, Vector2 p, float r, Color c) { for (int i = 0; i < 4; i++) { var d = Direction(i * Mathf.PI * .5f); var n = new Vector2(-d.y, d.x) * r * .13f; Triangle(m, p + n, p + d * r, p - n, c); } } static void Triangle(VertexHelper m, Vector2 a, Vector2 b, Vector2 c, Color color) { int i = m.currentVertCount; m.AddVert(a, color, Vector2.zero); m.AddVert(b, color, Vector2.zero); m.AddVert(c, color, Vector2.zero); m.AddTriangle(i, i + 1, i + 2); } internal static void Line(VertexHelper m, Vector2 a, Vector2 b, float width, Color c) { var d = (b - a).normalized; var offset = new Vector2(-d.y, d.x) * width * .5f; int i = m.currentVertCount; m.AddVert(a + offset, c, Vector2.zero); m.AddVert(b + offset, c, Vector2.zero); m.AddVert(b - offset, c, Vector2.zero); m.AddVert(a - offset, c, Vector2.zero); m.AddTriangle(i, i + 1, i + 2); m.AddTriangle(i, i + 2, i + 3); } const int Segments = 24; static void Glow(VertexHelper m, Vector2 p, float size, Color c) { int i = m.currentVertCount; m.AddVert(p, c, Vector2.zero); for (int j = 0; j < Segments; j++) m.AddVert(p + Direction(j * 2 * Mathf.PI / Segments) * size, Alpha(c, 0), Vector2.zero); for (int j = 0; j < Segments; j++) m.AddTriangle(i, i + 1 + j, i + 1 + (j + 1) % Segments); } static void Ring(VertexHelper m, Vector2 p, float size, float width, Color c) { int i = m.currentVertCount; for (int j = 0; j < Segments; j++) { var d = Direction(j * 2 * Mathf.PI / Segments); m.AddVert(p + d * size, c, Vector2.zero); m.AddVert(p + d * Mathf.Max(0, size - width), c, Vector2.zero); } for (int j = 0; j < Segments; j++) { int a = i + 2 * j, b = i + 2 * ((j + 1) % Segments); m.AddTriangle(a, b, b + 1); m.AddTriangle(a, b + 1, a + 1); } } } }