BattleEffects.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace TowerDevil
  5. {
  6. // One non-interactive canvas mesh for short-lived trails, sparks and shock waves.
  7. [RequireComponent(typeof(CanvasRenderer))]
  8. public sealed class BattleEffects : MaskableGraphic
  9. {
  10. enum Shape
  11. {
  12. Forge,
  13. Gather,
  14. Fireball,
  15. Bolt,
  16. Arrow,
  17. Frost,
  18. Impact,
  19. Claw,
  20. Muzzle
  21. }
  22. struct Effect
  23. {
  24. public Shape shape;
  25. public Vector2 from, to;
  26. public Color tint;
  27. public float age, duration, size;
  28. }
  29. readonly List<Effect> active = new List<Effect>(64);
  30. static readonly Color Brass = new Color32(255, 186, 66, 255), Fire = new Color32(255, 103, 38, 255),
  31. Ice = new Color32(112, 225, 255, 255), Steel = new Color32(202, 222, 242, 255),
  32. Claw = new Color32(255, 85, 60, 255);
  33. internal void Advance(float dt)
  34. {
  35. if (active.Count == 0)
  36. return;
  37. for (int i = active.Count - 1; i >= 0; i--)
  38. {
  39. var e = active[i];
  40. e.age += dt;
  41. if (e.age >= e.duration)
  42. active.RemoveAt(i);
  43. else
  44. active[i] = e;
  45. }
  46. SetVerticesDirty();
  47. }
  48. void Add(Shape shape, Vector2 from, Vector2 to, Color tint, float size, float duration, float delay = 0)
  49. {
  50. active.Add(new Effect { shape = shape, from = from, to = to, tint = tint, size = size, duration = duration,
  51. age = -delay });
  52. SetVerticesDirty();
  53. }
  54. internal void Gather(Vector2 from, Vector2 to, Color color) => Add(Shape.Gather, from, to, color, 1, .27f);
  55. internal void Forge(Vector2 at, Color color, float size) => Add(Shape.Forge, at, at, color, size, .47f);
  56. static Color Tint(Kind k) => k == Kind.Frost ? Ice : k == Kind.Cannon ? Fire : k == Kind.Arrow ? Brass : Steel;
  57. internal static float Flight(Kind k, float distance) =>
  58. k == Kind.Frost ? .09f : Mathf.Clamp(distance / (k == Kind.Cannon ? 1160f : 1600f), .09f, .38f);
  59. internal void Shoot(Kind k, Vector2 start, Vector2 end)
  60. {
  61. var tint = Tint(k);
  62. if (k == Kind.Frost)
  63. {
  64. Add(Shape.Frost, start, start, tint, 55, .44f);
  65. return;
  66. }
  67. Vector2 dir = (end - start).normalized;
  68. Add(Shape.Muzzle, start, start + dir, tint, k == Kind.Cannon ? 25 : 14, .14f);
  69. Add(k == Kind.Cannon ? Shape.Fireball
  70. : k == Kind.Ballista ? Shape.Bolt
  71. : Shape.Arrow,
  72. start, end, tint, 1, Flight(k, Vector2.Distance(start, end)));
  73. }
  74. internal void Hit(Kind k, Vector2 at, float delay) => Add(Shape.Impact, at, at, Tint(k),
  75. k == Kind.Cannon ? 36
  76. : k == Kind.Frost ? 28
  77. : 22,
  78. .29f, delay);
  79. internal void Strike(Vector2 at, bool boss, float delay) => Add(Shape.Claw, at, at, Claw, boss? 72: 44, .32f,
  80. delay);
  81. protected override void OnPopulateMesh(VertexHelper mesh)
  82. {
  83. mesh.Clear();
  84. foreach (var e in active)
  85. {
  86. if (e.age < 0)
  87. continue;
  88. float t = Mathf.Clamp01(e.age / e.duration);
  89. float fade = 1 - t, outwards = 1 - fade * fade;
  90. Vector2 p = e.from;
  91. float radius = e.size;
  92. switch (e.shape)
  93. {
  94. case Shape.Gather:
  95. p = Vector2.Lerp(e.from, e.to, t * t);
  96. Line(mesh, Vector2.Lerp(e.from, e.to, Mathf.Max(0, t * t - .19f)), p, 3 * fade + 1, e.tint);
  97. Glow(mesh, p, 11, Alpha(e.tint, .8f));
  98. break;
  99. case Shape.Forge:
  100. case Shape.Impact:
  101. Glow(mesh, p, radius * (1 + .4f * t), Alpha(e.tint, fade * .78f));
  102. Ring(mesh, p, radius * (.18f + outwards), 1 + 4 * fade, Alpha(e.tint, fade));
  103. Sparks(mesh, p, radius, t, e.tint, e.shape == Shape.Forge ? 13 : 8);
  104. if (e.shape == Shape.Forge)
  105. Star(mesh, p, radius * fade * .82f, Alpha(Color.white, fade));
  106. break;
  107. case Shape.Frost:
  108. Ring(mesh, p, radius * (.1f + outwards), 1 + 4 * fade, Alpha(Ice, fade));
  109. for (int i = 0; i < 6; i++)
  110. {
  111. Vector2 d = Direction(i * Mathf.PI / 3);
  112. Vector2 branch = p + d * radius * outwards * .58f;
  113. Vector2 tip = p + d * radius * outwards;
  114. var ice = Alpha(Ice, fade);
  115. Line(mesh, p, tip, 3 * fade + 1, ice);
  116. Vector2 side = new Vector2(-d.y, d.x) * radius *outwards * .15f;
  117. Line(mesh, branch, branch + d * 9 + side, 2, ice);
  118. Line(mesh, branch, branch + d * 9 - side, 2, ice);
  119. }
  120. break;
  121. case Shape.Claw:
  122. for (int i = -1; i <= 1; i++)
  123. {
  124. var offset = new Vector2(i * 12, i * 2);
  125. var a = p + offset + new Vector2(-.45f, .52f) * radius;
  126. var b = p + offset + new Vector2(.42f, -.5f) * radius;
  127. var end = Vector2.Lerp(a, b, Mathf.Min(1, t * 4));
  128. Line(mesh, a, end, 7 * fade, Alpha(Claw, fade));
  129. Line(mesh, a, end, 2 * fade, Alpha(Color.white, fade));
  130. }
  131. Sparks(mesh, p, radius * .85f, t, Fire, 8);
  132. break;
  133. case Shape.Muzzle:
  134. Vector2 aim = (e.to - p).normalized;
  135. Glow(mesh, p, radius * (1 + t), Alpha(e.tint, fade));
  136. Star(mesh, p + aim * radius * .32f, radius * fade, Alpha(Color.white, fade));
  137. break;
  138. default:
  139. Projectile(mesh, e, t);
  140. break;
  141. }
  142. }
  143. }
  144. static Vector2 Along(Effect e, float t)
  145. {
  146. Vector2 p = Vector2.Lerp(e.from, e.to, t);
  147. if (e.shape == Shape.Arrow || e.shape == Shape.Fireball)
  148. p.y += 4 * t * (1 - t) *
  149. Mathf.Min(e.shape == Shape.Arrow ? 48 : 24, Vector2.Distance(e.from, e.to) * .18f);
  150. return p;
  151. }
  152. static void Projectile(VertexHelper mesh, Effect e, float t)
  153. {
  154. Vector2 p = Along(e, t),
  155. d = (Along(e, Mathf.Min(1, t + .02f)) - Along(e, Mathf.Max(0, t - .02f))).normalized;
  156. Vector2 n = new Vector2(-d.y, d.x);
  157. if (e.shape == Shape.Fireball)
  158. {
  159. for (int i = 4; i >= 0; i--)
  160. Glow(mesh, Along(e, Mathf.Max(0, t - i * .045f)), 14 - i, Alpha(Fire, .87f - i * .12f));
  161. Glow(mesh, p, 8, Color.white);
  162. return;
  163. }
  164. bool bolt = e.shape == Shape.Bolt;
  165. var tail = p - d * (bolt ? 38 : 24);
  166. Line(mesh, tail - d * 14, p, bolt ? 7 : 4, Alpha(e.tint, .27f));
  167. Line(mesh, tail, p, bolt ? 3.5f : 2.5f, e.tint);
  168. Triangle(mesh, p + d * 8, p - d * 5 + n * 4, p - d * 5 - n * 4, Color.white);
  169. Line(mesh, tail + d * 8, tail + n * 4, 2, e.tint);
  170. Line(mesh, tail + d * 8, tail - n * 4, 2, e.tint);
  171. }
  172. static Color Alpha(Color c, float alpha)
  173. {
  174. c.a = Mathf.Clamp01(alpha);
  175. return c;
  176. }
  177. static Vector2 Direction(float a) => new Vector2(Mathf.Cos(a), Mathf.Sin(a));
  178. static void Sparks(VertexHelper m, Vector2 p, float radius, float t, Color tint, int count)
  179. {
  180. for (int i = 0; i < count; i++)
  181. {
  182. Vector2 d = Direction(i * 2.39996f + .43f);
  183. float speed = .78f + (i % 4) * .21f;
  184. Vector2 at = p + d * radius * speed * (1 - (1 - t) * (1 - t)) + Vector2.down * (t * t * 12);
  185. Line(m, at - d * (1 - t) * 10, at, 1 + (1 - t) * 2, Alpha(tint, 1 - t));
  186. }
  187. }
  188. static void Star(VertexHelper m, Vector2 p, float r, Color c)
  189. {
  190. for (int i = 0; i < 4; i++)
  191. {
  192. var d = Direction(i * Mathf.PI * .5f);
  193. var n = new Vector2(-d.y, d.x) * r * .13f;
  194. Triangle(m, p + n, p + d * r, p - n, c);
  195. }
  196. }
  197. static void Triangle(VertexHelper m, Vector2 a, Vector2 b, Vector2 c, Color color)
  198. {
  199. int i = m.currentVertCount;
  200. m.AddVert(a, color, Vector2.zero);
  201. m.AddVert(b, color, Vector2.zero);
  202. m.AddVert(c, color, Vector2.zero);
  203. m.AddTriangle(i, i + 1, i + 2);
  204. }
  205. internal static void Line(VertexHelper m, Vector2 a, Vector2 b, float width, Color c)
  206. {
  207. var d = (b - a).normalized;
  208. var offset = new Vector2(-d.y, d.x) * width * .5f;
  209. int i = m.currentVertCount;
  210. m.AddVert(a + offset, c, Vector2.zero);
  211. m.AddVert(b + offset, c, Vector2.zero);
  212. m.AddVert(b - offset, c, Vector2.zero);
  213. m.AddVert(a - offset, c, Vector2.zero);
  214. m.AddTriangle(i, i + 1, i + 2);
  215. m.AddTriangle(i, i + 2, i + 3);
  216. }
  217. const int Segments = 24;
  218. static void Glow(VertexHelper m, Vector2 p, float size, Color c)
  219. {
  220. int i = m.currentVertCount;
  221. m.AddVert(p, c, Vector2.zero);
  222. for (int j = 0; j < Segments; j++)
  223. m.AddVert(p + Direction(j * 2 * Mathf.PI / Segments) * size, Alpha(c, 0), Vector2.zero);
  224. for (int j = 0; j < Segments; j++)
  225. m.AddTriangle(i, i + 1 + j, i + 1 + (j + 1) % Segments);
  226. }
  227. static void Ring(VertexHelper m, Vector2 p, float size, float width, Color c)
  228. {
  229. int i = m.currentVertCount;
  230. for (int j = 0; j < Segments; j++)
  231. {
  232. var d = Direction(j * 2 * Mathf.PI / Segments);
  233. m.AddVert(p + d * size, c, Vector2.zero);
  234. m.AddVert(p + d * Mathf.Max(0, size - width), c, Vector2.zero);
  235. }
  236. for (int j = 0; j < Segments; j++)
  237. {
  238. int a = i + 2 * j, b = i + 2 * ((j + 1) % Segments);
  239. m.AddTriangle(a, b, b + 1);
  240. m.AddTriangle(a, b + 1, a + 1);
  241. }
  242. }
  243. }
  244. }