Files
Rhyculator/Assets/Scripts/Game/GridInput.cs
T
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

92 lines
3.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.EnhancedTouch;
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
// 一根手指在本幀的狀態
public struct GridTouch
{
public int fingerId;
public Vector2 position;
public bool began; // 本幀新按下
public bool ended; // 本幀放開 / 取消
public int x, y; // 所在格子,不在格子上為 -1
public double pressAge; // 按下事件距今幾秒(用來回推精確的按下時間)
public bool OnCell => x >= 0;
}
// 格子的觸控輸入(EnhancedTouch:同一幀內按下又放開的快速點擊也不會漏掉)
// - IgnoreCurrentTouches 之前就按著的手指會被忽略(換畫面時)
// - 按下時若最上層是面板、按鈕等 UI,這根手指直到放開都不會送到格子(防穿透)
// - 編輯器裡用滑鼠模擬觸控
public class GridInput
{
private readonly Grid grid;
private readonly HashSet<int> ignored = new HashSet<int>();
private readonly List<GridTouch> touches = new List<GridTouch>();
// 按下事件最多回推的秒數(避免掉幀時判定被推太遠)
private const double MaxPressAge = 0.1;
public GridInput(Grid grid)
{
this.grid = grid;
EnhancedTouchSupport.Enable();
#if UNITY_EDITOR
TouchSimulation.Enable();
#endif
}
public void IgnoreCurrentTouches()
{
foreach (var t in Touch.activeTouches)
ignored.Add(t.touchId);
}
// blocked = true 時(例如開著對話框)所有新的觸控都不送到格子
public List<GridTouch> Poll(bool blocked = false)
{
touches.Clear();
double now = Time.realtimeSinceStartupAsDouble;
foreach (var t in Touch.activeTouches)
{
int id = t.touchId;
TouchPhase phase = t.phase;
bool ended = phase == TouchPhase.Ended || phase == TouchPhase.Canceled;
bool began = phase == TouchPhase.Began;
if (began && (blocked || UIHitTest.IsBlocked(t.screenPosition)))
ignored.Add(id);
if (ignored.Contains(id))
{
if (ended) ignored.Remove(id);
continue;
}
Vector2 pos = t.screenPosition;
if (ended)
{
touches.Add(new GridTouch { fingerId = id, ended = true, x = -1, y = -1, position = pos });
continue;
}
grid.CellAt(pos, out int x, out int y);
double age = began ? System.Math.Min(MaxPressAge, System.Math.Max(0, now - t.startTime)) : 0;
touches.Add(new GridTouch { fingerId = id, position = pos, began = began, x = x, y = y, pressAge = age });
}
return touches;
}
// 本幀是否有新的點擊(任意位置,結算畫面「點擊繼續」用)
public static bool AnyNewTap()
{
foreach (var t in Touch.activeTouches)
if (t.phase == TouchPhase.Began) return true;
return false;
}
}