| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using System;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- [DisallowMultipleComponent]
- public class CheckInScreenView : MonoBehaviour
- {
- public event Action CloseRequested;
- public event Action<string> MessageRequested;
- readonly DayCell[] cells = new DayCell[GameDefinitions.MaxCheckInDay];
- TMP_Text subtitle;
- Button claimButton;
- TMP_Text claimLabel;
- bool wired;
- sealed class DayCell
- {
- public Transform root;
- public Transform activeCard;
- public Transform claimedOverlay;
- public TMP_Text dayLabel;
- public TMP_Text rewardLabel;
- }
- void Awake()
- {
- CacheReferences();
- WireButtons();
- }
- void OnEnable()
- {
- SaveService.Changed += HandleSaveChanged;
- LocaleService.Changed += HandleSaveChanged;
- Refresh();
- }
- void OnDisable()
- {
- SaveService.Changed -= HandleSaveChanged;
- LocaleService.Changed -= HandleSaveChanged;
- }
- public void Show()
- {
- gameObject.SetActive(true);
- Refresh();
- }
- public void Hide()
- {
- gameObject.SetActive(false);
- }
- void HandleSaveChanged()
- {
- if (isActiveAndEnabled)
- Refresh();
- }
- void CacheReferences()
- {
- subtitle = UIViewUtility.Text(transform, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
- claimButton = UIViewUtility.Button(transform, "IMG_BG/BTN_Claim");
- claimLabel = UIViewUtility.Text(transform, "IMG_BG/BTN_Claim/TMP_ClaimLabel");
- string[] paths =
- {
- "IMG_BG/GRP_CheckInItems/GameObject/GRP_Item_1",
- "IMG_BG/GRP_CheckInItems/GameObject/GRP_Item_2",
- "IMG_BG/GRP_CheckInItems/GameObject/GRP_Item_3",
- "IMG_BG/GRP_CheckInItems/GameObject/GRP_Item_4",
- "IMG_BG/GRP_CheckInItems/GameObject (1)/GRP_Item_5",
- "IMG_BG/GRP_CheckInItems/GameObject (1)/GRP_Item_6",
- "IMG_BG/GRP_CheckInItems/GameObject (1)/GRP_Item_7"
- };
- for (int i = 0; i < paths.Length; i++)
- {
- Transform root = UIViewUtility.Find(transform, paths[i]);
- if (root == null)
- continue;
- cells[i] = new DayCell
- {
- root = root,
- activeCard = UIViewUtility.Find(root, "IMG_RewardCardActive"),
- claimedOverlay = UIViewUtility.Find(root, "IMG_CheckInClaimedOverlay"),
- dayLabel = UIViewUtility.Text(root, "TMP_DayLabel"),
- rewardLabel = UIViewUtility.Text(root, "Image/TMP_RewardLabel")
- };
- }
- }
- void WireButtons()
- {
- if (wired)
- return;
- UIViewUtility.Bind(UIViewUtility.Button(transform, "IMG_BG/BTN_ClosePause"), () => CloseRequested?.Invoke());
- UIViewUtility.Bind(claimButton, Claim);
- wired = true;
- }
- void Claim()
- {
- if (!SaveService.ClaimCheckIn(out int day, out CheckInReward reward))
- {
- GameAudio.PlaySfx(GameSfx.Error);
- return;
- }
- GameAudio.PlaySfx(GameSfx.Buy);
- string message = reward.coins
- ? L.F("toast.checkin_coins", day, UIViewUtility.FormatNumber(reward.amount))
- : L.F("toast.checkin_item", day, GameLocale.PowerUpName(reward.itemKey));
- MessageRequested?.Invoke(message);
- Refresh();
- }
- public void Refresh()
- {
- if (!Application.isPlaying)
- return;
- UILocaleScreens.ApplyCheckIn(transform);
- var save = SaveService.Data;
- bool canClaim = SaveService.CanCheckInToday();
- int day = SaveService.CheckInDay();
- bool completedCycle = !canClaim && day == 1;
- for (int i = 0; i < cells.Length; i++)
- {
- DayCell cell = cells[i];
- if (cell == null)
- continue;
- int dayNumber = i + 1;
- bool claimed = completedCycle || dayNumber < day;
- bool isToday = canClaim && dayNumber == day;
- if (cell.activeCard != null)
- cell.activeCard.gameObject.SetActive(isToday);
- if (cell.claimedOverlay != null)
- cell.claimedOverlay.gameObject.SetActive(claimed);
- if (cell.dayLabel != null)
- cell.dayLabel.text = L.F("checkin.day_label", dayNumber);
- if (cell.rewardLabel != null && i < GameDefinitions.CheckInRewards.Length)
- {
- CheckInReward reward = GameDefinitions.CheckInRewards[i];
- cell.rewardLabel.text = reward.coins
- ? UIViewUtility.FormatNumber(reward.amount)
- : GameLocale.PowerUpName(reward.itemKey);
- }
- }
- if (subtitle != null)
- subtitle.text = L.F("checkin.subtitle", day, GameDefinitions.MaxCheckInDay);
- if (claimLabel != null)
- {
- claimLabel.text = canClaim ? L.F("checkin.claim_day", day) : L.Get("checkin.claimed_today");
- Color c = claimLabel.color;
- c.a = canClaim ? 1f : 128f / 255f;
- claimLabel.color = c;
- }
- if (claimButton != null)
- claimButton.interactable = canClaim;
- }
- }
|