89 lines
2.6 KiB
C#
89 lines
2.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TouchManager_Main : MonoBehaviour
|
|
{
|
|
public GridSpawner gridSpawner;
|
|
public HoldHintSpawner holdHintSpawner;
|
|
public GridDisplaySpawner gridDisplaySpawner;
|
|
public Text scoreLabel;
|
|
|
|
public System.Action<int, int> OnTap;
|
|
public System.Action<int, int> OnHoldStart;
|
|
public System.Action OnHoldEnd;
|
|
|
|
private GridTouchInput input;
|
|
private int holdFingerId = -1;
|
|
|
|
void Start()
|
|
{
|
|
input = new GridTouchInput(gridSpawner, gridSpawner, gridDisplaySpawner, holdHintSpawner);
|
|
input.IgnoreCurrentTouches();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
foreach (var t in input.Poll())
|
|
{
|
|
if (t.ended)
|
|
{
|
|
if (t.fingerId == holdFingerId)
|
|
{
|
|
holdFingerId = -1;
|
|
OnHoldEnd?.Invoke();
|
|
}
|
|
continue;
|
|
}
|
|
if (!t.OnCell) continue;
|
|
|
|
var cell = gridSpawner.gridCells[t.x, t.y];
|
|
if (!t.began)
|
|
{
|
|
cell.FlashWhite();
|
|
continue;
|
|
}
|
|
|
|
OnTap?.Invoke(t.x, t.y);
|
|
|
|
if (t.x == 2 && t.y == 2 && holdFingerId < 0)
|
|
{
|
|
holdFingerId = t.fingerId;
|
|
OnHoldStart?.Invoke(t.x, t.y);
|
|
cell.FlashBlue();
|
|
}
|
|
else
|
|
{
|
|
CheckNoteTimingAndFlash(t.x, t.y, SongClock.Now - t.pressAge);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 主畫面背景音符:點擊時顯示 Early / Late 毫秒數(可當作延遲校正參考)
|
|
void CheckNoteTimingAndFlash(int x, int y, double pressTime)
|
|
{
|
|
if (gridDisplaySpawner == null || !gridDisplaySpawner.HasNote(x, y)) return;
|
|
|
|
double delta = pressTime - gridDisplaySpawner.GetHitTime(x, y);
|
|
Judgement j = NoteJudge.Evaluate(delta);
|
|
if (j == Judgement.None || j == Judgement.Miss) return;
|
|
|
|
var cell = gridSpawner.gridCells[x, y];
|
|
switch (j)
|
|
{
|
|
case Judgement.Perfect: cell.FlashBlue(); break; // 藍色
|
|
case Judgement.Slow: cell.FlashOrange(); break; // 慢 橙色
|
|
case Judgement.Fast: cell.FlashGreen(); break; // 快 綠色
|
|
case Judgement.Bad: cell.FlashRed(); break; // 紅色
|
|
}
|
|
|
|
gridDisplaySpawner.Clear(x, y);
|
|
ShowOffset((int)System.Math.Round(delta * 1000));
|
|
}
|
|
|
|
private void ShowOffset(int ms)
|
|
{
|
|
if (scoreLabel == null) return;
|
|
scoreLabel.text = (ms < 0 ? "Early " : "Late ") + System.Math.Abs(ms) + "ms";
|
|
}
|
|
}
|