using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace TowerDevil { public sealed partial class TowerGame { RectTransform held; Vector2 holdStart; Vector3 holdScale; internal void HoldPiece(int r, int c) { ReleasePiece(); if (!CanUseBoard || run.Board[r, c] == null || !pieces.TryGetValue(run.Board[r, c], out var im)) return; held = im.rectTransform; holdStart = held.anchoredPosition; holdScale = held.localScale; held.SetAsLastSibling(); DragPiece(Vector2.zero); } internal void DragPiece(Vector2 delta) { if (!held) return; held.anchoredPosition = holdStart + Vector2.up * 5 + Vector2.ClampMagnitude(delta, 24) * .75f; held.localScale = holdScale * 1.07f; held.localRotation = Quaternion.Euler(0, 0, Mathf.Clamp(-delta.x * .18f, -5, 5)); } internal void ReleasePiece() { if (held) { held.anchoredPosition = holdStart; held.localScale = holdScale; held.localRotation = Quaternion.identity; } held = null; if (pieceShadows) pieceShadows.SetVerticesDirty(); } static Color MaterialGlow(Kind k) { switch (k) { case Kind.Frost: case Kind.Ice: return new Color32(127, 228, 255, 255); case Kind.Arrow: case Kind.Crystal: return new Color32(225, 143, 255, 255); case Kind.Cannon: case Kind.Stone: return new Color32(255, 150, 77, 255); default: return new Color32(255, 209, 108, 255); } } struct Move { internal RectTransform tile; internal Vector2 from, to; internal Vector3 before, after; internal bool born; } IEnumerator AnimateBoard() { animating = true; while (frames.Count > 0 && route == "Run") { var step = frames.Dequeue(); if (step.MergeTargets != null) { yield return GatherPieces(step); continue; } var oldPos = new Dictionary(); var oldScale = new Dictionary(); foreach (var im in pieces.Values) { oldPos[im.rectTransform] = im.rectTransform.anchoredPosition; oldScale[im.rectTransform] = im.rectTransform.localScale; } DrawBoard(step.Cells, oldPos); var moves = new List(); bool forge = step.Cue == "forge", swap = step.Cue == "swap"; var arrivals = new int[Run.Size]; if (!forge && !swap) for (int c = 0; c < Run.Size; c++) for (int r = 0; r < Run.Size; r++) if (step.Cells[r,c] != null && !oldScale.ContainsKey(pieces[step.Cells[r,c]].rectTransform)) arrivals[c]++; for (int r = 0; r < Run.Size; r++) for (int c = 0; c < Run.Size; c++) { var piece = step.Cells[r, c]; if (piece == null) continue; var rt = pieces[piece].rectTransform; var end = rt.anchoredPosition; bool born = !oldScale.TryGetValue(rt, out var scale); var start = oldPos[rt]; if (born && arrivals[c] > 0) start = end + Vector2.up * arrivals[c] * BoardLayout.Depth(0); if (born && forge) { start = end; battleEffects.Forge(BoardLayout.Center(r, c), MaterialGlow(piece.Kind), BoardLayout.Size(r) * .51f); } if (!born && (end - start).sqrMagnitude < .01f) continue; moves.Add(new Move { tile = rt, from = start, to = end, before = born ? rt.localScale * (forge ? .2f : .86f) : scale, after = rt.localScale, born = born }); } if (moves.Count == 0) continue; float duration = swap ? .21f : forge ? .25f : .32f; float since = motionTime; while (motionTime - since < duration) { float t = Mathf.Clamp01((motionTime - since) / duration); foreach (var move in moves) { var delta = move.to - move.from; bool reveal = forge && move.born; float k = swap || reveal ? Mathf.SmoothStep(0, 1, t) : Mathf.Min(1, t * t * 1.55f); float arc = Mathf.Sin(t * Mathf.PI); Vector2 at = Vector2.Lerp(move.from, move.to, k); Vector3 scale = Vector3.Lerp(move.before, move.after, k); if (swap) { // Opposite offsets let both pieces be seen as their paths cross. at += new Vector2(-delta.y, delta.x).normalized *(arc * 13); move.tile.localRotation = Quaternion.Euler(0, 0, arc * Mathf.Clamp(-delta.x * .065f, -7, 7)); } else if (reveal) scale *= 1 + .25f * Mathf.Sin(t * Mathf.PI); else if (t > .8f) { float land = Mathf.Sin((t - .8f) * 5 * Mathf.PI); scale = Vector3.Scale(scale, new Vector3(1 + .085f * land, 1 - .12f * land, 1)); at += Vector2.up * 4 * land; } move.tile.anchoredPosition = at; move.tile.localScale = scale; } yield return null; } foreach (var move in moves) { move.tile.anchoredPosition = move.to; move.tile.localScale = move.after; move.tile.localRotation = Quaternion.identity; } } animating = false; pieceShadows.SetVerticesDirty(); playback = null; RefreshHud(); if (run.Phase == Phase.Plan && run.Moves == 0) nextAttack = .9f; } IEnumerator GatherPieces(BoardFrame step) { Sound(forgeSound); var moves = new List(); foreach (var pair in step.MergeTargets) { var from = pair.Key; var target = pair.Value; var piece = step.Cells[from.y, from.x]; var im = pieces[piece]; var rt = im.rectTransform; var sourceCenter = BoardLayout.Center(from.y, from.x); var destination = BoardLayout.Center(target.y, target.x); moves.Add(new Move { tile = rt, from = rt.anchoredPosition, to = destination - Vector2.up * 6, before = rt.localScale }); im.color = Color.Lerp(Color.white, MaterialGlow(piece.Kind), .25f); battleEffects.Gather(sourceCenter, destination, MaterialGlow(piece.Kind)); } float since = motionTime; while (motionTime - since < .27f) { float t = Mathf.Clamp01((motionTime - since) / .27f); foreach (var move in moves) { float pull = t * t; move.tile.anchoredPosition = Vector2.Lerp(move.from, move.to, pull) + Vector2.up * 8 * Mathf.Sin(t * Mathf.PI); move.tile.localScale = move.before * (1 + .18f * Mathf.Sin(t * Mathf.PI) - .82f * pull); } yield return null; } } } }