CombatMotion.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 CanvasGroup iceShell;
  13. internal float chillAt;
  14. internal float phase, lastRow, hitAt = -1, deadAt = -1;
  15. }
  16. float motionTime, gateHitTime = -1;
  17. int nextMotionSeed;
  18. static readonly Color SlowTint = new Color32(154, 219, 255, 255), HurtTint = new Color32(255, 124, 100, 255);
  19. RectTransform battleAnnouncement;
  20. void OnWaveCleared()
  21. {
  22. if (route == "Run") StartCoroutine(AnnounceBattle(victory: true));
  23. }
  24. IEnumerator AnnounceBattle(bool victory)
  25. {
  26. Remove(battleAnnouncement);
  27. var banner = battleAnnouncement = Box(fx, victory ? "Wave victory" : "Defense begins", 0, 0, 360, 116);
  28. banner.pivot = new Vector2(.5f, .5f);
  29. var fade = banner.gameObject.AddComponent<CanvasGroup>();
  30. fade.blocksRaycasts = false;
  31. fade.interactable = false;
  32. if (victory)
  33. {
  34. KeepLayer(banner, "workshopPlate", 8, 41, 344, 72);
  35. DrawPart(banner, "crown", 148, 0, 64, 54, true);
  36. KeepText(banner, "VICTORY", 21, 46, 318, 58, 46).color = Hex("FFDC84");
  37. }
  38. else
  39. {
  40. KeepLayer(banner, "defendPlate", 12, 22, 336, 76);
  41. KeepLayer(banner, "defendCrest", 35, 34, 51, 53);
  42. KeepText(banner, "NIGHT " + run.Day, 88, 28, 237, 60, 43);
  43. }
  44. Sound(victory ? victoryChime : startHorn);
  45. float railY = victory ? 77 : 59;
  46. var left = Solid(banner, "Left flare", 10, railY, 0, 2, Gold).rectTransform;
  47. left.pivot = new Vector2(1, 1);
  48. var right = Solid(banner, "Right flare", 350, railY, 0, 2, Gold).rectTransform;
  49. float began = motionTime;
  50. float holdUntil = victory ? .97f : .78f;
  51. while (banner && banner.gameObject.activeInHierarchy && (victory || run.Phase == Phase.Assault))
  52. {
  53. float age = motionTime - began;
  54. if (age >= holdUntil + .28f) break;
  55. float enter = Mathf.SmoothStep(0, 1, age / (victory ? .24f : .15f));
  56. float leave = Mathf.SmoothStep(0, 1, (age - holdUntil) / .28f);
  57. fade.alpha = enter * (1 - leave);
  58. banner.localScale = Vector3.one * Mathf.Lerp(victory ? .86f : 1f, 1f, enter);
  59. banner.anchoredPosition = victory
  60. ? new Vector2(BoardLayout.Width * .5f, 155 - 18 * (1 - enter) + 23 * leave)
  61. : new Vector2(BoardLayout.Width * .5f - 62 * (1 - enter) + 24 * leave, 144);
  62. left.sizeDelta = right.sizeDelta = new Vector2(48 * enter * (1 - leave), 2);
  63. yield return null;
  64. }
  65. if (battleAnnouncement == banner)
  66. {
  67. Remove(banner);
  68. battleAnnouncement = null;
  69. }
  70. }
  71. FoeView FoeVisual(Foe foe)
  72. {
  73. if (foes.TryGetValue(foe, out var view))
  74. return view;
  75. bool heavy = foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
  76. float size = foe.Boss ? 166 : heavy ? 101 : 86;
  77. var root = Box(monsters, foe.Breed + " movement", 0, 0, 0, 0);
  78. var shadow = Round(root, "Ground shadow", -size * .31f, -4, size * .62f, 11, "170E16", "170E16", 6);
  79. shadow.top = shadow.bottom = shadow.border = new Color(.045f, .025f, .045f, .45f);
  80. var im = Image(root, foe.Breed.ToString(), Foe.Art[(int)foe.Breed], 0, 0, size, size);
  81. im.rectTransform.pivot = new Vector2(.5f, 0);
  82. im.rectTransform.anchoredPosition = Vector2.zero;
  83. var track = Solid(root, "Health track", -size * .32f, -size - 7, size * .64f, 5, Hex("3B2424"));
  84. var health = Solid(track.transform, "Health", 0, 0, size * .64f, 5, Hex("E37A53"));
  85. view = new FoeView { root = root, body = im, health = health.rectTransform, lastRow = foe.Row,
  86. phase = nextMotionSeed++ * 2.4f };
  87. foes.Add(foe, view);
  88. return view;
  89. }
  90. void DrawFoes()
  91. {
  92. foreach (var foe in run.Enemies)
  93. if (foe.Row <= 7.2f && foe.Hp > 0)
  94. FoeVisual(foe);
  95. gone.Clear();
  96. foreach (var pair in foes)
  97. {
  98. var foe = pair.Key;
  99. var v = pair.Value;
  100. if ((!run.Enemies.Contains(foe) && v.deadAt < 0) || (v.deadAt >= 0 && motionTime > v.deadAt + .19f))
  101. {
  102. Remove(v.root);
  103. gone.Add(foe);
  104. continue;
  105. }
  106. float depth = BoardLayout.Size(foe.Row) / BoardLayout.FrontCell;
  107. v.root.anchoredPosition = Feet(foe);
  108. v.root.localScale = Vector3.one * depth;
  109. UpdateIceShell(v, foe);
  110. bool fly = foe.Breed == Breed.Succubus || foe.Breed == Breed.Broodmother;
  111. bool heavy = foe.Boss || foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
  112. // Drive the gait from travelled distance so a frost hit slows the body and its path together.
  113. v.phase += Mathf.Abs(v.lastRow - foe.Row) * (heavy ? 7.5f : 12);
  114. v.lastRow = foe.Row;
  115. float step = Mathf.Sin(v.phase);
  116. float hop = fly ? 8 + 5 * step : Mathf.Abs(step) * (heavy ? 4 : 8);
  117. float lunge = Mathf.InverseLerp(.55f, -.5f, foe.Row);
  118. var rt = v.body.rectTransform;
  119. rt.anchoredPosition = new Vector2(step * (fly ? 4 : 2), hop + 10 * lunge * lunge);
  120. rt.localRotation = Quaternion.Euler(0, 0, step * (heavy ? 4 : 7));
  121. rt.localScale = new Vector3(1 + .042f * step, 1 - .055f * step + .08f * lunge, 1);
  122. Color tint = foe.Slow > 0 ? SlowTint : Color.white;
  123. float since = motionTime - v.hitAt;
  124. if (v.hitAt >= 0 && since >= 0 && since < .16f)
  125. {
  126. float recoil = 1 - since / .16f;
  127. rt.anchoredPosition += Vector2.down * (7 * recoil);
  128. tint = Color.Lerp(tint, HurtTint, recoil);
  129. }
  130. if (v.deadAt >= 0 && motionTime >= v.deadAt)
  131. {
  132. float t = Mathf.Clamp01((motionTime - v.deadAt) / .19f);
  133. rt.localScale *= 1 - .42f * t;
  134. rt.localRotation *= Quaternion.Euler(0, 0, 18 * t);
  135. tint.a = 1 - t;
  136. }
  137. v.body.color = tint;
  138. float w = ((RectTransform)v.health.parent).sizeDelta.x;
  139. v.health.sizeDelta = new Vector2(w * Mathf.Clamp01((float)foe.Hp / foe.MaxHealth), 5);
  140. }
  141. foreach (var foe in gone)
  142. foes.Remove(foe);
  143. }
  144. void UpdateIceShell(FoeView view, Foe foe)
  145. {
  146. bool chilled = foe.Slow > 0 && foe.Hp > 0;
  147. if (!chilled)
  148. {
  149. if (view.iceShell && view.iceShell.gameObject.activeSelf) view.iceShell.gameObject.SetActive(false);
  150. return;
  151. }
  152. if (!view.iceShell)
  153. {
  154. float size = Mathf.Max(view.body.rectTransform.rect.width, view.body.rectTransform.rect.height);
  155. float diameter = size * 1.18f;
  156. var shell = Box(view.root, "Frost shell", 0, -size * .49f, diameter, diameter);
  157. shell.pivot = new Vector2(.5f, .5f);
  158. shell.SetSiblingIndex(view.body.transform.GetSiblingIndex() + 1);
  159. view.iceShell = shell.gameObject.AddComponent<CanvasGroup>();
  160. view.iceShell.blocksRaycasts = false;
  161. var halo = shell.gameObject.AddComponent<BattleHalo>();
  162. halo.color = Hex("8DE8FF");
  163. halo.raycastTarget = false;
  164. Image(shell, "Ice crystals", "frost_shell", diameter * .06f, diameter * .61f,
  165. diameter * .88f, diameter * .34f);
  166. view.chillAt = motionTime;
  167. }
  168. else if (!view.iceShell.gameObject.activeSelf)
  169. {
  170. view.iceShell.gameObject.SetActive(true);
  171. view.chillAt = motionTime;
  172. }
  173. float strength = Mathf.SmoothStep(0, 1, Mathf.Min((motionTime - view.chillAt) / .11f, foe.Slow / .22f));
  174. view.iceShell.alpha = strength;
  175. view.iceShell.transform.localScale = Vector3.one * Mathf.Lerp(.83f, 1, strength);
  176. }
  177. Vector2 HitPoint(Foe foe) => Feet(foe) + Vector2.up * (foe.Boss ? 61 : 34) *
  178. (BoardLayout.Size(foe.Row) / BoardLayout.FrontCell);
  179. Vector2 Muzzle(int r, int c, Kind kind, Vector2 destination)
  180. {
  181. Vector2 basePoint = BoardLayout.Center(r, c);
  182. float offset = kind == Kind.Frost
  183. ? 0
  184. : Mathf.Min(BoardLayout.Size(r) * .32f, Vector2.Distance(destination, basePoint) * .48f);
  185. return basePoint + (destination - basePoint).normalized * offset;
  186. }
  187. void OnFire(int r, int c, Foe foe, Piece tower)
  188. {
  189. if (route != "Run")
  190. return;
  191. Vector2 end = HitPoint(run.ShotEnd ?? foe), start = Muzzle(r, c, tower.Kind, end);
  192. if (pieces.TryGetValue(tower, out var im))
  193. {
  194. SetPieceArt(im, tower, true);
  195. StartCoroutine(Kick(im.rectTransform, (end - start).normalized, tower.Kind));
  196. }
  197. battleEffects.Shoot(tower.Kind, start, end);
  198. if (!effects.isPlaying)
  199. Sound(shotSound);
  200. }
  201. IEnumerator Kick(RectTransform rt, Vector2 direction, Kind kind)
  202. {
  203. var rest = rt.anchoredPosition;
  204. var scale = rt.localScale;
  205. float begin = motionTime;
  206. while (rt && rt.gameObject.activeInHierarchy && motionTime - begin < .18f)
  207. {
  208. float t = Mathf.Clamp01((motionTime - begin) / .18f);
  209. float kick = Mathf.Sin(Mathf.Min(1, t * 5) * Mathf.PI * .5f) * (1 - t) * (1 - t);
  210. rt.anchoredPosition = rest - direction * kick * (kind == Kind.Cannon ? 13 : 7);
  211. rt.localScale = Vector3.Scale(scale, new Vector3(1 + .08f * kick, 1 - .09f * kick, 1));
  212. yield return null;
  213. }
  214. if (rt)
  215. {
  216. rt.anchoredPosition = rest;
  217. rt.localScale = scale;
  218. }
  219. }
  220. void OnHit(int r, int c, Foe foe, Kind kind, int damage)
  221. {
  222. if (route != "Run")
  223. return;
  224. var at = HitPoint(foe);
  225. var end = HitPoint(run.ShotEnd ?? foe);
  226. var start = Muzzle(r, c, kind, end);
  227. float distance = Vector2.Distance(start, end);
  228. float delay =
  229. BattleEffects.Flight(kind, distance) *
  230. (kind == Kind.Frost ? 1 : Mathf.Clamp01(Vector2.Distance(start, at) / Mathf.Max(1, distance)));
  231. battleEffects.Hit(kind, at, delay);
  232. if (kind == Kind.Frost) return;
  233. var view = FoeVisual(foe);
  234. view.hitAt = motionTime + delay;
  235. if (foe.Hp <= 0)
  236. view.deadAt = view.hitAt;
  237. var label = Label(fx, damage.ToString(), 0, 0, 80, 36, 20, Ink, TextAnchor.MiddleCenter);
  238. label.gameObject.AddComponent<Shadow>().effectDistance = new Vector2(1.5f, -1.5f);
  239. StartCoroutine(FloatHit(label, at, delay));
  240. }
  241. IEnumerator FloatHit(Text label, Vector2 at, float delay)
  242. {
  243. float begin = motionTime + delay;
  244. Color tint = label.color;
  245. label.enabled = false;
  246. while (label && label.gameObject.activeInHierarchy && motionTime < begin + .36f)
  247. {
  248. float t = (motionTime - begin) / .36f;
  249. if (t >= 0)
  250. {
  251. label.enabled = true;
  252. label.rectTransform.anchoredPosition = at + new Vector2(-40, 28 + 29 * t);
  253. label.color = new Color(tint.r, tint.g, tint.b, 1 - t * t);
  254. }
  255. yield return null;
  256. }
  257. if (label)
  258. Remove(label.rectTransform);
  259. }
  260. void OnGateHit(Foe foe)
  261. {
  262. if (route != "Run")
  263. return;
  264. var from = Feet(foe);
  265. Vector2 seal = boardStage.InverseTransformPoint(courtyard.rectTransform.TransformPoint(
  266. courtyard.Point(CourtyardBackdrop.SealU, CourtyardBackdrop.SealV)));
  267. var target = new Vector2(Mathf.Lerp(from.x, seal.x, .63f), seal.y - 20);
  268. if (foes.TryGetValue(foe, out var existing))
  269. {
  270. Remove(existing.root);
  271. foes.Remove(foe);
  272. }
  273. var body =
  274. Image(fx, "Gate attacker", Foe.Art[(int)foe.Breed], 0, 0, foe.Boss ? 146 : 82, foe.Boss ? 146 : 82);
  275. body.rectTransform.pivot = new Vector2(.5f, 0);
  276. body.rectTransform.anchoredPosition = from;
  277. gateHitTime = motionTime + .17f;
  278. battleEffects.Strike(target + Vector2.up * 19, foe.Boss, .17f);
  279. StartCoroutine(RamGate(body, from, target));
  280. }
  281. IEnumerator RamGate(RawImage body, Vector2 from, Vector2 target)
  282. {
  283. float begin = motionTime;
  284. bool struck = false;
  285. while (body && body.gameObject.activeInHierarchy && motionTime - begin < .40f)
  286. {
  287. float age = motionTime - begin;
  288. var rt = body.rectTransform;
  289. if (age < .17f)
  290. {
  291. float t = age / .17f;
  292. rt.anchoredPosition = Vector2.Lerp(from, target, t * t) + Vector2.up * 10 * Mathf.Sin(t * Mathf.PI);
  293. rt.localScale = new Vector3(1 - .12f * t, 1 + .17f * t, 1);
  294. rt.localRotation = Quaternion.Euler(0, 0, -Mathf.Sign(target.x - from.x) * 12 * t);
  295. }
  296. else
  297. {
  298. if (!struck)
  299. {
  300. struck = true;
  301. Sound(gateSound);
  302. #if UNITY_IOS && !UNITY_EDITOR
  303. if (profile.haptics)
  304. Handheld.Vibrate();
  305. #endif
  306. }
  307. float t = (age - .17f) / .23f;
  308. rt.anchoredPosition = target + Vector2.down * (20 * t);
  309. rt.localScale = Vector3.one * (1 - .35f * t);
  310. body.color = new Color(1, .7f, .58f, 1 - t);
  311. }
  312. yield return null;
  313. }
  314. if (body)
  315. Remove(body.rectTransform);
  316. }
  317. void ShakeGate()
  318. {
  319. float t = motionTime - gateHitTime;
  320. Vector2 shake = gateHitTime >= 0 && t >= 0 && t < .24f
  321. ? new Vector2(Mathf.Sin(77 * t) * 8, Mathf.Cos(94 * t) * 4) * (1 - t / .24f)
  322. : Vector2.zero;
  323. courtyard.MoveGate(shake);
  324. gateFractures.rectTransform.anchoredPosition = fractureOrigin + shake;
  325. }
  326. }
  327. }