| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- namespace DiamondArrow
- {
- public sealed partial class DiamondArrowGame
- {
- void BeginModal(string id)
- {
- CloseModal();
- CurrentModal = id;
- modal = Node(root, "Modal " + id, 0, 0, 750, 1334);
- Stretch(modal);
- var shade = modal.gameObject.AddComponent<Image>();
- shade.color = new Color(.01f, 0, .025f, .88f);
- }
- RectTransform Sheet(string title,float height=740,bool dismiss=true,string id=null)
- {
- BeginModal(id ?? title);
- if(dismiss){var b=modal.gameObject.AddComponent<Button>();b.transition=Selectable.Transition.None;b.onClick.AddListener(()=>{if(!chestBusy)CloseAndRefresh();});}
- var panel=Panel(modal,title,0,0,680,height);Center(panel);panel.GetComponent<NeonPanel>().raycastTarget=true;panel.gameObject.AddComponent<Button>().transition=Selectable.Transition.None;
- Label(panel,title,30,24,530,52,30,Color.white,TextAnchor.MiddleLeft);Rule(panel,30,104,620,Neon.Purple);
- if(dismiss)CloseImageButton(panel);
- return panel;
- }
- void CloseImageButton(RectTransform panel)
- {
- var r=Node(panel,"Close",576,10,88,88);var hit=r.gameObject.AddComponent<Image>();hit.color=Color.clear;hit.raycastTarget=true;
- var face=Node(r,"Close artwork",0,0,88,88).gameObject.AddComponent<Image>();face.sprite=theme.closeButton;face.preserveAspect=true;face.raycastTarget=false;
- 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();});
- }
- RectTransform FooterButton(RectTransform panel,string name,float x,float width,string title,Action action,bool primary=true)
- {return Button(panel,name,x,panel.rect.height-128,width,96,title,action,primary);}
- void CloseModal()
- {
- if(modal){modal.gameObject.SetActive(false);Destroy(modal.gameObject);modal=null;}
- victory = null;
- CurrentModal=null;if(gemsText)gemsText.text=Save.gems.ToString();
- }
- void CloseAndRefresh(){CloseModal();if(CurrentScreen=="Lobby")ShowLobby();}
- void RequestReward(Action grant)
- {
- if (rewardPending) return;
- var origin = modal;
- var owner = Save;
- rewardPending = true;
- if (!origin.TryGetComponent<CanvasGroup>(out var controls))
- controls = origin.gameObject.AddComponent<CanvasGroup>();
- controls.interactable = false;
- platform.ShowReward(earned => {
- rewardPending = false;
- if (!this || !origin || origin != modal || owner != Save) return;
- controls.interactable = true;
- if (earned) grant();
- else Toast("Ad unavailable or unfinished. Please try again.");
- });
- }
- public void ShowQuit()
- {
- if(Run==null||CurrentScreen!="Game")return;if(Run.Status!=RunStatus.Playing)return;
- 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);
- FooterButton(p,"Stay",32,290,"STAY",CloseModal,false);FooterButton(p,"Leave",352,295,"LEAVE",ShowLobby,true);
- }
- public void ShowFail()
- {
- 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");
- Icon(p,12,273,88,130);Label(p,"Level "+Run.Level.number+" · "+Run.Remaining+" arrows left",30,235,620,40,26,Neon.Ice);
- 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);
- if (Run.CanRevive)
- {
- var attempt = Run;
- Button(p, "Revive", 52, 382, 576, 96, "WATCH AD · +1 HEART", () =>
- RequestReward(() => {
- if (Run != attempt || !attempt.Revive()) return;
- Sound(SoundCue.Heart);
- CloseModal();
- resultScheduled = false;
- readyAt = Time.unscaledTime + .35f;
- UpdateGameHud();
- }), true);
- }
- FooterButton(p,"Lobby",30,295,"HOME",ShowLobby,false);FooterButton(p,"Retry",355,295,"RETRY",()=>StartLevel(Run.Level.number),true);
- }
- public void ShowResult()
- {
- if (Run == null || Run.Status != RunStatus.Cleared || Run.RewardClaimed) return;
- var reward = Economy.Clear(Run, Save);
- Persist();
- Sound(SoundCue.Clear);
- PresentVictory(reward);
- }
- void PresentVictory(ClearReward reward)
- {
- BeginModal("Result");
- victory = Instantiate(victoryLayout, modal, false);
- Center((RectTransform)victory.transform);
- int number = Run.Level.number;
- victory.level.text = "LEVEL " + number;
- victory.chapter.text = Book.Chapter(number);
- string mistakes = Run.Mistakes == 0 ? "No mistakes" : Run.Mistakes == 1 ? "1 mistake" : Run.Mistakes + " mistakes";
- victory.performance.text = mistakes + " · " + Clock(Run.TimeLeft) + " left";
- victory.ShowReward(reward);
- RefreshIdentity();
- BindAction(victory.next, () => StartLevel(number + 1));
- BindAction(victory.home, ShowLobby);
- }
- public void ShowChest()
- {
- var p = Sheet("DAILY CHEST", 720);
- var chest = Icon(p, 8, 220, 128, 240);
- Label(p, "20–300 diamonds or power-ups", 40, 392, 600, 48, 27, Neon.Ice);
- 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);
- var action = FooterButton(p, "Open chest", 80, 520, "OPEN FREE", () => TryOpenChest(chest.rectTransform));
- chestAction = action.GetComponent<Button>();
- chestActionText = action.GetComponentInChildren<Text>();
- RefreshChestControls(DateTimeOffset.Now, true);
- }
- void RefreshChestControls(DateTimeOffset now, bool force = false)
- {
- long second = now.ToUnixTimeSeconds();
- if (!force && second == lastChestDisplaySecond) return;
- lastChestDisplaySecond = second;
- if (CurrentModal == "DAILY CHECK-IN" && checkinDisplayedDate != Today) ShowCheckin();
- bool free = Economy.IsFreeChest(Save, now);
- int wait = Economy.ChestWaitSeconds(Save, now);
- string remaining = TimeSpan.FromSeconds(wait).ToString(@"mm\:ss");
- if (lobby) lobby.chestBadge.text = wait > 0 ? remaining : free ? "FREE" : "AD";
- if (CurrentModal != "DAILY CHEST" || !chestAction) return;
- chestAction.interactable = wait == 0 && !chestBusy;
- chestActionText.text = wait > 0 ? "READY IN " + remaining
- : free ? "OPEN FREE" : "WATCH AD TO OPEN";
- }
- void TryOpenChest(RectTransform chest)
- {
- var now = DateTimeOffset.Now;
- if (chestBusy || rewardPending || Economy.ChestWaitSeconds(Save, now) > 0) return;
- if (Economy.IsFreeChest(Save, now)) GrantChest(chest, false);
- else RequestReward(() => GrantChest(chest, true));
- }
- void GrantChest(RectTransform chest, bool earnedAd)
- {
- var now = DateTimeOffset.Now;
- string prize = Economy.OpenChest(Save, now, earnedAd, random.NextDouble());
- if (prize == null) return;
- Persist();
- chestBusy = true;
- RefreshChestControls(now, true);
- Sound(SoundCue.ChestOpen);
- StartCoroutine(RevealChest(chest, prize));
- }
- IEnumerator RevealChest(RectTransform chest,string prize)
- {
- 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;}
- 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);
- }
- public void ShowDiamonds()
- {
- 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);
- }
- public void ShowProfile()
- {
- 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);
- }
- public void ShowSettings()
- {
- var p=Sheet("SETTINGS",720);string[] names={"Sound effects","Music","Haptics"};bool[] flags={Save.sfx,Save.music,Save.haptics};
- 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]);}
- 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);
- }
- void ShowReset()
- {
- 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);
- }
- void CreateAudio()
- {
- gameAudio=gameObject.AddComponent<GameAudio>();
- gameAudio.Initialize(theme,Save.music,Save.sfx);
- }
- void ApplyAudioPreferences(){if(gameAudio)gameAudio.ApplyPreferences(Save.music,Save.sfx);}
- void Sound(SoundCue cue){if(Save!=null&&Save.sfx&&gameAudio)gameAudio.Play(cue);}
- void CountdownAudio()
- {
- int second=Mathf.CeilToInt(Run.TimeLeft);
- if(Run.Status==RunStatus.Playing&&second>=1&&second<=5&&second!=lastCountdownSecond)
- {lastCountdownSecond=second;Sound(SoundCue.Countdown);}
- }
- void Buzz(){
- #if UNITY_IOS && !UNITY_EDITOR
- if(Save.haptics)Handheld.Vibrate();
- #endif
- }
- }
- }
|