Files
SahikandClaude Opus 5.5 ddd5fdc28b Rebuild the game as a single code-driven scene with a package-folder pack source
- One empty scene; App builds the canvas, safe-area layout and a shared 5x6
  Grid in code. Pages (Intro, Main, Choose, Play, Account, Settings) replace
  5 scenes, 5 prefabs and the per-cell spawner/display components.
- Packs: the game reads Rhyculator/package/ (with All files access) or the
  app folder, like a plugged-in USB stick. Zips stay where they are and are
  extracted to a cache keyed by name/size/mtime; folders are read directly.
  Scans run every second on a background thread. Old songs/ imports and inbox
  zips are migrated automatically.
- Download packs by 8-char code from the new pack website, or via
  rhyculator://pack/<code> links; opened/shared zips go into the package folder.
- Play: pause/resume/retry, auto-pause on background, combo and progress bar;
  judging and scoring unchanged. Choose page fits 9:16 screens; empty state,
  pack delete. Android back key handled everywhere.
- Slimming: removed TextMesh Pro, unused 2D/Timeline/VisualScripting/etc.
  packages and modules, scene template, unused sprites; menu theme wav (20MB)
  -> ogg (2.3MB). Build profile replaced by explicit settings in BuildAndroid.
- Tests: PackLibrary/PackReader/migration/serializer tests (39 passing).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-24 14:18:54 +08:00

149 lines
5.1 KiB
C#

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<AudioClip>("MainTheme/music");
var sheet = Resources.Load<TextAsset>("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<GridTouch> 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);
}
}