GamePanels.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace DiamondArrow
  6. {
  7. public sealed partial class DiamondArrowGame
  8. {
  9. void BeginModal(string id)
  10. {
  11. CloseModal();
  12. CurrentModal = id;
  13. modal = Node(root, "Modal " + id, 0, 0, 750, 1334);
  14. Stretch(modal);
  15. var shade = modal.gameObject.AddComponent<Image>();
  16. shade.color = new Color(.01f, 0, .025f, .88f);
  17. }
  18. RectTransform Sheet(string title,float height=740,bool dismiss=true,string id=null)
  19. {
  20. BeginModal(id ?? title);
  21. if(dismiss){var b=modal.gameObject.AddComponent<Button>();b.transition=Selectable.Transition.None;b.onClick.AddListener(()=>{if(!chestBusy)CloseAndRefresh();});}
  22. var panel=Panel(modal,title,0,0,680,height);Center(panel);panel.GetComponent<NeonPanel>().raycastTarget=true;panel.gameObject.AddComponent<Button>().transition=Selectable.Transition.None;
  23. Label(panel,title,30,24,530,52,30,Color.white,TextAnchor.MiddleLeft);Rule(panel,30,104,620,Neon.Purple);
  24. if(dismiss)CloseImageButton(panel);
  25. return panel;
  26. }
  27. void CloseImageButton(RectTransform panel)
  28. {
  29. var r=Node(panel,"Close",576,10,88,88);var hit=r.gameObject.AddComponent<Image>();hit.color=Color.clear;hit.raycastTarget=true;
  30. var face=Node(r,"Close artwork",0,0,88,88).gameObject.AddComponent<Image>();face.sprite=theme.closeButton;face.preserveAspect=true;face.raycastTarget=false;
  31. var button=r.gameObject.AddComponent<Button>();button.targetGraphic=face;var colors=button.colors;colors.pressedColor=new Color(.6f,.45f,.7f);button.colors=colors;r.gameObject.AddComponent<ButtonDepth>();button.onClick.AddListener(()=>{Sound(SoundCue.Tap);if(!chestBusy)CloseAndRefresh();});
  32. }
  33. RectTransform FooterButton(RectTransform panel,string name,float x,float width,string title,Action action,bool primary=true)
  34. {return Button(panel,name,x,panel.rect.height-128,width,96,title,action,primary);}
  35. void CloseModal()
  36. {
  37. if(modal){modal.gameObject.SetActive(false);Destroy(modal.gameObject);modal=null;}
  38. victory = null;
  39. CurrentModal=null;if(gemsText)gemsText.text=Save.gems.ToString();
  40. }
  41. void CloseAndRefresh(){CloseModal();if(CurrentScreen=="Lobby")ShowLobby();}
  42. void RequestReward(Action grant)
  43. {
  44. if (rewardPending) return;
  45. var origin = modal;
  46. var owner = Save;
  47. rewardPending = true;
  48. if (!origin.TryGetComponent<CanvasGroup>(out var controls))
  49. controls = origin.gameObject.AddComponent<CanvasGroup>();
  50. controls.interactable = false;
  51. platform.ShowReward(earned => {
  52. rewardPending = false;
  53. if (!this || !origin || origin != modal || owner != Save) return;
  54. controls.interactable = true;
  55. if (earned) grant();
  56. else Toast("Ad unavailable or unfinished. Please try again.");
  57. });
  58. }
  59. public void ShowQuit()
  60. {
  61. if(Run==null||CurrentScreen!="Game")return;if(Run.Status!=RunStatus.Playing)return;
  62. var p=Sheet("LEAVE THIS LEVEL?",420);Label(p,"Your progress in this attempt will be lost.\nYour diamonds and cleared levels are safe.",35,110,610,112,24,Neon.Muted);
  63. FooterButton(p,"Stay",32,290,"STAY",CloseModal,false);FooterButton(p,"Leave",352,295,"LEAVE",ShowLobby,true);
  64. }
  65. public void ShowFail()
  66. {
  67. if(Run==null)return;if(CurrentModal!="Fail")Sound(SoundCue.Fail);bool time=Run.Status==RunStatus.FailedTime;var p=Sheet(time?"TIME UP":"OUT OF HEARTS",650,false,"Fail");
  68. Icon(p,12,273,88,130);Label(p,"Level "+Run.Level.number+" · "+Run.Remaining+" arrows left",30,235,620,40,26,Neon.Ice);
  69. Label(p,Run.ReviveUsed?"Try again from the beginning.":time?"Revive with one more heart and 30 seconds.":"Find a clear path and try again.",30,286,620,56,22,Neon.Muted);
  70. if (Run.CanRevive)
  71. {
  72. var attempt = Run;
  73. Button(p, "Revive", 52, 382, 576, 96, "WATCH AD · +1 HEART", () =>
  74. RequestReward(() => {
  75. if (Run != attempt || !attempt.Revive()) return;
  76. Sound(SoundCue.Heart);
  77. CloseModal();
  78. resultScheduled = false;
  79. readyAt = Time.unscaledTime + .35f;
  80. UpdateGameHud();
  81. }), true);
  82. }
  83. FooterButton(p,"Lobby",30,295,"HOME",ShowLobby,false);FooterButton(p,"Retry",355,295,"RETRY",()=>StartLevel(Run.Level.number),true);
  84. }
  85. public void ShowResult()
  86. {
  87. if (Run == null || Run.Status != RunStatus.Cleared || Run.RewardClaimed) return;
  88. var reward = Economy.Clear(Run, Save);
  89. Persist();
  90. Sound(SoundCue.Clear);
  91. PresentVictory(reward);
  92. }
  93. void PresentVictory(ClearReward reward)
  94. {
  95. BeginModal("Result");
  96. victory = Instantiate(victoryLayout, modal, false);
  97. Center((RectTransform)victory.transform);
  98. int number = Run.Level.number;
  99. victory.level.text = "LEVEL " + number;
  100. victory.chapter.text = Book.Chapter(number);
  101. string mistakes = Run.Mistakes == 0 ? "No mistakes" : Run.Mistakes == 1 ? "1 mistake" : Run.Mistakes + " mistakes";
  102. victory.performance.text = mistakes + " · " + Clock(Run.TimeLeft) + " left";
  103. victory.ShowReward(reward);
  104. RefreshIdentity();
  105. BindAction(victory.next, () => StartLevel(number + 1));
  106. BindAction(victory.home, ShowLobby);
  107. }
  108. public void ShowChest()
  109. {
  110. var p = Sheet("DAILY CHEST", 720);
  111. var chest = Icon(p, 8, 220, 128, 240);
  112. Label(p, "20–300 diamonds or power-ups", 40, 392, 600, 48, 27, Neon.Ice);
  113. Label(p, "First chest free each day. Watch an ad for the next.\nEach ad reward starts a 5-minute wait.", 48, 455, 584, 82, 23, Neon.Muted);
  114. var action = FooterButton(p, "Open chest", 80, 520, "OPEN FREE", () => TryOpenChest(chest.rectTransform));
  115. chestAction = action.GetComponent<Button>();
  116. chestActionText = action.GetComponentInChildren<Text>();
  117. RefreshChestControls(DateTimeOffset.Now, true);
  118. }
  119. void RefreshChestControls(DateTimeOffset now, bool force = false)
  120. {
  121. long second = now.ToUnixTimeSeconds();
  122. if (!force && second == lastChestDisplaySecond) return;
  123. lastChestDisplaySecond = second;
  124. if (CurrentModal == "DAILY CHECK-IN" && checkinDisplayedDate != Today) ShowCheckin();
  125. bool free = Economy.IsFreeChest(Save, now);
  126. int wait = Economy.ChestWaitSeconds(Save, now);
  127. string remaining = TimeSpan.FromSeconds(wait).ToString(@"mm\:ss");
  128. if (lobby) lobby.chestBadge.text = wait > 0 ? remaining : free ? "FREE" : "AD";
  129. if (CurrentModal != "DAILY CHEST" || !chestAction) return;
  130. chestAction.interactable = wait == 0 && !chestBusy;
  131. chestActionText.text = wait > 0 ? "READY IN " + remaining
  132. : free ? "OPEN FREE" : "WATCH AD TO OPEN";
  133. }
  134. void TryOpenChest(RectTransform chest)
  135. {
  136. var now = DateTimeOffset.Now;
  137. if (chestBusy || rewardPending || Economy.ChestWaitSeconds(Save, now) > 0) return;
  138. if (Economy.IsFreeChest(Save, now)) GrantChest(chest, false);
  139. else RequestReward(() => GrantChest(chest, true));
  140. }
  141. void GrantChest(RectTransform chest, bool earnedAd)
  142. {
  143. var now = DateTimeOffset.Now;
  144. string prize = Economy.OpenChest(Save, now, earnedAd, random.NextDouble());
  145. if (prize == null) return;
  146. Persist();
  147. chestBusy = true;
  148. RefreshChestControls(now, true);
  149. Sound(SoundCue.ChestOpen);
  150. StartCoroutine(RevealChest(chest, prize));
  151. }
  152. IEnumerator RevealChest(RectTransform chest,string prize)
  153. {
  154. for(float t=0;t<.9f;t+=Time.unscaledDeltaTime){if(!chest)yield break;chest.localScale=Vector3.one*(1+.08f*Mathf.Sin(t*45));chest.localRotation=Quaternion.Euler(0,0,Mathf.Sin(t*55)*4);yield return null;}
  155. chestBusy=false;Sound(SoundCue.Reward);Buzz();var p=Sheet("VAULT UNLOCKED",600);Icon(p,prize.Contains("HAMMER")?11:1,243,100,194);Label(p,prize,25,324,630,68,29,Neon.Gold);FooterButton(p,"Collect",100,480,"COLLECT",ShowChest,true);
  156. }
  157. public void ShowDiamonds()
  158. {
  159. var p=Sheet("DIAMONDS",690);Icon(p,1,250,100,180);Label(p,Save.gems.ToString(),25,296,630,75,54,Color.white);Label(p,"Earn diamonds by clearing levels,\nchecking in and opening your daily chest.\nSpend them on power-ups in the shop.",45,389,590,135,23,Neon.Muted);FooterButton(p,"Open shop",140,400,"VISIT SHOP",ShowShop,true);
  160. }
  161. public void ShowProfile()
  162. {
  163. var p=Sheet("PLAYER PROFILE",760);profilePortrait=Icon(p,0,250,94,180);profilePortrait.name="Avatar portrait";profilePortrait.sprite=PlayerPortrait();profileName=FittedLabel(Label(p,DisplayName,30,295,620,48,33,Color.white));Label(p,"LEVELS CLEARED "+Save.Cleared+"\nSTARS COLLECTED "+Save.TotalStars+"\nDIAMONDS EARNED "+Save.earned+"\nCHECK-INS "+Save.checkinTotal,75,370,530,200,25,Neon.Ice,TextAnchor.MiddleLeft);FooterButton(p,"Done",125,430,"DONE",CloseAndRefresh,true);
  164. }
  165. public void ShowSettings()
  166. {
  167. var p=Sheet("SETTINGS",720);string[] names={"Sound effects","Music","Haptics"};bool[] flags={Save.sfx,Save.music,Save.haptics};
  168. for(int i=0;i<3;i++){int k=i;Label(p,names[i],38,120+i*108,400,88,27,Color.white,TextAnchor.MiddleLeft);Button(p,"Toggle "+names[i],476,120+i*108,160,88,flags[i]?"ON":"OFF",()=>{if(k==0)Save.sfx=!Save.sfx;if(k==1)Save.music=!Save.music;if(k==2)Save.haptics=!Save.haptics;ApplyAudioPreferences();Persist();ShowSettings();},flags[i]);}
  169. Label(p,"All progress is saved on this device.\nDiamond Arrow Slide · 1.0",35,470,610,75,21,Neon.Muted);FooterButton(p,"Reset progress",100,480,"RESET LOCAL PROGRESS",ShowReset,false);
  170. }
  171. void ShowReset()
  172. {
  173. var p=Sheet("RESET PROGRESS?",430);Label(p,"This erases this game's levels, diamonds,\nitems, check-in and profile on this device.",35,115,610,110,24,Neon.Muted);FooterButton(p,"Cancel reset",30,295,"CANCEL",ShowSettings,false);FooterButton(p,"Confirm reset",355,295,"RESET",()=>{store.Reset();Save=new PlayerSave();Save.InitializeGuest(random);ApplyAudioPreferences();Persist();ShowStart();},true);
  174. }
  175. void CreateAudio()
  176. {
  177. gameAudio=gameObject.AddComponent<GameAudio>();
  178. gameAudio.Initialize(theme,Save.music,Save.sfx);
  179. }
  180. void ApplyAudioPreferences(){if(gameAudio)gameAudio.ApplyPreferences(Save.music,Save.sfx);}
  181. void Sound(SoundCue cue){if(Save!=null&&Save.sfx&&gameAudio)gameAudio.Play(cue);}
  182. void CountdownAudio()
  183. {
  184. int second=Mathf.CeilToInt(Run.TimeLeft);
  185. if(Run.Status==RunStatus.Playing&&second>=1&&second<=5&&second!=lastCountdownSecond)
  186. {lastCountdownSecond=second;Sound(SoundCue.Countdown);}
  187. }
  188. void Buzz(){
  189. #if UNITY_IOS && !UNITY_EDITOR
  190. if(Save.haptics)Handheld.Vibrate();
  191. #endif
  192. }
  193. }
  194. }