TileView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections;
  3. using DG.Tweening;
  4. using UnityEngine.EventSystems;
  5. using DevilLink2.Core;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. namespace DevilLink2.Presentation
  9. {
  10. [DisallowMultipleComponent]
  11. public sealed class TileView : MonoBehaviour, IPointerClickHandler
  12. {
  13. [SerializeField] private Image face;
  14. [SerializeField] private Image border;
  15. [SerializeField] private Text label;
  16. [SerializeField] private Sprite impArt = null;
  17. [SerializeField] private Sprite tridentArt = null;
  18. [SerializeField] private Sprite potionArt = null;
  19. [SerializeField] private Sprite piggyArt = null;
  20. [SerializeField] private Sprite ringArt = null;
  21. [SerializeField] private Sprite candleArt = null;
  22. [SerializeField] private Sprite starArt = null;
  23. [SerializeField] private Sprite aArt = null;
  24. [SerializeField] private Sprite qArt = null;
  25. [SerializeField] private Sprite jArt = null;
  26. [SerializeField] private Sprite tenArt = null;
  27. [SerializeField] private Sprite nineArt = null;
  28. private Tween stateTween, removalTween;
  29. private Action removalCompleted;
  30. private bool removalNotified;
  31. private Image artwork;
  32. public Image Artwork => artwork;
  33. private bool interactive, removing;
  34. private Coroutine shake;
  35. private Vector2 shakeOrigin;
  36. private bool selected, hinted;
  37. public bool IsHinted => hinted;
  38. public bool IsRemoving => removing;
  39. public Tile Data { get; private set; }
  40. public event Action<TileView> Clicked;
  41. public void SetInteractive(bool enabled) { interactive = enabled; if (border) border.raycastTarget = enabled && !removing; }
  42. public void OnPointerClick(PointerEventData data)
  43. { if (interactive && !removing && data.button == PointerEventData.InputButton.Left) Clicked?.Invoke(this); }
  44. public void Shake()
  45. {
  46. if (removing || !Application.isPlaying) return;
  47. if (shake != null) { StopCoroutine(shake); ((RectTransform)transform).anchoredPosition = shakeOrigin; }
  48. shakeOrigin = ((RectTransform)transform).anchoredPosition; shake = StartCoroutine(ShakeRoutine());
  49. }
  50. private IEnumerator ShakeRoutine()
  51. {
  52. float elapsed = 0;
  53. while (elapsed < .32f)
  54. {
  55. elapsed += Time.unscaledDeltaTime;
  56. ((RectTransform)transform).anchoredPosition = shakeOrigin + Vector2.right * Mathf.Sin(elapsed / .32f * Mathf.PI * 6) * 4 * (1 - elapsed / .32f);
  57. yield return null;
  58. }
  59. ((RectTransform)transform).anchoredPosition = shakeOrigin; shake = null;
  60. }
  61. public void PlayRemoval(Action completed)
  62. {
  63. if (removing) return;
  64. removing = true; removalNotified = false; removalCompleted = completed; SetInteractive(false);
  65. KillStateAndShake();
  66. if (!Application.isPlaying || !isActiveAndEnabled) { CompleteRemoval(); return; }
  67. var group = GetComponent<CanvasGroup>(); if (!group) group = gameObject.AddComponent<CanvasGroup>();
  68. float progress = 0;
  69. removalTween = DOTween.To(() => progress, value => {
  70. progress = value; group.alpha = 1 - value;
  71. transform.localScale = Vector3.one * Mathf.Lerp(1, .86f, value);
  72. }, 1, .22f).SetEase(Ease.OutQuad).SetUpdate(true).OnComplete(CompleteRemoval);
  73. }
  74. private void CompleteRemoval()
  75. {
  76. if (removalNotified) return;
  77. removalNotified = true;
  78. removalTween?.Kill(); removalTween = null;
  79. var callback = removalCompleted; removalCompleted = null;
  80. try { callback?.Invoke(); }
  81. finally
  82. {
  83. if (this) { gameObject.SetActive(false); if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject); }
  84. }
  85. }
  86. private void KillStateAndShake()
  87. {
  88. stateTween?.Kill(); stateTween = null;
  89. if (shake != null) { StopCoroutine(shake); shake = null; ((RectTransform)transform).anchoredPosition = shakeOrigin; }
  90. }
  91. // Pause completes already accepted removals; cancellation on destruction never invokes stale callbacks.
  92. public void FinishVisuals()
  93. {
  94. KillStateAndShake();
  95. if (removing)
  96. {
  97. if (!removalNotified) CompleteRemoval();
  98. else if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject);
  99. return;
  100. }
  101. hinted = false; ApplyVisualState(false);
  102. }
  103. private void OnDisable()
  104. {
  105. KillStateAndShake(); removalTween?.Kill(); removalTween = null;
  106. removalCompleted = null; removalNotified = true;
  107. transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity;
  108. var group = GetComponent<CanvasGroup>(); if (group) group.alpha = 1;
  109. }
  110. public int TileId { get; private set; }
  111. public string DisplayText => label.text;
  112. public void Configure(Image background, Image outline, Text text) { face = background; border = outline; label = text; }
  113. public void Bind(Tile tile)
  114. {
  115. if (tile == null) throw new ArgumentNullException(nameof(tile));
  116. if (!face || !border || !label) throw new InvalidOperationException("TileView prefab is missing visual references.");
  117. KillStateAndShake(); removalTween?.Kill(); removalTween = null; removalCompleted = null;
  118. removing = false; removalNotified = false;
  119. transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity;
  120. var group = GetComponent<CanvasGroup>(); if (group) group.alpha = 1;
  121. Data = tile; TileId = tile.Id; label.text = SymbolText(tile.Symbol);
  122. ApplyArtwork(tile.Symbol);
  123. face.raycastTarget = border.raycastTarget = label.raycastTarget = false;
  124. selected = hinted = false; ApplyVisualState(false);
  125. }
  126. public void SetHighlighted(bool on)
  127. {
  128. if (selected == on) return;
  129. selected = on; ApplyVisualState();
  130. }
  131. public void SetHinted(bool on) { if (hinted == on) return; hinted = on; ApplyVisualState(); }
  132. private void ApplyArtwork(TileSymbol symbol)
  133. {
  134. Sprite sprite = null;
  135. switch (symbol)
  136. {
  137. case TileSymbol.imp: sprite = impArt; break;
  138. case TileSymbol.trident: sprite = tridentArt; break;
  139. case TileSymbol.potion: sprite = potionArt; break;
  140. case TileSymbol.piggy: sprite = piggyArt; break;
  141. case TileSymbol.ring: sprite = ringArt; break;
  142. case TileSymbol.candle: sprite = candleArt; break;
  143. case TileSymbol.star: sprite = starArt; break;
  144. case TileSymbol.A: sprite = aArt; break;
  145. case TileSymbol.Q: sprite = qArt; break;
  146. case TileSymbol.J: sprite = jArt; break;
  147. case TileSymbol.T10: sprite = tenArt; break;
  148. case TileSymbol.N9: sprite = nineArt; break;
  149. }
  150. if (sprite && !artwork)
  151. {
  152. // Keep existing prefab references and cell geometry; art is only an inset visual.
  153. var rect = (RectTransform)new GameObject("Artwork", typeof(RectTransform)).transform;
  154. rect.SetParent(face.transform, false);
  155. rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one;
  156. rect.offsetMin = Vector2.one; rect.offsetMax = -Vector2.one;
  157. artwork = rect.gameObject.AddComponent<Image>();
  158. artwork.type = Image.Type.Simple; artwork.preserveAspect = true;
  159. artwork.raycastTarget = false; artwork.color = Color.white;
  160. }
  161. if (artwork) { artwork.sprite = sprite; artwork.enabled = sprite != null; }
  162. // Keep the Text component and its value for existing BoardView/API compatibility.
  163. label.enabled = !sprite;
  164. }
  165. private void ApplyVisualState(bool animate = true)
  166. {
  167. stateTween?.Kill(); stateTween = null;
  168. if (!face || !border || !label) return;
  169. Color targetFace = selected ? new Color32(43, 40, 64, 255) : hinted ? new Color32(35, 30, 55, 255) : new Color32(24, 24, 40, 255);
  170. Color targetBorder = selected ? new Color32(225, 190, 100, 255) : hinted ? new Color32(52, 224, 255, 255) : new Color32(55, 56, 77, 255);
  171. Color targetText = selected || hinted ? Color.white : (Color)new Color32(220, 230, 247, 255);
  172. if (!animate || !Application.isPlaying || !isActiveAndEnabled || removing)
  173. { face.color = targetFace; border.color = targetBorder; label.color = targetText; return; }
  174. Color fromFace = face.color, fromBorder = border.color, fromText = label.color;
  175. float progress = 0;
  176. stateTween = DOTween.To(() => progress, value => {
  177. progress = value; face.color = Color.Lerp(fromFace, targetFace, value);
  178. border.color = Color.Lerp(fromBorder, targetBorder, value); label.color = Color.Lerp(fromText, targetText, value);
  179. }, 1, .12f).SetEase(Ease.OutQuad).SetUpdate(true);
  180. }
  181. public static string SymbolText(TileSymbol symbol)
  182. {
  183. switch (symbol)
  184. {
  185. case TileSymbol.imp: return "IMP"; case TileSymbol.trident: return "TRI";
  186. case TileSymbol.ring: return "RNG"; case TileSymbol.potion: return "POT";
  187. case TileSymbol.candle: return "CND"; case TileSymbol.star: return "STR";
  188. case TileSymbol.piggy: return "PIG"; case TileSymbol.A: return "A";
  189. case TileSymbol.Q: return "Q"; case TileSymbol.J: return "J";
  190. case TileSymbol.T10: return "10"; case TileSymbol.N9: return "9";
  191. default: throw new ArgumentOutOfRangeException(nameof(symbol));
  192. }
  193. }
  194. }
  195. }