using System; using System.Collections; using TMPro; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] public class DrawScreenView : MonoBehaviour { public event Action CloseRequested; public event Action MessageRequested; readonly Cell[] cells = new Cell[9]; Button spinButton; TMP_Text spinLabel; TMP_Text subtitle; Coroutine spinRoutine; int pendingPrizeIndex = -1; bool wired; sealed class Cell { public Transform root; public Transform glow; public Transform highlight; public TMP_Text label; } void Awake() { CacheReferences(); WireButtons(); } void OnEnable() { SaveService.Changed += HandleSaveChanged; LocaleService.Changed += HandleSaveChanged; Refresh(); } void OnDisable() { SaveService.Changed -= HandleSaveChanged; LocaleService.Changed -= HandleSaveChanged; if (spinRoutine != null) { StopCoroutine(spinRoutine); spinRoutine = null; ResolvePendingPrize(); } ClearHighlights(); } public void Show() { gameObject.SetActive(true); Refresh(); } public void Hide() { gameObject.SetActive(false); } void HandleSaveChanged() { if (isActiveAndEnabled && spinRoutine == null) Refresh(); } void CacheReferences() { subtitle = UIViewUtility.Text(transform, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo"); spinButton = UIViewUtility.Button(transform, "IMG_BG/BTN_Spin"); spinLabel = UIViewUtility.Text(transform, "IMG_BG/BTN_Spin/TMP_SpinLabel"); for (int i = 0; i < cells.Length; i++) { Transform root = UIViewUtility.Find(transform, "IMG_BG/GameObject/item" + (i + 1)); if (root == null) continue; cells[i] = new Cell { root = root, glow = UIViewUtility.Find(root, "IMG_RewardHighlightGlow"), highlight = UIViewUtility.Find(root, "IMG_RewardCardActive"), label = UIViewUtility.Text(root, "TMP_RewardLabel") }; } } void WireButtons() { if (wired) return; UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause"), () => CloseRequested?.Invoke()); UIViewUtility.Bind(spinButton, BeginSpin); wired = true; } public void Refresh() { if (!Application.isPlaying) return; UILocaleScreens.ApplyDraw(transform); for (int i = 0; i < cells.Length; i++) { Cell cell = cells[i]; if (cell == null || cell.label == null) continue; int prizeIndex = GameDefinitions.DrawGridPrizeMap[i]; if (prizeIndex < 0 || prizeIndex >= GameDefinitions.DrawPrizes.Length) continue; DrawPrize prize = GameDefinitions.DrawPrizes[prizeIndex]; cell.label.text = prize.coins ? UIViewUtility.FormatNumber(prize.amount) : GameLocale.PowerUpShortName(prize.itemKey); } bool free = SaveService.FreeDrawAvailable(); int tickets = SaveService.Data.tickets; bool usable = free || tickets > 0; if (subtitle != null) subtitle.text = L.Get("draw.subtitle"); if (spinLabel != null) { if (free) spinLabel.text = L.Get("draw.free_today"); else if (tickets > 0) spinLabel.text = L.F("draw.ticket", tickets); else spinLabel.text = L.Get("draw.no_ticket"); } if (spinButton != null) { spinButton.interactable = spinRoutine == null; ColorBlock colors = spinButton.colors; Color tint = usable ? Color.white : new Color(0.62f, 0.62f, 0.62f, 1f); colors.normalColor = tint; colors.highlightedColor = usable ? new Color(0.96f, 0.96f, 0.96f, 1f) : new Color(0.70f, 0.70f, 0.70f, 1f); colors.pressedColor = usable ? new Color(0.86f, 0.86f, 0.86f, 1f) : new Color(0.56f, 0.56f, 0.56f, 1f); colors.selectedColor = tint; colors.disabledColor = new Color(0.72f, 0.72f, 0.72f, 0.72f); spinButton.colors = colors; Color c = spinLabel.color; c.a = usable ? 1f : 128f / 255f; spinLabel.color = c; } } void BeginSpin() { if (spinRoutine != null) return; if (!SaveService.CanDraw()) { GameAudio.PlaySfx(GameSfx.Error); MessageRequested?.Invoke(L.Get("toast.draw_tickets")); return; } spinRoutine = StartCoroutine(SpinRoutine()); } IEnumerator SpinRoutine() { if (spinButton != null) spinButton.interactable = false; int prizeIndex = PickPrize(); pendingPrizeIndex = prizeIndex; int targetGrid = -1; for (int i = 0; i < GameDefinitions.DrawGridPrizeMap.Length; i++) { if (GameDefinitions.DrawGridPrizeMap[i] == prizeIndex) { targetGrid = i; break; } } int ringPosition = 0; for (int i = 0; i < GameDefinitions.DrawRingGridOrder.Length; i++) { if (GameDefinitions.DrawRingGridOrder[i] == targetGrid) { ringPosition = i; break; } } int totalSteps = 16 + Mathf.Max(0, ringPosition); int currentGrid = -1; for (int step = 0; step <= totalSteps; step++) { int grid = GameDefinitions.DrawRingGridOrder[step % GameDefinitions.DrawRingGridOrder.Length]; if (grid != currentGrid) { ClearHighlights(); SetHighlight(grid, true); currentGrid = grid; GameAudio.PlaySfx(GameSfx.Spin); } int remaining = totalSteps - step; float delay = remaining <= 6 ? 0.09f + (6 - remaining) * 0.055f : 0.055f; yield return new WaitForSecondsRealtime(delay); } ResolvePendingPrize(); yield return new WaitForSecondsRealtime(0.65f); ClearHighlights(); spinRoutine = null; Refresh(); } void ResolvePendingPrize() { if (pendingPrizeIndex < 0) return; int index = pendingPrizeIndex; pendingPrizeIndex = -1; if (!SaveService.ApplyDrawPrize(index, out DrawPrize prize)) return; GameAudio.PlaySfx(prize.coins ? GameSfx.Coin : GameSfx.Win); string message = prize.coins ? L.F("toast.draw_coins", UIViewUtility.FormatNumber(prize.amount)) : L.F("toast.draw_item", GameLocale.PowerUpName(prize.itemKey)); MessageRequested?.Invoke(message); } int PickPrize() { int total = 0; for (int i = 0; i < GameDefinitions.DrawPrizes.Length; i++) total += GameDefinitions.DrawPrizes[i].weight; float roll = UnityEngine.Random.value * total; for (int i = 0; i < GameDefinitions.DrawPrizes.Length; i++) { roll -= GameDefinitions.DrawPrizes[i].weight; if (roll <= 0f) return i; } return GameDefinitions.DrawPrizes.Length - 1; } void SetHighlight(int gridIndex, bool active) { if (gridIndex < 0 || gridIndex >= cells.Length) return; Cell cell = cells[gridIndex]; if (cell == null) return; if (cell.glow != null) cell.glow.gameObject.SetActive(active); if (cell.highlight != null) cell.highlight.gameObject.SetActive(active); } void ClearHighlights() { for (int i = 0; i < cells.Length; i++) { Cell cell = cells[i]; if (cell == null) continue; if (cell.glow != null) cell.glow.gameObject.SetActive(false); if (cell.highlight != null) cell.highlight.gameObject.SetActive(false); } } }