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 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); 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; 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); } 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); var view = FoeVisual(foe); view.hitAt = motionTime + delay; if (foe.Hp <= 0) view.deadAt = view.hitAt; var label = Label(fx, damage > 0 ? damage.ToString() : "FROST", 0, 0, 80, 36, 20, kind == Kind.Frost ? SlowTint : Ink, TextAnchor.MiddleCenter); label.gameObject.AddComponent().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; } } }