| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- namespace TowerDevil
- {
- public sealed partial class TowerGame
- {
- sealed class FoeView
- {
- internal RectTransform root, health;
- internal RawImage body;
- internal CanvasGroup iceShell;
- internal float chillAt;
- internal float phase, lastRow, hitAt = -1, deadAt = -1;
- }
- float motionTime, gateHitTime = -1;
- int nextMotionSeed;
- static readonly Color SlowTint = new Color32(154, 219, 255, 255), HurtTint = new Color32(255, 124, 100, 255);
- RectTransform battleAnnouncement;
- void OnWaveCleared()
- {
- if (route == "Run") StartCoroutine(AnnounceBattle(victory: true));
- }
- IEnumerator AnnounceBattle(bool victory)
- {
- Remove(battleAnnouncement);
- var banner = battleAnnouncement = Box(fx, victory ? "Wave victory" : "Defense begins", 0, 0, 360, 116);
- banner.pivot = new Vector2(.5f, .5f);
- var fade = banner.gameObject.AddComponent<CanvasGroup>();
- fade.blocksRaycasts = false;
- fade.interactable = false;
- if (victory)
- {
- KeepLayer(banner, "workshopPlate", 8, 41, 344, 72);
- DrawPart(banner, "crown", 148, 0, 64, 54, true);
- KeepText(banner, "VICTORY", 21, 46, 318, 58, 46).color = Hex("FFDC84");
- }
- else
- {
- KeepLayer(banner, "defendPlate", 12, 22, 336, 76);
- KeepLayer(banner, "defendCrest", 35, 34, 51, 53);
- KeepText(banner, "NIGHT " + run.Day, 88, 28, 237, 60, 43);
- }
- Sound(victory ? victoryChime : startHorn);
- float railY = victory ? 77 : 59;
- var left = Solid(banner, "Left flare", 10, railY, 0, 2, Gold).rectTransform;
- left.pivot = new Vector2(1, 1);
- var right = Solid(banner, "Right flare", 350, railY, 0, 2, Gold).rectTransform;
- float began = motionTime;
- float holdUntil = victory ? .97f : .78f;
- while (banner && banner.gameObject.activeInHierarchy && (victory || run.Phase == Phase.Assault))
- {
- float age = motionTime - began;
- if (age >= holdUntil + .28f) break;
- float enter = Mathf.SmoothStep(0, 1, age / (victory ? .24f : .15f));
- float leave = Mathf.SmoothStep(0, 1, (age - holdUntil) / .28f);
- fade.alpha = enter * (1 - leave);
- banner.localScale = Vector3.one * Mathf.Lerp(victory ? .86f : 1f, 1f, enter);
- banner.anchoredPosition = victory
- ? new Vector2(BoardLayout.Width * .5f, 155 - 18 * (1 - enter) + 23 * leave)
- : new Vector2(BoardLayout.Width * .5f - 62 * (1 - enter) + 24 * leave, 144);
- left.sizeDelta = right.sizeDelta = new Vector2(48 * enter * (1 - leave), 2);
- yield return null;
- }
- if (battleAnnouncement == banner)
- {
- Remove(banner);
- battleAnnouncement = null;
- }
- }
- FoeView FoeVisual(Foe foe)
- {
- if (foes.TryGetValue(foe, out var view))
- return view;
- bool heavy = foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
- float size = foe.Boss ? 166 : heavy ? 101 : 86;
- var root = Box(monsters, foe.Breed + " movement", 0, 0, 0, 0);
- var shadow = Round(root, "Ground shadow", -size * .31f, -4, size * .62f, 11, "170E16", "170E16", 6);
- shadow.top = shadow.bottom = shadow.border = new Color(.045f, .025f, .045f, .45f);
- var im = Image(root, foe.Breed.ToString(), Foe.Art[(int)foe.Breed], 0, 0, size, size);
- im.rectTransform.pivot = new Vector2(.5f, 0);
- im.rectTransform.anchoredPosition = Vector2.zero;
- var track = Solid(root, "Health track", -size * .32f, -size - 7, size * .64f, 5, Hex("3B2424"));
- var health = Solid(track.transform, "Health", 0, 0, size * .64f, 5, Hex("E37A53"));
- view = new FoeView { root = root, body = im, health = health.rectTransform, lastRow = foe.Row,
- phase = nextMotionSeed++ * 2.4f };
- foes.Add(foe, view);
- return view;
- }
- void DrawFoes()
- {
- foreach (var foe in run.Enemies)
- if (foe.Row <= 7.2f && foe.Hp > 0)
- FoeVisual(foe);
- gone.Clear();
- foreach (var pair in foes)
- {
- var foe = pair.Key;
- var v = pair.Value;
- if ((!run.Enemies.Contains(foe) && v.deadAt < 0) || (v.deadAt >= 0 && motionTime > v.deadAt + .19f))
- {
- Remove(v.root);
- gone.Add(foe);
- continue;
- }
- float depth = BoardLayout.Size(foe.Row) / BoardLayout.FrontCell;
- v.root.anchoredPosition = Feet(foe);
- v.root.localScale = Vector3.one * depth;
- UpdateIceShell(v, foe);
- bool fly = foe.Breed == Breed.Succubus || foe.Breed == Breed.Broodmother;
- bool heavy = foe.Boss || foe.Breed == Breed.Armor || foe.Breed == Breed.Brute;
- // Drive the gait from travelled distance so a frost hit slows the body and its path together.
- v.phase += Mathf.Abs(v.lastRow - foe.Row) * (heavy ? 7.5f : 12);
- v.lastRow = foe.Row;
- float step = Mathf.Sin(v.phase);
- float hop = fly ? 8 + 5 * step : Mathf.Abs(step) * (heavy ? 4 : 8);
- float lunge = Mathf.InverseLerp(.55f, -.5f, foe.Row);
- var rt = v.body.rectTransform;
- rt.anchoredPosition = new Vector2(step * (fly ? 4 : 2), hop + 10 * lunge * lunge);
- rt.localRotation = Quaternion.Euler(0, 0, step * (heavy ? 4 : 7));
- rt.localScale = new Vector3(1 + .042f * step, 1 - .055f * step + .08f * lunge, 1);
- Color tint = foe.Slow > 0 ? SlowTint : Color.white;
- float since = motionTime - v.hitAt;
- if (v.hitAt >= 0 && since >= 0 && since < .16f)
- {
- float recoil = 1 - since / .16f;
- rt.anchoredPosition += Vector2.down * (7 * recoil);
- tint = Color.Lerp(tint, HurtTint, recoil);
- }
- if (v.deadAt >= 0 && motionTime >= v.deadAt)
- {
- float t = Mathf.Clamp01((motionTime - v.deadAt) / .19f);
- rt.localScale *= 1 - .42f * t;
- rt.localRotation *= Quaternion.Euler(0, 0, 18 * t);
- tint.a = 1 - t;
- }
- v.body.color = tint;
- float w = ((RectTransform)v.health.parent).sizeDelta.x;
- v.health.sizeDelta = new Vector2(w * Mathf.Clamp01((float)foe.Hp / foe.MaxHealth), 5);
- }
- foreach (var foe in gone)
- foes.Remove(foe);
- }
- void UpdateIceShell(FoeView view, Foe foe)
- {
- bool chilled = foe.Slow > 0 && foe.Hp > 0;
- if (!chilled)
- {
- if (view.iceShell && view.iceShell.gameObject.activeSelf) view.iceShell.gameObject.SetActive(false);
- return;
- }
- if (!view.iceShell)
- {
- float size = Mathf.Max(view.body.rectTransform.rect.width, view.body.rectTransform.rect.height);
- float diameter = size * 1.18f;
- var shell = Box(view.root, "Frost shell", 0, -size * .49f, diameter, diameter);
- shell.pivot = new Vector2(.5f, .5f);
- shell.SetSiblingIndex(view.body.transform.GetSiblingIndex() + 1);
- view.iceShell = shell.gameObject.AddComponent<CanvasGroup>();
- view.iceShell.blocksRaycasts = false;
- var halo = shell.gameObject.AddComponent<BattleHalo>();
- halo.color = Hex("8DE8FF");
- halo.raycastTarget = false;
- Image(shell, "Ice crystals", "frost_shell", diameter * .06f, diameter * .61f,
- diameter * .88f, diameter * .34f);
- view.chillAt = motionTime;
- }
- else if (!view.iceShell.gameObject.activeSelf)
- {
- view.iceShell.gameObject.SetActive(true);
- view.chillAt = motionTime;
- }
- float strength = Mathf.SmoothStep(0, 1, Mathf.Min((motionTime - view.chillAt) / .11f, foe.Slow / .22f));
- view.iceShell.alpha = strength;
- view.iceShell.transform.localScale = Vector3.one * Mathf.Lerp(.83f, 1, strength);
- }
- Vector2 HitPoint(Foe foe) => Feet(foe) + Vector2.up * (foe.Boss ? 61 : 34) *
- (BoardLayout.Size(foe.Row) / BoardLayout.FrontCell);
- Vector2 Muzzle(int r, int c, Kind kind, Vector2 destination)
- {
- Vector2 basePoint = BoardLayout.Center(r, c);
- float offset = kind == Kind.Frost
- ? 0
- : Mathf.Min(BoardLayout.Size(r) * .32f, Vector2.Distance(destination, basePoint) * .48f);
- return basePoint + (destination - basePoint).normalized * offset;
- }
- void OnFire(int r, int c, Foe foe, Piece tower)
- {
- if (route != "Run")
- return;
- Vector2 end = HitPoint(run.ShotEnd ?? foe), start = Muzzle(r, c, tower.Kind, end);
- if (pieces.TryGetValue(tower, out var im))
- {
- SetPieceArt(im, tower, true);
- StartCoroutine(Kick(im.rectTransform, (end - start).normalized, tower.Kind));
- }
- battleEffects.Shoot(tower.Kind, start, end);
- if (!effects.isPlaying)
- Sound(shotSound);
- }
- IEnumerator Kick(RectTransform rt, Vector2 direction, Kind kind)
- {
- var rest = rt.anchoredPosition;
- var scale = rt.localScale;
- float begin = motionTime;
- while (rt && rt.gameObject.activeInHierarchy && motionTime - begin < .18f)
- {
- float t = Mathf.Clamp01((motionTime - begin) / .18f);
- float kick = Mathf.Sin(Mathf.Min(1, t * 5) * Mathf.PI * .5f) * (1 - t) * (1 - t);
- rt.anchoredPosition = rest - direction * kick * (kind == Kind.Cannon ? 13 : 7);
- rt.localScale = Vector3.Scale(scale, new Vector3(1 + .08f * kick, 1 - .09f * kick, 1));
- yield return null;
- }
- if (rt)
- {
- rt.anchoredPosition = rest;
- rt.localScale = scale;
- }
- }
- void OnHit(int r, int c, Foe foe, Kind kind, int damage)
- {
- if (route != "Run")
- return;
- var at = HitPoint(foe);
- var end = HitPoint(run.ShotEnd ?? foe);
- var start = Muzzle(r, c, kind, end);
- float distance = Vector2.Distance(start, end);
- float delay =
- BattleEffects.Flight(kind, distance) *
- (kind == Kind.Frost ? 1 : Mathf.Clamp01(Vector2.Distance(start, at) / Mathf.Max(1, distance)));
- battleEffects.Hit(kind, at, delay);
- if (kind == Kind.Frost) return;
- var view = FoeVisual(foe);
- view.hitAt = motionTime + delay;
- if (foe.Hp <= 0)
- view.deadAt = view.hitAt;
- var label = Label(fx, damage.ToString(), 0, 0, 80, 36, 20, Ink, TextAnchor.MiddleCenter);
- label.gameObject.AddComponent<Shadow>().effectDistance = new Vector2(1.5f, -1.5f);
- StartCoroutine(FloatHit(label, at, delay));
- }
- IEnumerator FloatHit(Text label, Vector2 at, float delay)
- {
- float begin = motionTime + delay;
- Color tint = label.color;
- label.enabled = false;
- while (label && label.gameObject.activeInHierarchy && motionTime < begin + .36f)
- {
- float t = (motionTime - begin) / .36f;
- if (t >= 0)
- {
- label.enabled = true;
- label.rectTransform.anchoredPosition = at + new Vector2(-40, 28 + 29 * t);
- label.color = new Color(tint.r, tint.g, tint.b, 1 - t * t);
- }
- yield return null;
- }
- if (label)
- Remove(label.rectTransform);
- }
- void OnGateHit(Foe foe)
- {
- if (route != "Run")
- return;
- var from = Feet(foe);
- Vector2 seal = boardStage.InverseTransformPoint(courtyard.rectTransform.TransformPoint(
- courtyard.Point(CourtyardBackdrop.SealU, CourtyardBackdrop.SealV)));
- var target = new Vector2(Mathf.Lerp(from.x, seal.x, .63f), seal.y - 20);
- if (foes.TryGetValue(foe, out var existing))
- {
- Remove(existing.root);
- foes.Remove(foe);
- }
- var body =
- Image(fx, "Gate attacker", Foe.Art[(int)foe.Breed], 0, 0, foe.Boss ? 146 : 82, foe.Boss ? 146 : 82);
- body.rectTransform.pivot = new Vector2(.5f, 0);
- body.rectTransform.anchoredPosition = from;
- gateHitTime = motionTime + .17f;
- battleEffects.Strike(target + Vector2.up * 19, foe.Boss, .17f);
- StartCoroutine(RamGate(body, from, target));
- }
- IEnumerator RamGate(RawImage body, Vector2 from, Vector2 target)
- {
- float begin = motionTime;
- bool struck = false;
- while (body && body.gameObject.activeInHierarchy && motionTime - begin < .40f)
- {
- float age = motionTime - begin;
- var rt = body.rectTransform;
- if (age < .17f)
- {
- float t = age / .17f;
- rt.anchoredPosition = Vector2.Lerp(from, target, t * t) + Vector2.up * 10 * Mathf.Sin(t * Mathf.PI);
- rt.localScale = new Vector3(1 - .12f * t, 1 + .17f * t, 1);
- rt.localRotation = Quaternion.Euler(0, 0, -Mathf.Sign(target.x - from.x) * 12 * t);
- }
- else
- {
- if (!struck)
- {
- struck = true;
- Sound(gateSound);
- #if UNITY_IOS && !UNITY_EDITOR
- if (profile.haptics)
- Handheld.Vibrate();
- #endif
- }
- float t = (age - .17f) / .23f;
- rt.anchoredPosition = target + Vector2.down * (20 * t);
- rt.localScale = Vector3.one * (1 - .35f * t);
- body.color = new Color(1, .7f, .58f, 1 - t);
- }
- yield return null;
- }
- if (body)
- Remove(body.rectTransform);
- }
- void ShakeGate()
- {
- float t = motionTime - gateHitTime;
- Vector2 shake = gateHitTime >= 0 && t >= 0 && t < .24f
- ? new Vector2(Mathf.Sin(77 * t) * 8, Mathf.Cos(94 * t) * 4) * (1 - t / .24f)
- : Vector2.zero;
- courtyard.MoveGate(shake);
- gateFractures.rectTransform.anchoredPosition = fractureOrigin + shake;
- }
- }
- }
|