| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public partial class GameScreenView
- {
- const string FailReasonTimeUp = "time_up";
- const string FailReasonNoMoves = "no_moves";
- TMP_Text pauseModeInfo;
- TMP_Text pauseMovesCount;
- TMP_Text pauseFruitsLeftCount;
- TMP_Text pauseServedCount;
- GameObject winPanel;
- GameObject losePanel;
- TMP_Text winModeInfo;
- TMP_Text winSummary;
- TMP_Text winLevelBonusValue;
- TMP_Text winMovesBonusValue;
- TMP_Text winServedValue;
- TMP_Text winTotalReward;
- GameObject winTicketBadge;
- TMP_Text winTicketText;
- TMP_Text winNextLabel;
- TMP_Text loseTimeUpModeInfo;
- TMP_Text loseNoMovesModeInfo;
- TMP_Text loseMessage;
- GameObject loseTimeUpGroup;
- GameObject loseNoMovesGroup;
- TMP_Text loseServedLabel;
- TMP_Text loseServedValue;
- TMP_Text loseFruitLeftLabel;
- TMP_Text loseFruitLeftValue;
- TMP_Text loseMovesLeftLabel;
- TMP_Text loseMovesLeftValue;
- TMP_Text loseNextLabel;
- bool resultIsWin;
- string resultFailTitle = "";
- [Header("Panel Prefabs")]
- [SerializeField] GameObject pausePanelPrefab;
- [SerializeField] GameObject winPanelPrefab;
- [SerializeField] GameObject losePanelPrefab;
- void BuildBanner()
- {
- var bannerGo = new GameObject("Banner", typeof(RectTransform), typeof(CanvasGroup));
- bannerGo.transform.SetParent(transform, false);
- var rect = (RectTransform)bannerGo.transform;
- rect.anchorMin = new Vector2(0.5f, 0.5f);
- rect.anchorMax = new Vector2(0.5f, 0.5f);
- rect.pivot = new Vector2(0.5f, 0.5f);
- rect.anchoredPosition = new Vector2(0f, 95f);
- rect.sizeDelta = new Vector2(640f, 110f);
- bannerGroup = bannerGo.GetComponent<CanvasGroup>();
- bannerGroup.alpha = 0f;
- bannerGroup.blocksRaycasts = false;
- bannerGroup.interactable = false;
- bannerText = CreateText(bannerGo.transform, "BannerText", textTemplate, "NICE!", 44f, new Color(1f, 0.85f, 0.32f, 1f), TextAlignmentOptions.Center, Vector2.zero, new Vector2(640f, 110f));
- if (bannerText != null)
- {
- var outline = bannerText.gameObject.AddComponent<Outline>();
- outline.effectColor = new Color(0.28f, 0.07f, 0.01f, 1f);
- outline.effectDistance = new Vector2(3f, -3f);
- }
- }
- public void SetPanelPrefabs(GameObject pause, GameObject win, GameObject lose, GameObject pop)
- {
- pausePanelPrefab = pause;
- winPanelPrefab = win;
- losePanelPrefab = lose;
- popViewPanelPrefab = pop;
- }
- void BuildPanels()
- {
- BuildPausePanel();
- BuildWinPanel();
- BuildLosePanel();
- BuildPopView();
- }
- GameObject InstantiatePanel(GameObject prefab, string resourcePath, string panelName)
- {
- if (prefab == null)
- prefab = Resources.Load<GameObject>(resourcePath);
- if (prefab == null)
- {
- Debug.LogError("[GameScreen] Missing panel prefab: " + resourcePath);
- return null;
- }
- GameObject panel = Instantiate(prefab, transform, false);
- panel.name = panelName;
- panel.SetActive(false);
- return panel;
- }
- static Transform PanelChild(GameObject panel, string path)
- {
- if (panel == null)
- return null;
- Transform child = panel.transform.Find(path);
- if (child == null)
- Debug.LogWarning("[GameScreen] Missing panel node: " + path);
- return child;
- }
- static TMP_Text PanelText(GameObject panel, string path)
- {
- Transform child = PanelChild(panel, path);
- TMP_Text text = child != null ? child.GetComponent<TMP_Text>() : null;
- if (child != null && text == null)
- Debug.LogWarning("[GameScreen] Panel node is not TMP text: " + path);
- return text;
- }
- static Button PanelButton(GameObject panel, string path)
- {
- Transform child = PanelChild(panel, path);
- return child != null ? UIViewUtility.EnsureButton(child) : null;
- }
- void BuildPausePanel()
- {
- pausePanel = InstantiatePanel(pausePanelPrefab, "Prefab/08_pause", "PausePanel");
- if (pausePanel == null)
- return;
- pauseModeInfo = PanelText(pausePanel, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
- pauseMovesCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_MovesCount");
- pauseFruitsLeftCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_FruitsLeftCount");
- pauseServedCount = PanelText(pausePanel, "IMG_BG/IMG_PauseStatsBG/TMP_ServedCount");
- UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_ClosePause"), ResumeGame);
- UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_Resume"), ResumeGame);
- UIViewUtility.Bind(PanelButton(pausePanel, "IMG_BG/BTN_Quit"), HandleBack);
- UILocaleScreens.ApplyPausePanel(pausePanel != null ? pausePanel.transform : null);
- }
- void BuildWinPanel()
- {
- winPanel = InstantiatePanel(winPanelPrefab, "Prefab/09_win", "WinPanel");
- if (winPanel == null)
- return;
- winModeInfo = PanelText(winPanel, "IMG_BG/IMG_PauseModeBadge/TMP_PauseModeInfo");
- winSummary = PanelText(winPanel, "IMG_BG/IMG_StandEmptySummaryBG/TMP_StandEmptySummary");
- winLevelBonusValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Value");
- winMovesBonusValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Value");
- winServedValue = PanelText(winPanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Value");
- winTotalReward = PanelText(winPanel, "IMG_BG/IMG_TotalRewardBG/OBJ_Content/TMP_Reward_Coin");
- winTicketBadge = PanelChild(winPanel, "IMG_BG/IMG_DrawTicketBadgeBG")?.gameObject;
- winTicketText = PanelText(winPanel, "IMG_BG/IMG_DrawTicketBadgeBG/OBJ_Content/TMP_Reward_Ticket");
- winNextLabel = PanelText(winPanel, "IMG_BG/BTN_NextLevel/TMP_Label");
- UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_ClosePause"), HandleBack);
- UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_Back"), HandleBack);
- UIViewUtility.Bind(PanelButton(winPanel, "IMG_BG/BTN_NextLevel"), HandleResultAction);
- UILocaleScreens.ApplyWinPanel(winPanel != null ? winPanel.transform : null);
- }
- void BuildLosePanel()
- {
- losePanel = InstantiatePanel(losePanelPrefab, "Prefab/10_lose", "LosePanel");
- if (losePanel == null)
- return;
- loseTimeUpGroup = PanelChild(losePanel, "IMG_BG/GRP_TimeUp")?.gameObject;
- loseNoMovesGroup = PanelChild(losePanel, "IMG_BG/GRP_NoMoves")?.gameObject;
- loseTimeUpModeInfo = PanelText(losePanel, "IMG_BG/GRP_TimeUp/IMG_PauseModeBadge/TMP_PauseModeInfo");
- loseNoMovesModeInfo = PanelText(losePanel, "IMG_BG/GRP_NoMoves/IMG_PauseModeBadge/TMP_PauseModeInfo");
- loseMessage = PanelText(losePanel, "IMG_BG/TMP_Message");
- loseServedLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Label");
- loseServedValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_SetsServed/TMP_Value");
- loseFruitLeftLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Label");
- loseFruitLeftValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_FruitLeft/TMP_Value");
- loseMovesLeftLabel = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Label");
- loseMovesLeftValue = PanelText(losePanel, "IMG_BG/IMG_ClearedScoreBG/GRP_MovesLeft/TMP_Value");
- loseNextLabel = PanelText(losePanel, "IMG_BG/BTN_NextLevel/TMP_Label");
- UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_ClosePause"), HandleBack);
- UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_Back"), HandleBack);
- UIViewUtility.Bind(PanelButton(losePanel, "IMG_BG/BTN_NextLevel"), HandleResultAction);
- }
- void HandleResultAction()
- {
- int next = resultIsWin
- ? (levelNumber >= GameDefinitions.MaxLevel ? GameDefinitions.MaxLevel : levelNumber + 1)
- : levelNumber;
- HidePanels();
- NextLevelRequested?.Invoke(next);
- }
- bool IsResultVisible()
- {
- return (winPanel != null && winPanel.activeSelf) || (losePanel != null && losePanel.activeSelf);
- }
- void PauseGame()
- {
- if (paused || ended)
- return;
- if (IsResultVisible())
- return;
- paused = true;
- toolMode = "";
- ClearSelection();
- RefreshPausePanel();
- ShowOverlay(pausePanel);
- }
- void RefreshPausePanel()
- {
- UILocaleScreens.ApplyPausePanel(pausePanel != null ? pausePanel.transform : null);
- if (pauseModeInfo != null)
- pauseModeInfo.text = GameLocale.ModeLevelLine(modeKey, levelNumber);
- if (pauseMovesCount != null)
- pauseMovesCount.text = state.MovesLeft.ToString();
- if (pauseFruitsLeftCount != null)
- pauseFruitsLeftCount.text = state.RemainingFruits.ToString();
- if (pauseServedCount != null)
- pauseServedCount.text = state.Served.ToString();
- }
- void ResumeGame()
- {
- paused = false;
- if (pausePanel != null)
- pausePanel.SetActive(false);
- }
- void HidePanels()
- {
- paused = false;
- if (pausePanel != null)
- pausePanel.SetActive(false);
- if (winPanel != null)
- winPanel.SetActive(false);
- if (losePanel != null)
- losePanel.SetActive(false);
- if (popView != null)
- popView.Hide();
- }
- void ShowOverlay(GameObject overlay)
- {
- if (overlay == null)
- return;
- overlay.SetActive(true);
- overlay.transform.SetAsLastSibling();
- var group = overlay.GetComponent<CanvasGroup>();
- if (group == null)
- return;
- if (!Application.isPlaying)
- {
- group.alpha = 1f;
- return;
- }
- StartCoroutine(FadeOverlay(group));
- }
- IEnumerator FadeOverlay(CanvasGroup group)
- {
- group.alpha = 0f;
- float elapsed = 0f;
- while (elapsed < 0.18f)
- {
- elapsed += Time.unscaledDeltaTime;
- group.alpha = Mathf.Clamp01(elapsed / 0.18f);
- yield return null;
- }
- group.alpha = 1f;
- }
- void ShowBanner(string message, float hold, float size)
- {
- if (bannerText == null || bannerGroup == null)
- return;
- bannerText.text = message;
- bannerText.fontSize = size;
- if (!Application.isPlaying)
- {
- bannerGroup.alpha = 0f;
- return;
- }
- if (bannerRoutine != null)
- StopCoroutine(bannerRoutine);
- bannerRoutine = StartCoroutine(BannerRoutine(hold));
- }
- IEnumerator BannerRoutine(float hold)
- {
- bannerGroup.alpha = 0f;
- var rect = bannerText.rectTransform;
- rect.localScale = Vector3.one * 0.82f;
- float elapsed = 0f;
- while (elapsed < 0.14f)
- {
- elapsed += Time.unscaledDeltaTime;
- float t = Mathf.Clamp01(elapsed / 0.14f);
- bannerGroup.alpha = t;
- rect.localScale = Vector3.one * Mathf.Lerp(0.82f, 1.04f, t);
- yield return null;
- }
- rect.localScale = Vector3.one;
- yield return new WaitForSecondsRealtime(Mathf.Max(0.05f, hold));
- elapsed = 0f;
- while (elapsed < 0.22f)
- {
- elapsed += Time.unscaledDeltaTime;
- bannerGroup.alpha = 1f - Mathf.Clamp01(elapsed / 0.22f);
- yield return null;
- }
- bannerGroup.alpha = 0f;
- bannerRoutine = null;
- }
- void WinLevel()
- {
- if (ended)
- return;
- ended = true;
- busy = false;
- toolMode = "";
- ClearSelection();
- ApplyWinRewards();
- GameAudio.PlaySfx(GameSfx.Win);
- if (Application.isPlaying)
- StartCoroutine(WinSequence());
- else
- ShowResult(true, "", "");
- }
- IEnumerator WinSequence()
- {
- ShowBanner(L.Get("game.banner.stand_empty"), 1.1f, 54f);
- bool reduce = SaveService.Data.settings.reduceMotion;
- if (!reduce)
- yield return new WaitForSecondsRealtime(0.95f);
- ShowResult(true, "", "");
- }
- void FailLevel(string title, string subtitle)
- {
- if (ended)
- return;
- ended = true;
- busy = false;
- toolMode = "";
- ClearSelection();
- GameAudio.PlaySfx(GameSfx.Fail);
- if (title == FailReasonNoMoves && CanOfferAdRevive())
- OfferAdRevive(title, subtitle);
- else
- ShowResult(false, title, subtitle);
- }
- void ApplyWinRewards()
- {
- var save = SaveService.Data;
- int previous = save.cleared.Get(modeKey);
- bool firstClear = levelNumber > previous;
- lastRewardCoins = definition.BaseReward + state.MovesLeft * 8;
- lastServedCoins = state.Served * 2;
- lastTicket = 0;
- if (firstClear && levelNumber == 1)
- lastTicket = 1;
- else if (firstClear && UnityEngine.Random.value < 0.35f)
- lastTicket = 1;
- SaveService.AddCoins(lastRewardCoins + lastServedCoins, "ledger.game_clear|" + modeKey + "|" + levelNumber);
- save.cleared.Set(modeKey, Mathf.Max(previous, levelNumber));
- save.stats.clears += 1;
- save.stats.served += state.Served;
- save.stats.best = Mathf.Max(save.stats.best, state.Score);
- save.tickets += lastTicket;
- SaveService.SaveNow();
- }
- void ShowResult(bool win, string title, string subtitle)
- {
- if (AdMobEngagement.RecordRoundCompleted())
- AdMobService.NotifyNonRewardedAdsUnlocked();
- resultIsWin = win;
- resultFailTitle = title;
- if (pausePanel != null)
- pausePanel.SetActive(false);
- if (win)
- {
- if (lastRewardCoins + lastServedCoins > 0)
- GameAudio.PlaySfx(GameSfx.Coin);
- UpdateWinPanel();
- ShowOverlay(winPanel);
- if (losePanel != null)
- losePanel.SetActive(false);
- PlayConfetti();
- }
- else
- {
- UpdateLosePanel(title, subtitle);
- ShowOverlay(losePanel);
- if (winPanel != null)
- winPanel.SetActive(false);
- }
- }
- void UpdateWinPanel()
- {
- UILocaleScreens.ApplyWinPanel(winPanel != null ? winPanel.transform : null);
- if (winModeInfo != null)
- winModeInfo.text = GameLocale.ModeLevelLine(modeKey, levelNumber);
- if (winSummary != null)
- winSummary.text = L.F("result.win_summary", state.Served, state.MovesLeft);
- if (winLevelBonusValue != null)
- winLevelBonusValue.text = "+" + definition.BaseReward;
- if (winMovesBonusValue != null)
- winMovesBonusValue.text = "+" + (state.MovesLeft * 8);
- if (winServedValue != null)
- winServedValue.text = state.Served.ToString();
- if (winTotalReward != null)
- winTotalReward.text = "+" + (lastRewardCoins + lastServedCoins);
- if (winTicketBadge != null)
- winTicketBadge.SetActive(lastTicket > 0);
- if (winTicketText != null)
- winTicketText.text = L.F("result.draw_ticket", lastTicket);
- if (winNextLabel != null)
- winNextLabel.text = levelNumber >= GameDefinitions.MaxLevel
- ? L.F("result.replay_max", GameDefinitions.MaxLevel)
- : L.Get("result.next_level");
- }
- void UpdateLosePanel(string title, string subtitle)
- {
- UILocaleScreens.ApplyLosePanel(losePanel != null ? losePanel.transform : null);
- bool timeUp = title == FailReasonTimeUp;
- string modeInfo = GameLocale.ModeLevelLine(modeKey, levelNumber);
- if (loseTimeUpModeInfo != null)
- loseTimeUpModeInfo.text = modeInfo;
- if (loseNoMovesModeInfo != null)
- loseNoMovesModeInfo.text = modeInfo;
- if (loseTimeUpGroup != null)
- loseTimeUpGroup.SetActive(timeUp);
- if (loseNoMovesGroup != null)
- loseNoMovesGroup.SetActive(!timeUp);
- if (loseMessage != null)
- {
- loseMessage.text = timeUp
- ? L.Get("result.lose_time_msg")
- : L.Get("result.lose_moves_msg");
- }
- if (loseServedLabel != null)
- loseServedLabel.text = L.Get("result.sets_served");
- if (loseServedValue != null)
- loseServedValue.text = state.Served.ToString();
- if (loseFruitLeftLabel != null)
- loseFruitLeftLabel.text = L.Get("result.fruit_left");
- if (loseFruitLeftValue != null)
- loseFruitLeftValue.text = state.RemainingFruits.ToString();
- if (loseMovesLeftLabel != null)
- loseMovesLeftLabel.text = L.Get("result.moves_left");
- if (loseMovesLeftValue != null)
- loseMovesLeftValue.text = state.MovesLeft.ToString();
- if (loseNextLabel != null)
- loseNextLabel.text = L.Get("common.retry");
- }
- class ConfettiData
- {
- public RectTransform rect;
- public Vector2 velocity;
- public float spin;
- public Image image;
- }
- void PlayConfetti()
- {
- if (winPanel == null || SaveService.Data.settings.reduceMotion)
- return;
- ClearConfetti();
- Color[] colors =
- {
- new Color(1f, 0.78f, 0.2f, 1f),
- new Color(1f, 0.25f, 0.56f, 1f),
- new Color(0.36f, 0.9f, 0.52f, 1f),
- new Color(0.33f, 0.91f, 0.89f, 1f),
- new Color(1f, 0.95f, 0.72f, 1f)
- };
- var pieces = new List<ConfettiData>();
- for (int i = 0; i < 54; i++)
- {
- var go = new GameObject("Confetti", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
- go.transform.SetParent(winPanel.transform, false);
- var rect = (RectTransform)go.transform;
- rect.anchorMin = new Vector2(0.5f, 0.5f);
- rect.anchorMax = new Vector2(0.5f, 0.5f);
- rect.pivot = new Vector2(0.5f, 0.5f);
- rect.anchoredPosition = new Vector2(UnityEngine.Random.Range(-350f, 350f), UnityEngine.Random.Range(420f, 760f));
- rect.sizeDelta = new Vector2(UnityEngine.Random.Range(7f, 13f), UnityEngine.Random.Range(12f, 22f));
- var image = go.GetComponent<Image>();
- image.color = colors[UnityEngine.Random.Range(0, colors.Length)];
- image.raycastTarget = false;
- pieces.Add(new ConfettiData
- {
- rect = rect,
- image = image,
- velocity = new Vector2(UnityEngine.Random.Range(-90f, 90f), UnityEngine.Random.Range(-220f, -520f)),
- spin = UnityEngine.Random.Range(-220f, 220f)
- });
- }
- StartCoroutine(ConfettiRoutine(pieces));
- }
- IEnumerator ConfettiRoutine(List<ConfettiData> pieces)
- {
- float elapsed = 0f;
- while (elapsed < 2.1f)
- {
- float dt = Time.unscaledDeltaTime;
- elapsed += dt;
- for (int i = 0; i < pieces.Count; i++)
- {
- var piece = pieces[i];
- if (piece.rect == null)
- continue;
- piece.velocity.y -= 780f * dt;
- piece.rect.anchoredPosition += piece.velocity * dt;
- piece.rect.Rotate(0f, 0f, piece.spin * dt);
- if (piece.image != null)
- {
- Color color = piece.image.color;
- color.a = Mathf.Clamp01(1f - (elapsed - 1.4f) / 0.7f);
- piece.image.color = color;
- }
- }
- yield return null;
- }
- for (int i = 0; i < pieces.Count; i++)
- {
- if (pieces[i].rect != null)
- Destroy(pieces[i].rect.gameObject);
- }
- }
- void ClearConfetti()
- {
- if (confetti.Count > 0)
- {
- for (int i = 0; i < confetti.Count; i++)
- {
- if (confetti[i] != null)
- DestroyObject(confetti[i]);
- }
- confetti.Clear();
- }
- if (winPanel == null)
- return;
- for (int i = winPanel.transform.childCount - 1; i >= 0; i--)
- {
- Transform child = winPanel.transform.GetChild(i);
- if (child.name == "Confetti")
- DestroyObject(child.gameObject);
- }
- }
- }
|