using System; using System.Collections; using DG.Tweening; using UnityEngine.EventSystems; using DevilLink2.Core; using UnityEngine; using UnityEngine.UI; namespace DevilLink2.Presentation { [DisallowMultipleComponent] public sealed class TileView : MonoBehaviour, IPointerClickHandler { [SerializeField] private Image face; [SerializeField] private Image border; [SerializeField] private Text label; [SerializeField] private Sprite impArt = null; [SerializeField] private Sprite tridentArt = null; [SerializeField] private Sprite potionArt = null; [SerializeField] private Sprite piggyArt = null; [SerializeField] private Sprite ringArt = null; [SerializeField] private Sprite candleArt = null; [SerializeField] private Sprite starArt = null; [SerializeField] private Sprite aArt = null; [SerializeField] private Sprite qArt = null; [SerializeField] private Sprite jArt = null; [SerializeField] private Sprite tenArt = null; [SerializeField] private Sprite nineArt = null; private Tween stateTween, removalTween; private Action removalCompleted; private bool removalNotified; private Image artwork; public Image Artwork => artwork; private bool interactive, removing; private Coroutine shake; private Vector2 shakeOrigin; private bool selected, hinted; public bool IsHinted => hinted; public bool IsRemoving => removing; public Tile Data { get; private set; } public event Action Clicked; public void SetInteractive(bool enabled) { interactive = enabled; if (border) border.raycastTarget = enabled && !removing; } public void OnPointerClick(PointerEventData data) { if (interactive && !removing && data.button == PointerEventData.InputButton.Left) Clicked?.Invoke(this); } public void Shake() { if (removing || !Application.isPlaying) return; if (shake != null) { StopCoroutine(shake); ((RectTransform)transform).anchoredPosition = shakeOrigin; } shakeOrigin = ((RectTransform)transform).anchoredPosition; shake = StartCoroutine(ShakeRoutine()); } private IEnumerator ShakeRoutine() { float elapsed = 0; while (elapsed < .32f) { elapsed += Time.unscaledDeltaTime; ((RectTransform)transform).anchoredPosition = shakeOrigin + Vector2.right * Mathf.Sin(elapsed / .32f * Mathf.PI * 6) * 4 * (1 - elapsed / .32f); yield return null; } ((RectTransform)transform).anchoredPosition = shakeOrigin; shake = null; } public void PlayRemoval(Action completed) { if (removing) return; removing = true; removalNotified = false; removalCompleted = completed; SetInteractive(false); KillStateAndShake(); if (!Application.isPlaying || !isActiveAndEnabled) { CompleteRemoval(); return; } var group = GetComponent(); if (!group) group = gameObject.AddComponent(); float progress = 0; removalTween = DOTween.To(() => progress, value => { progress = value; group.alpha = 1 - value; transform.localScale = Vector3.one * Mathf.Lerp(1, .86f, value); }, 1, .22f).SetEase(Ease.OutQuad).SetUpdate(true).OnComplete(CompleteRemoval); } private void CompleteRemoval() { if (removalNotified) return; removalNotified = true; removalTween?.Kill(); removalTween = null; var callback = removalCompleted; removalCompleted = null; try { callback?.Invoke(); } finally { if (this) { gameObject.SetActive(false); if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject); } } } private void KillStateAndShake() { stateTween?.Kill(); stateTween = null; if (shake != null) { StopCoroutine(shake); shake = null; ((RectTransform)transform).anchoredPosition = shakeOrigin; } } // Pause completes already accepted removals; cancellation on destruction never invokes stale callbacks. public void FinishVisuals() { KillStateAndShake(); if (removing) { if (!removalNotified) CompleteRemoval(); else if (Application.isPlaying) Destroy(gameObject); else DestroyImmediate(gameObject); return; } hinted = false; ApplyVisualState(false); } private void OnDisable() { KillStateAndShake(); removalTween?.Kill(); removalTween = null; removalCompleted = null; removalNotified = true; transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity; var group = GetComponent(); if (group) group.alpha = 1; } public int TileId { get; private set; } public string DisplayText => label.text; public void Configure(Image background, Image outline, Text text) { face = background; border = outline; label = text; } public void Bind(Tile tile) { if (tile == null) throw new ArgumentNullException(nameof(tile)); if (!face || !border || !label) throw new InvalidOperationException("TileView prefab is missing visual references."); KillStateAndShake(); removalTween?.Kill(); removalTween = null; removalCompleted = null; removing = false; removalNotified = false; transform.localScale = Vector3.one; transform.localRotation = Quaternion.identity; var group = GetComponent(); if (group) group.alpha = 1; Data = tile; TileId = tile.Id; label.text = SymbolText(tile.Symbol); ApplyArtwork(tile.Symbol); face.raycastTarget = border.raycastTarget = label.raycastTarget = false; selected = hinted = false; ApplyVisualState(false); } public void SetHighlighted(bool on) { if (selected == on) return; selected = on; ApplyVisualState(); } public void SetHinted(bool on) { if (hinted == on) return; hinted = on; ApplyVisualState(); } private void ApplyArtwork(TileSymbol symbol) { Sprite sprite = null; switch (symbol) { case TileSymbol.imp: sprite = impArt; break; case TileSymbol.trident: sprite = tridentArt; break; case TileSymbol.potion: sprite = potionArt; break; case TileSymbol.piggy: sprite = piggyArt; break; case TileSymbol.ring: sprite = ringArt; break; case TileSymbol.candle: sprite = candleArt; break; case TileSymbol.star: sprite = starArt; break; case TileSymbol.A: sprite = aArt; break; case TileSymbol.Q: sprite = qArt; break; case TileSymbol.J: sprite = jArt; break; case TileSymbol.T10: sprite = tenArt; break; case TileSymbol.N9: sprite = nineArt; break; } if (sprite && !artwork) { // Keep existing prefab references and cell geometry; art is only an inset visual. var rect = (RectTransform)new GameObject("Artwork", typeof(RectTransform)).transform; rect.SetParent(face.transform, false); rect.anchorMin = Vector2.zero; rect.anchorMax = Vector2.one; rect.offsetMin = Vector2.one; rect.offsetMax = -Vector2.one; artwork = rect.gameObject.AddComponent(); artwork.type = Image.Type.Simple; artwork.preserveAspect = true; artwork.raycastTarget = false; artwork.color = Color.white; } if (artwork) { artwork.sprite = sprite; artwork.enabled = sprite != null; } // Keep the Text component and its value for existing BoardView/API compatibility. label.enabled = !sprite; } private void ApplyVisualState(bool animate = true) { stateTween?.Kill(); stateTween = null; if (!face || !border || !label) return; Color targetFace = selected ? new Color32(43, 40, 64, 255) : hinted ? new Color32(35, 30, 55, 255) : new Color32(24, 24, 40, 255); Color targetBorder = selected ? new Color32(225, 190, 100, 255) : hinted ? new Color32(52, 224, 255, 255) : new Color32(55, 56, 77, 255); Color targetText = selected || hinted ? Color.white : (Color)new Color32(220, 230, 247, 255); if (!animate || !Application.isPlaying || !isActiveAndEnabled || removing) { face.color = targetFace; border.color = targetBorder; label.color = targetText; return; } Color fromFace = face.color, fromBorder = border.color, fromText = label.color; float progress = 0; stateTween = DOTween.To(() => progress, value => { progress = value; face.color = Color.Lerp(fromFace, targetFace, value); border.color = Color.Lerp(fromBorder, targetBorder, value); label.color = Color.Lerp(fromText, targetText, value); }, 1, .12f).SetEase(Ease.OutQuad).SetUpdate(true); } public static string SymbolText(TileSymbol symbol) { switch (symbol) { case TileSymbol.imp: return "IMP"; case TileSymbol.trident: return "TRI"; case TileSymbol.ring: return "RNG"; case TileSymbol.potion: return "POT"; case TileSymbol.candle: return "CND"; case TileSymbol.star: return "STR"; case TileSymbol.piggy: return "PIG"; case TileSymbol.A: return "A"; case TileSymbol.Q: return "Q"; case TileSymbol.J: return "J"; case TileSymbol.T10: return "10"; case TileSymbol.N9: return "9"; default: throw new ArgumentOutOfRangeException(nameof(symbol)); } } } }