CombatMotion.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. im.texture = Art(tower.Art);
  107. im.uvRect = tower.Kind == Kind.Cannon && tower.Reversed ? new Rect(1, 0, -1, 1) : new Rect(0, 0, 1, 1);
  108. StartCoroutine(Kick(im.rectTransform, (end - start).normalized, tower.Kind));
  109. }
  110. battleEffects.Shoot(tower.Kind, start, end);
  111. if (!effects.isPlaying)
  112. Sound(shotSound);
  113. }
  114. IEnumerator Kick(RectTransform rt, Vector2 direction, Kind kind)
  115. {
  116. var rest = rt.anchoredPosition;
  117. var scale = rt.localScale;
  118. float begin = motionTime;
  119. while (rt && rt.gameObject.activeInHierarchy && motionTime - begin < .18f)
  120. {
  121. float t = Mathf.Clamp01((motionTime - begin) / .18f);
  122. float kick = Mathf.Sin(Mathf.Min(1, t * 5) * Mathf.PI * .5f) * (1 - t) * (1 - t);
  123. rt.anchoredPosition = rest - direction * kick * (kind == Kind.Cannon ? 13 : 7);
  124. rt.localScale = Vector3.Scale(scale, new Vector3(1 + .08f * kick, 1 - .09f * kick, 1));
  125. yield return null;
  126. }
  127. if (rt)
  128. {
  129. rt.anchoredPosition = rest;
  130. rt.localScale = scale;
  131. }
  132. }
  133. void OnHit(int r, int c, Foe foe, Kind kind, int damage)
  134. {
  135. if (route != "Run")
  136. return;
  137. var at = HitPoint(foe);
  138. var end = HitPoint(run.ShotEnd ?? foe);
  139. var start = Muzzle(r, c, kind, end);
  140. float distance = Vector2.Distance(start, end);
  141. float delay =
  142. BattleEffects.Flight(kind, distance) *
  143. (kind == Kind.Frost ? 1 : Mathf.Clamp01(Vector2.Distance(start, at) / Mathf.Max(1, distance)));
  144. battleEffects.Hit(kind, at, delay);
  145. var view = FoeVisual(foe);
  146. view.hitAt = motionTime + delay;
  147. if (foe.Hp <= 0)
  148. view.deadAt = view.hitAt;
  149. var label = Label(fx, damage > 0 ? damage.ToString() : "FROST", 0, 0, 80, 36, 20,
  150. kind == Kind.Frost ? SlowTint : Ink, TextAnchor.MiddleCenter);
  151. label.gameObject.AddComponent<Shadow>().effectDistance = new Vector2(1.5f, -1.5f);
  152. StartCoroutine(FloatHit(label, at, delay));
  153. }
  154. IEnumerator FloatHit(Text label, Vector2 at, float delay)
  155. {
  156. float begin = motionTime + delay;
  157. Color tint = label.color;
  158. label.enabled = false;
  159. while (label && label.gameObject.activeInHierarchy && motionTime < begin + .36f)
  160. {
  161. float t = (motionTime - begin) / .36f;
  162. if (t >= 0)
  163. {
  164. label.enabled = true;
  165. label.rectTransform.anchoredPosition = at + new Vector2(-40, 28 + 29 * t);
  166. label.color = new Color(tint.r, tint.g, tint.b, 1 - t * t);
  167. }
  168. yield return null;
  169. }
  170. if (label)
  171. Remove(label.rectTransform);
  172. }
  173. void OnGateHit(Foe foe)
  174. {
  175. if (route != "Run")
  176. return;
  177. var from = Feet(foe);
  178. var target = new Vector2(Mathf.Lerp(from.x, 375, .56f), 124);
  179. if (foes.TryGetValue(foe, out var existing))
  180. {
  181. Remove(existing.root);
  182. foes.Remove(foe);
  183. }
  184. var body =
  185. Image(fx, "Gate attacker", Foe.Art[(int)foe.Breed], 0, 0, foe.Boss ? 146 : 82, foe.Boss ? 146 : 82);
  186. body.rectTransform.pivot = new Vector2(.5f, 0);
  187. body.rectTransform.anchoredPosition = from;
  188. gateHitTime = motionTime + .17f;
  189. battleEffects.Strike(target + Vector2.up * 19, foe.Boss, .17f);
  190. StartCoroutine(RamGate(body, from, target));
  191. }
  192. IEnumerator RamGate(RawImage body, Vector2 from, Vector2 target)
  193. {
  194. float begin = motionTime;
  195. bool struck = false;
  196. while (body && body.gameObject.activeInHierarchy && motionTime - begin < .40f)
  197. {
  198. float age = motionTime - begin;
  199. var rt = body.rectTransform;
  200. if (age < .17f)
  201. {
  202. float t = age / .17f;
  203. rt.anchoredPosition = Vector2.Lerp(from, target, t * t) + Vector2.up * 10 * Mathf.Sin(t * Mathf.PI);
  204. rt.localScale = new Vector3(1 - .12f * t, 1 + .17f * t, 1);
  205. rt.localRotation = Quaternion.Euler(0, 0, -Mathf.Sign(target.x - from.x) * 12 * t);
  206. }
  207. else
  208. {
  209. if (!struck)
  210. {
  211. struck = true;
  212. Sound(gateSound);
  213. #if UNITY_IOS && !UNITY_EDITOR
  214. if (profile.haptics)
  215. Handheld.Vibrate();
  216. #endif
  217. }
  218. float t = (age - .17f) / .23f;
  219. rt.anchoredPosition = target + Vector2.down * (20 * t);
  220. rt.localScale = Vector3.one * (1 - .35f * t);
  221. body.color = new Color(1, .7f, .58f, 1 - t);
  222. }
  223. yield return null;
  224. }
  225. if (body)
  226. Remove(body.rectTransform);
  227. }
  228. void ShakeGate()
  229. {
  230. float t = motionTime - gateHitTime;
  231. Vector2 shake = gateHitTime >= 0 && t >= 0 && t < .24f
  232. ? new Vector2(Mathf.Sin(77 * t) * 8, Mathf.Cos(94 * t) * 4) * (1 - t / .24f)
  233. : Vector2.zero;
  234. gateArt.rectTransform.anchoredPosition = shake;
  235. }
  236. }
  237. }