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

326 lines
12 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
public enum NoteType { Tap, Hold, Slide }
public enum CellFlash { White, Blue, Green, Orange, Red }
// 5x6 的格子:所有畫面共用一個,畫面只設定格子上的圖示、文字與音符
// 每格由下而上:外框(判定閃色)、圖示、音符、長按進度環、文字
// 音符動畫完全由 SongClock 決定,保證在判定時間時縮放剛好為 1
public class Grid : MonoBehaviour
{
public const int Columns = 5;
public const int Rows = 6;
// 1080 寬的畫布單位
public const float CellSize = 180f;
public const float Spacing = 20f;
public const float Width = Columns * CellSize + (Columns - 1) * Spacing;
public const float Height = Rows * CellSize + (Rows - 1) * Spacing;
// 音符在判定點前多久出現(秒);與 Maker 的 zones.APPEAR_MS 一致
public const double AppearLead = 0.5;
private const double FillDuration = 0.2; // 出現時的圓形填滿動畫
private const double GrowHalf = 0.2; // 判定點前後的縮放區間:0.5 → 1 → 1.5
private const float FlashDuration = 0.3f;
private const float RingScale = 1.2f;
// 音符超過判定時間仍未被處理(x, y, 種類)
public event Action<int, int, NoteType> NoteExpired;
private class Cell
{
public RectTransform rt;
public Image frame, icon, note, ring;
public Text label;
public bool hasNote;
public NoteType type;
public double hitTime;
public int holdMs;
public float flashEnd;
public bool flashColored; // 目前是判定色(優先於按住時的白色)
public float ringStart, ringLength;
}
private readonly Cell[,] cells = new Cell[Columns, Rows];
private RectTransform area;
private Sprite frameDefault, noteTap, noteHold, noteSlide;
private Sprite[] flashSprites;
private Font font;
public static Grid Create(RectTransform parent, Font font)
{
var go = new GameObject("Grid", typeof(RectTransform));
go.transform.SetParent(parent, false);
var g = go.AddComponent<Grid>();
g.Build(font);
return g;
}
void Build(Font f)
{
font = f;
frameDefault = Sprites.Get("Grid_dgray");
noteTap = Sprites.Get("TapNote");
noteHold = Sprites.Get("HoldNote");
noteSlide = Sprites.Get("SlideNote");
flashSprites = new[]
{
Sprites.Get("Grid_white"), Sprites.Get("Grid_blue"), Sprites.Get("Grid_green"),
Sprites.Get("Grid_orange"), Sprites.Get("Grid_red"),
};
Sprite ringSprite = Sprites.Get("LongPressHint");
area = (RectTransform)transform;
area.anchorMin = area.anchorMax = area.pivot = new Vector2(0.5f, 0f);
area.sizeDelta = new Vector2(Width, Height);
for (int y = 0; y < Rows; y++)
for (int x = 0; x < Columns; x++)
{
var c = new Cell();
var go = new GameObject($"Cell {x},{y}", typeof(RectTransform));
go.transform.SetParent(area, false);
c.rt = (RectTransform)go.transform;
c.rt.anchorMin = c.rt.anchorMax = Vector2.zero;
c.rt.pivot = new Vector2(0.5f, 0.5f);
c.rt.sizeDelta = new Vector2(CellSize, CellSize);
c.rt.anchoredPosition = new Vector2(x * (CellSize + Spacing) + CellSize / 2, y * (CellSize + Spacing) + CellSize / 2);
c.frame = Layer(c.rt, "Frame", frameDefault);
c.icon = Layer(c.rt, "Icon", null);
c.icon.preserveAspect = true;
c.note = Layer(c.rt, "Note", null);
c.note.type = Image.Type.Filled;
c.note.fillMethod = Image.FillMethod.Radial360;
c.note.fillOrigin = 2;
c.note.fillClockwise = true;
c.ring = Layer(c.rt, "Ring", ringSprite);
c.ring.type = Image.Type.Filled;
c.ring.fillMethod = Image.FillMethod.Radial360;
c.ring.fillOrigin = 2;
c.ring.rectTransform.localScale = Vector3.one * RingScale;
c.ring.enabled = false;
var lgo = new GameObject("Label", typeof(RectTransform));
lgo.transform.SetParent(c.rt, false);
c.label = lgo.AddComponent<Text>();
c.label.font = font;
c.label.fontStyle = FontStyle.Bold;
c.label.alignment = TextAnchor.MiddleCenter;
c.label.horizontalOverflow = HorizontalWrapMode.Overflow;
c.label.verticalOverflow = VerticalWrapMode.Overflow;
c.label.raycastTarget = false;
Stretch(c.label.rectTransform);
c.label.enabled = false;
cells[x, y] = c;
}
}
static Image Layer(RectTransform parent, string name, Sprite sprite)
{
var go = new GameObject(name, typeof(RectTransform));
go.transform.SetParent(parent, false);
var img = go.AddComponent<Image>();
img.sprite = sprite;
img.raycastTarget = false; // 格子不擋觸控判斷(UIHitTest 只看面板、按鈕)
img.enabled = sprite != null;
Stretch(img.rectTransform);
return img;
}
static void Stretch(RectTransform rt)
{
rt.anchorMin = Vector2.zero;
rt.anchorMax = Vector2.one;
rt.offsetMin = rt.offsetMax = Vector2.zero;
}
public static bool InBounds(int x, int y) => x >= 0 && y >= 0 && x < Columns && y < Rows;
// 由 App 每幀呼叫:在畫面處理完觸控(判定)之後、生成新音符之前
public void Tick()
{
double now = SongClock.Now;
float t = Time.unscaledTime;
for (int y = 0; y < Rows; y++)
for (int x = 0; x < Columns; x++)
{
var c = cells[x, y];
if (c.hasNote)
{
double d = now - c.hitTime;
if (d > NoteJudge.BadWindow)
{
var type = c.type;
ClearNote(x, y);
NoteExpired?.Invoke(x, y, type);
}
else UpdateNote(c, d);
}
if (c.flashEnd > 0 && t >= c.flashEnd)
{
c.frame.sprite = frameDefault;
c.flashEnd = 0;
}
if (c.ring.enabled)
{
float p = c.ringLength > 0 ? (t - c.ringStart) / c.ringLength : 1f;
if (p >= 1f) c.ring.enabled = false;
else c.ring.fillAmount = 1f - p;
}
}
}
// ---------- 圖示與文字 ----------
public void SetIcon(int x, int y, Sprite sprite)
{
if (!InBounds(x, y)) return;
var c = cells[x, y];
c.icon.sprite = sprite;
c.icon.enabled = sprite != null;
}
public void SetLabel(int x, int y, string text, Color color, float fontScale = 0.55f)
{
if (!InBounds(x, y)) return;
var l = cells[x, y].label;
l.enabled = !string.IsNullOrEmpty(text);
l.text = text ?? "";
l.color = color;
l.fontSize = Mathf.RoundToInt(CellSize * fontScale);
}
// 清除所有圖示、文字、音符與進度環(換畫面時)
public void ClearAll()
{
for (int y = 0; y < Rows; y++)
for (int x = 0; x < Columns; x++)
{
var c = cells[x, y];
ClearNote(x, y);
SetIcon(x, y, null);
c.label.enabled = false;
c.ring.enabled = false;
c.frame.sprite = frameDefault;
c.flashEnd = 0;
}
}
// ---------- 音符 ----------
public void ShowNote(int x, int y, NoteType type, double hitTime, int holdMs = 0)
{
if (!InBounds(x, y)) return;
var c = cells[x, y];
// 同格還有未處理的音符:視為 Miss,避免音符無聲消失造成統計少算
if (c.hasNote)
{
var old = c.type;
ClearNote(x, y);
NoteExpired?.Invoke(x, y, old);
}
c.hasNote = true;
c.type = type;
c.hitTime = hitTime;
c.holdMs = holdMs;
c.note.sprite = type == NoteType.Tap ? noteTap : type == NoteType.Hold ? noteHold : noteSlide;
c.note.enabled = true;
UpdateNote(c, SongClock.Now - hitTime);
}
// d = 目前時間 - 判定時間
static void UpdateNote(Cell c, double d)
{
float fill = (float)Math.Min(1.0, Math.Max(0.0, (d + AppearLead) / FillDuration));
float scale = d < -GrowHalf ? 0.5f : (float)(1.0 + d / (2 * GrowHalf));
float alpha = scale > 1f ? Mathf.Clamp01(1f - (scale - 1f) / 0.5f) : 1f;
c.note.fillAmount = fill;
c.note.color = new Color(1, 1, 1, alpha);
c.note.rectTransform.localScale = Vector3.one * scale;
}
public bool HasNote(int x, int y) => InBounds(x, y) && cells[x, y].hasNote;
public NoteType NoteTypeAt(int x, int y) => cells[x, y].type;
public double HitTime(int x, int y) => cells[x, y].hitTime;
public int HoldMs(int x, int y) => cells[x, y].holdMs;
public void ClearNote(int x, int y)
{
if (!InBounds(x, y)) return;
var c = cells[x, y];
c.hasNote = false;
c.note.enabled = false;
c.note.rectTransform.localScale = Vector3.one;
}
// ---------- 回饋 ----------
public void Flash(int x, int y, CellFlash color)
{
if (!InBounds(x, y)) return;
var c = cells[x, y];
bool colored = color != CellFlash.White;
// 按住時的白色不覆蓋判定色
if (!colored && c.flashEnd > 0 && c.flashColored) return;
c.frame.sprite = flashSprites[(int)color];
c.flashColored = colored;
c.flashEnd = Time.unscaledTime + FlashDuration;
}
public void StartRing(int x, int y, float seconds)
{
if (!InBounds(x, y) || seconds <= 0) return;
var c = cells[x, y];
c.ring.enabled = true;
c.ring.fillAmount = 1f;
c.ringStart = Time.unscaledTime;
c.ringLength = seconds;
}
public void StopRing(int x, int y)
{
if (InBounds(x, y)) cells[x, y].ring.enabled = false;
}
// 螢幕座標 → 格子;在格子外(含間距)回傳 false
public bool CellAt(Vector2 screenPos, out int x, out int y)
{
x = y = -1;
if (!isActiveAndEnabled) return false;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(area, screenPos, null, out Vector2 local)) return false;
// area 的 pivot 在下方中央
Vector2 p = local + new Vector2(Width / 2, 0);
const float step = CellSize + Spacing;
int cx = Mathf.FloorToInt(p.x / step);
int cy = Mathf.FloorToInt(p.y / step);
if (!InBounds(cx, cy)) return false;
if (p.x - cx * step > CellSize || p.y - cy * step > CellSize) return false;
x = cx;
y = cy;
return true;
}
}
// Resources/Sprites 的圖片(快取)
public static class Sprites
{
private static readonly System.Collections.Generic.Dictionary<string, Sprite> cache =
new System.Collections.Generic.Dictionary<string, Sprite>();
public static Sprite Get(string name)
{
if (!cache.TryGetValue(name, out var s))
{
s = Resources.Load<Sprite>("Sprites/" + name);
if (s == null) Debug.LogWarning("[Sprites] 找不到 " + name);
cache[name] = s;
}
return s;
}
}