CombatMotion.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace TowerDevil
  5. {
  6. public sealed partial class TowerGame
  7. {
  8. sealed class FoeView
  9. {
  10. internal RectTransform root, health;
  11. internal RawImage body;
  12. internal float phase, lastRow, hitAt = -1, deadAt = -1;
  13. }
  14. float motionTime, gateHitTime = -1;
  15. int nextMotionSeed;
  16. static readonly Color SlowTint = new Color32(154, 219, 255, 255), HurtTint = new Color32(255, 124, 100, 255);
  17. FoeView FoeVisual(Foe foe)
  18. {
  19. if (foes.TryGetValue(foe, out var view))
  20. return view;
  21. bool heavy = foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
  22. float size = foe.Boss ? 166 : heavy ? 101 : 86;
  23. var root = Box(monsters, foe.Breed + " movement", 0, 0, 0, 0);
  24. var shadow = Round(root, "Ground shadow", -size * .31f, -4, size * .62f, 11, "170E16", "170E16", 6);
  25. shadow.top = shadow.bottom = shadow.border = new Color(.045f, .025f, .045f, .45f);
  26. var im = Image(root, foe.Breed.ToString(), Foe.Art[(int)foe.Breed], 0, 0, size, size);
  27. im.rectTransform.pivot = new Vector2(.5f, 0);
  28. im.rectTransform.anchoredPosition = Vector2.zero;
  29. var track = Solid(root, "Health track", -size * .32f, -size - 7, size * .64f, 5, Hex("3B2424"));
  30. var health = Solid(track.transform, "Health", 0, 0, size * .64f, 5, Hex("E37A53"));
  31. view = new FoeView { root = root, body = im, health = health.rectTransform, lastRow = foe.Row,
  32. phase = nextMotionSeed++ * 2.4f };
  33. foes.Add(foe, view);
  34. return view;
  35. }
  36. void DrawFoes()
  37. {
  38. foreach (var foe in run.Enemies)
  39. if (foe.Row <= 7.2f && foe.Hp > 0)
  40. FoeVisual(foe);
  41. gone.Clear();
  42. foreach (var pair in foes)
  43. {
  44. var foe = pair.Key;
  45. var v = pair.Value;
  46. if ((!run.Enemies.Contains(foe) && v.deadAt < 0) || (v.deadAt >= 0 && motionTime > v.deadAt + .19f))
  47. {
  48. Remove(v.root);
  49. gone.Add(foe);
  50. continue;
  51. }
  52. float depth = BoardLayout.Size(foe.Row) / BoardLayout.FrontCell;
  53. v.root.anchoredPosition = Feet(foe);
  54. v.root.localScale = Vector3.one * depth;
  55. bool fly = foe.Breed == Breed.Succubus || foe.Breed == Breed.Broodmother;
  56. bool heavy = foe.Boss || foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
  57. // Drive the gait from travelled distance so a frost hit slows the body and its path together.
  58. v.phase += Mathf.Abs(v.lastRow - foe.Row) * (heavy ? 7.5f : 12);
  59. v.lastRow = foe.Row;
  60. float step = Mathf.Sin(v.phase);
  61. float hop = fly ? 8 + 5 * step : Mathf.Abs(step) * (heavy ? 4 : 8);
  62. float lunge = Mathf.InverseLerp(.55f, -.5f, foe.Row);
  63. var rt = v.body.rectTransform;
  64. rt.anchoredPosition = new Vector2(step * (fly ? 4 : 2), hop + 10 * lunge * lunge);
  65. rt.localRotation = Quaternion.Euler(0, 0, step * (heavy ? 4 : 7));
  66. rt.localScale = new Vector3(1 + .042f * step, 1 - .055f * step + .08f * lunge, 1);
  67. Color tint = foe.Slow > 0 ? SlowTint : Color.white;
  68. float since = motionTime - v.hitAt;
  69. if (v.hitAt >= 0 && since >= 0 && since < .16f)
  70. {
  71. float recoil = 1 - since / .16f;
  72. rt.anchoredPosition += Vector2.down * (7 * recoil);
  73. tint = Color.Lerp(tint, HurtTint, recoil);
  74. }
  75. if (v.deadAt >= 0 && motionTime >= v.deadAt)
  76. {
  77. float t = Mathf.Clamp01((motionTime - v.deadAt) / .19f);
  78. rt.localScale *= 1 - .42f * t;
  79. rt.localRotation *= Quaternion.Euler(0, 0, 18 * t);
  80. tint.a = 1 - t;
  81. }
  82. v.body.color = tint;
  83. float w = ((RectTransform)v.health.parent).sizeDelta.x;
  84. v.health.sizeDelta = new Vector2(w * Mathf.Clamp01((float)foe.Hp / foe.MaxHealth), 5);
  85. }
  86. foreach (var foe in gone)
  87. foes.Remove(foe);
  88. }
  89. Vector2 HitPoint(Foe foe) => Feet(foe) + Vector2.up * (foe.Boss ? 61 : 34) *
  90. (BoardLayout.Size(foe.Row) / BoardLayout.FrontCell);
  91. Vector2 Muzzle(int r, int c, Kind kind, Vector2 destination)
  92. {
  93. Vector2 basePoint = BoardLayout.Center(r, c);
  94. float offset = kind == Kind.Frost
  95. ? 0
  96. : Mathf.Min(BoardLayout.Size(r) * .32f, Vector2.Distance(destination, basePoint) * .48f);
  97. return basePoint + (destination - basePoint).normalized * offset;
  98. }
  99. void OnFire(int r, int c, Foe foe, Piece tower)
  100. {
  101. if (route != "Run")
  102. return;
  103. Vector2 end = HitPoint(run.ShotEnd ?? foe), start = Muzzle(r, c, tower.Kind, end);
  104. if (pieces.TryGetValue(tower, out var im))
  105. {
  106. SetPieceArt(im, tower, true);
  107. StartCoroutine(Kick(im.rectTransform, (end - start).normalized, tower.Kind));
  108. }
  109. battleEffects.Shoot(tower.Kind, start, end);
  110. if (!effects.isPlaying)
  111. Sound(shotSound);
  112. }
  113. IEnumerator Kick(RectTransform rt, Vector2 direction, Kind kind)
  114. {
  115. var rest = rt.anchoredPosition;
  116. var scale = rt.localScale;
  117. float begin = motionTime;
  118. while (rt && rt.gameObject.activeInHierarchy && motionTime - begin < .18f)
  119. {
  120. float t = Mathf.Clamp01((motionTime - begin) / .18f);
  121. float kick = Mathf.Sin(Mathf.Min(1, t * 5) * Mathf.PI * .5f) * (1 - t) * (1 - t);
  122. rt.anchoredPosition = rest - direction * kick * (kind == Kind.Cannon ? 13 : 7);
  123. rt.localScale = Vector3.Scale(scale, new Vector3(1 + .08f * kick, 1 - .09f * kick, 1));
  124. yield return null;
  125. }
  126. if (rt)
  127. {
  128. rt.anchoredPosition = rest;
  129. rt.localScale = scale;
  130. }
  131. }
  132. void OnHit(int r, int c, Foe foe, Kind kind, int damage)
  133. {
  134. if (route != "Run")
  135. return;
  136. var at = HitPoint(foe);
  137. var end = HitPoint(run.ShotEnd ?? foe);
  138. var start = Muzzle(r, c, kind, end);
  139. float distance = Vector2.Distance(start, end);
  140. float delay =
  141. BattleEffects.Flight(kind, distance) *
  142. (kind == Kind.Frost ? 1 : Mathf.Clamp01(Vector2.Distance(start, at) / Mathf.Max(1, distance)));
  143. battleEffects.Hit(kind, at, delay);
  144. var view = FoeVisual(foe);
  145. view.hitAt = motionTime + delay;
  146. if (foe.Hp <= 0)
  147. view.deadAt = view.hitAt;
  148. var label = Label(fx, damage > 0 ? damage.ToString() : "FROST", 0, 0, 80, 36, 20,
  149. kind == Kind.Frost ? SlowTint : Ink, TextAnchor.MiddleCenter);
  150. label.gameObject.AddComponent<Shadow>().effectDistance = new Vector2(1.5f, -1.5f);
  151. StartCoroutine(FloatHit(label, at, delay));
  152. }
  153. IEnumerator FloatHit(Text label, Vector2 at, float delay)
  154. {
  155. float begin = motionTime + delay;
  156. Color tint = label.color;
  157. label.enabled = false;
  158. while (label && label.gameObject.activeInHierarchy && motionTime < begin + .36f)
  159. {
  160. float t = (motionTime - begin) / .36f;
  161. if (t >= 0)
  162. {
  163. label.enabled = true;
  164. label.rectTransform.anchoredPosition = at + new Vector2(-40, 28 + 29 * t);
  165. label.color = new Color(tint.r, tint.g, tint.b, 1 - t * t);
  166. }
  167. yield return null;
  168. }
  169. if (label)
  170. Remove(label.rectTransform);
  171. }
  172. void OnGateHit(Foe foe)
  173. {
  174. if (route != "Run")
  175. return;
  176. var from = Feet(foe);
  177. Vector2 seal = boardStage.InverseTransformPoint(courtyard.rectTransform.TransformPoint(
  178. courtyard.Point(CourtyardBackdrop.SealU, CourtyardBackdrop.SealV)));
  179. var target = new Vector2(Mathf.Lerp(from.x, seal.x, .63f), seal.y - 20);
  180. if (foes.TryGetValue(foe, out var existing))
  181. {
  182. Remove(existing.root);
  183. foes.Remove(foe);
  184. }
  185. var body =
  186. Image(fx, "Gate attacker", Foe.Art[(int)foe.Breed], 0, 0, foe.Boss ? 146 : 82, foe.Boss ? 146 : 82);
  187. body.rectTransform.pivot = new Vector2(.5f, 0);
  188. body.rectTransform.anchoredPosition = from;
  189. gateHitTime = motionTime + .17f;
  190. battleEffects.Strike(target + Vector2.up * 19, foe.Boss, .17f);
  191. StartCoroutine(RamGate(body, from, target));
  192. }
  193. IEnumerator RamGate(RawImage body, Vector2 from, Vector2 target)
  194. {
  195. float begin = motionTime;
  196. bool struck = false;
  197. while (body && body.gameObject.activeInHierarchy && motionTime - begin < .40f)
  198. {
  199. float age = motionTime - begin;
  200. var rt = body.rectTransform;
  201. if (age < .17f)
  202. {
  203. float t = age / .17f;
  204. rt.anchoredPosition = Vector2.Lerp(from, target, t * t) + Vector2.up * 10 * Mathf.Sin(t * Mathf.PI);
  205. rt.localScale = new Vector3(1 - .12f * t, 1 + .17f * t, 1);
  206. rt.localRotation = Quaternion.Euler(0, 0, -Mathf.Sign(target.x - from.x) * 12 * t);
  207. }
  208. else
  209. {
  210. if (!struck)
  211. {
  212. struck = true;
  213. Sound(gateSound);
  214. #if UNITY_IOS && !UNITY_EDITOR
  215. if (profile.haptics)
  216. Handheld.Vibrate();
  217. #endif
  218. }
  219. float t = (age - .17f) / .23f;
  220. rt.anchoredPosition = target + Vector2.down * (20 * t);
  221. rt.localScale = Vector3.one * (1 - .35f * t);
  222. body.color = new Color(1, .7f, .58f, 1 - t);
  223. }
  224. yield return null;
  225. }
  226. if (body)
  227. Remove(body.rectTransform);
  228. }
  229. void ShakeGate()
  230. {
  231. float t = motionTime - gateHitTime;
  232. Vector2 shake = gateHitTime >= 0 && t >= 0 && t < .24f
  233. ? new Vector2(Mathf.Sin(77 * t) * 8, Mathf.Cos(94 * t) * 4) * (1 - t / .24f)
  234. : Vector2.zero;
  235. courtyard.MoveGate(shake);
  236. gateFractures.rectTransform.anchoredPosition = fractureOrigin + shake;
  237. }
  238. }
  239. }