using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; // 主選單:長按中間開始;上排帳號 / 設定 / 曲包網站;背景播放主題曲與它的譜面 // 點背景音符會顯示 Early / Late 毫秒數(可當延遲校正參考) public class MainPage : Page, ILibraryListener { private const float HoldSeconds = 1f; private const int HoldX = 2, HoldY = 2; private ChartPlayer chart; private Text info, offset; private int holdFinger = -1; private float holdStart; public override void Enter() { var title = Ui.Label(Top, "Rhyculator", 140, Color.white); title.fontStyle = FontStyle.Bold; Ui.Place(title.rectTransform, new Vector2(0.5f, 0.5f), new Vector2(0, 60), new Vector2(1080, 200)); info = Ui.Label(Top, "", 40, Ui.Muted); Ui.Place(info.rectTransform, new Vector2(0.5f, 0.5f), new Vector2(0, -70), new Vector2(1000, 60)); offset = Ui.Label(Top, "", 44, Ui.Accent); Ui.Place(offset.rectTransform, new Vector2(0.5f, 0f), new Vector2(0, 10), new Vector2(1000, 70)); LibraryChanged(); Grid.SetIcon(1, 5, Sprites.Get("Display account")); Grid.SetIcon(2, 5, Sprites.Get("Display setting")); Grid.SetIcon(3, 5, Sprites.Get("Display global")); Grid.SetIcon(HoldX, HoldY, Sprites.Get("HoldStart")); Grid.SetIcon(1, 2, Sprites.Get("ArrowRight")); Grid.SetIcon(3, 2, Sprites.Get("ArrowLeft")); Grid.SetIcon(2, 1, Sprites.Get("ArrowUp")); Grid.SetIcon(2, 3, Sprites.Get("ArrowDown")); StartTheme(); } public void LibraryChanged() { int n = Library.Songs.Count; info.text = !Library.Loaded ? "讀取曲包中…" : n > 0 ? $"已載入 {n} 個曲包 長按中間開始" : "還沒有曲包:長按中間開始後可輸入代碼下載"; } void StartTheme() { var music = App.Music; var clip = Resources.Load("MainTheme/music"); var sheet = Resources.Load("MainTheme/sheet"); if (clip == null || sheet == null) return; try { chart = new ChartPlayer(ChartLoader.Parse(sheet.text, 0, out _)); } catch (System.Exception e) { Debug.LogWarning("[Main] 主題譜面讀取失敗:" + e.Message); } // 從帳號 / 設定回來時主題曲還在播:接著播,時鐘對齊目前位置 if (music.clip == clip && music.isPlaying) { double start = AudioSettings.dspTime - music.time; SongClock.Begin(start); chart?.Seek(music.time); return; } music.Stop(); music.clip = clip; music.loop = false; double dspStart = AudioSettings.dspTime + 0.5; music.PlayScheduled(dspStart); SongClock.Begin(dspStart); chart?.Seek(0); } public override void Tick(List touches) { // 主題曲播完:重新開始 if (App.Music.clip != null && !App.Music.isPlaying && SongClock.Running && SongClock.Now > App.Music.clip.length) StartTheme(); foreach (var t in touches) { if (t.ended) { if (t.fingerId == holdFinger) StopHold(); continue; } if (!t.OnCell) continue; if (!t.began) { Grid.Flash(t.x, t.y, CellFlash.White); continue; } if (t.x == HoldX && t.y == HoldY && holdFinger < 0) { holdFinger = t.fingerId; holdStart = Time.unscaledTime; Grid.StartRing(t.x, t.y, HoldSeconds); Grid.Flash(t.x, t.y, CellFlash.Blue); } else if (t.y == 5 && t.x == 1) App.Go(new AccountPage()); else if (t.y == 5 && t.x == 2) App.Go(new SettingsPage()); else if (t.y == 5 && t.x == 3) Application.OpenURL(ServerConfig.GameServerUrl + "/"); else JudgeBackground(t); } if (holdFinger >= 0 && Time.unscaledTime - holdStart >= HoldSeconds) { holdFinger = -1; App.Go(new ChoosePage()); } } public override void LateTick() { if (chart != null && SongClock.Running) chart.Tick(Grid, SongClock.Now); } void StopHold() { holdFinger = -1; Grid.StopRing(HoldX, HoldY); } void JudgeBackground(GridTouch t) { if (!Grid.HasNote(t.x, t.y)) return; double delta = SongClock.Now - t.pressAge - Grid.HitTime(t.x, t.y); Judgement j = NoteJudge.Evaluate(delta); if (j == Judgement.None || j == Judgement.Miss) return; Grid.Flash(t.x, t.y, PlayPage.FlashFor(j)); Grid.ClearNote(t.x, t.y); int ms = (int)System.Math.Round(delta * 1000); offset.text = (ms < 0 ? "Early " : "Late ") + System.Math.Abs(ms) + "ms"; } public override void Back() { var d = new Dialog(App.Overlay, "離開遊戲?"); d.AddClose("取消"); d.AddClose("離開", Application.Quit, primary: true); } public override void Exit() { Grid.StopRing(HoldX, HoldY); } }