183 lines
5.7 KiB
C#
183 lines
5.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TouchManager_Playing : MonoBehaviour
|
|
{
|
|
public GridSpawner gridSpawner;
|
|
public HoldHintSpawner holdHintSpawner;
|
|
public GridDisplaySpawner gridDisplaySpawner;
|
|
public Text scoreLabel;
|
|
|
|
private const int HOLD_FULL_SCORE = 212;
|
|
private const int SLIDE_SCORE = 106;
|
|
|
|
public PlayResult Result { get; } = new PlayResult();
|
|
|
|
private GridTouchInput input;
|
|
|
|
// 按住中的 Hold(可同時多個)
|
|
private class ActiveHold
|
|
{
|
|
public int x, y, fingerId;
|
|
public double hitTime, duration;
|
|
public Judgement head;
|
|
}
|
|
private readonly List<ActiveHold> holds = new List<ActiveHold>();
|
|
|
|
void Start()
|
|
{
|
|
input = new GridTouchInput(gridSpawner, gridSpawner, gridDisplaySpawner, holdHintSpawner);
|
|
input.IgnoreCurrentTouches();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
if (gridDisplaySpawner != null) gridDisplaySpawner.NoteExpired += HandleNoteExpired;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
if (gridDisplaySpawner != null) gridDisplaySpawner.NoteExpired -= HandleNoteExpired;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
double now = SongClock.Now;
|
|
|
|
// Hold 按滿時間 → 自動結算
|
|
for (int i = holds.Count - 1; i >= 0; i--)
|
|
if (now >= holds[i].hitTime + holds[i].duration)
|
|
FinishHold(holds[i], now);
|
|
|
|
foreach (var t in input.Poll())
|
|
{
|
|
if (t.ended)
|
|
{
|
|
var h = holds.Find(a => a.fingerId == t.fingerId);
|
|
if (h != null) FinishHold(h, now);
|
|
continue;
|
|
}
|
|
if (!t.OnCell) continue;
|
|
|
|
// 空按也亮白色(不覆蓋判定色)
|
|
if (!gridDisplaySpawner.HasNote(t.x, t.y))
|
|
{
|
|
gridSpawner.gridCells[t.x, t.y].FlashWhite();
|
|
continue;
|
|
}
|
|
|
|
// 按下的精確時間(觸控事件時間回推),不受幀率影響
|
|
double pressTime = now - t.pressAge;
|
|
string noteType = gridDisplaySpawner.GetCurrentNote(t.x, t.y);
|
|
|
|
if (noteType == "Tap")
|
|
{
|
|
if (t.began) ProcessTap(t.x, t.y, pressTime);
|
|
}
|
|
else if (noteType == "Hold")
|
|
{
|
|
if (t.began)
|
|
{
|
|
int holdMs = gridDisplaySpawner.GetCurrentSec(t.x, t.y);
|
|
double hit = gridDisplaySpawner.GetHitTime(t.x, t.y);
|
|
Judgement j = JudgeHead(t.x, t.y, pressTime);
|
|
if (j != Judgement.None && holdMs > 0)
|
|
{
|
|
// Hold 頭判先記住,按滿才給分
|
|
FlashJudgement(t.x, t.y, j);
|
|
gridDisplaySpawner.Clear(t.x, t.y);
|
|
var h = new ActiveHold
|
|
{
|
|
x = t.x, y = t.y, fingerId = t.fingerId,
|
|
hitTime = hit, duration = holdMs / 1000.0, head = j
|
|
};
|
|
holds.Add(h);
|
|
holdHintSpawner.StartProgressAt(t.x, t.y, (float)(hit + h.duration - now));
|
|
}
|
|
}
|
|
}
|
|
else if (noteType == "Slide")
|
|
{
|
|
// Slide 不提早判定:到達判定點後手指在格子上即算接到(可延遲到判定窗結束)
|
|
if (now >= gridDisplaySpawner.GetHitTime(t.x, t.y))
|
|
{
|
|
gridSpawner.gridCells[t.x, t.y].FlashBlue();
|
|
gridDisplaySpawner.Clear(t.x, t.y);
|
|
AddScore(Judgement.Perfect, SLIDE_SCORE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void FinishHold(ActiveHold h, double now)
|
|
{
|
|
bool completed = now >= h.hitTime + h.duration - 0.02;
|
|
var cell = gridSpawner.gridCells[h.x, h.y];
|
|
|
|
if (completed)
|
|
{
|
|
// 按滿:頭判分數 + 持續段分數
|
|
cell.FlashBlue();
|
|
AddScore(h.head, NoteJudge.Score(h.head) + HOLD_FULL_SCORE);
|
|
}
|
|
else
|
|
{
|
|
// 提早放開:亮紅色、不給分
|
|
cell.FlashRed();
|
|
AddScore(Judgement.Miss, 0);
|
|
}
|
|
holdHintSpawner.StopProgressAt(h.x, h.y);
|
|
holds.Remove(h);
|
|
}
|
|
|
|
void HandleNoteExpired(int x, int y, string note)
|
|
{
|
|
gridSpawner.gridCells[x, y].FlashRed();
|
|
AddScore(Judgement.Miss, 0);
|
|
}
|
|
|
|
// 只做判定,不給分、不清除(None = 不在判定範圍)
|
|
Judgement JudgeHead(int x, int y, double pressTime)
|
|
{
|
|
Judgement j = NoteJudge.Evaluate(pressTime - gridDisplaySpawner.GetHitTime(x, y));
|
|
return j == Judgement.Miss ? Judgement.None : j;
|
|
}
|
|
|
|
void FlashJudgement(int x, int y, Judgement j)
|
|
{
|
|
var cell = gridSpawner.gridCells[x, y];
|
|
switch (j)
|
|
{
|
|
case Judgement.Perfect: cell.FlashBlue(); break;
|
|
case Judgement.Fast: cell.FlashGreen(); break;
|
|
case Judgement.Slow: cell.FlashOrange(); break;
|
|
case Judgement.Bad: cell.FlashRed(); break;
|
|
}
|
|
}
|
|
|
|
// Tap:判定、給分、清除;回傳是否有判定到音符
|
|
bool ProcessTap(int x, int y, double pressTime)
|
|
{
|
|
Judgement j = JudgeHead(x, y, pressTime);
|
|
if (j == Judgement.None) return false;
|
|
|
|
FlashJudgement(x, y, j);
|
|
AddScore(j, NoteJudge.Score(j));
|
|
gridDisplaySpawner.Clear(x, y);
|
|
return true;
|
|
}
|
|
|
|
private void AddScore(Judgement j, int amount)
|
|
{
|
|
Result.Add(j, amount);
|
|
ShowScore();
|
|
}
|
|
|
|
private void ShowScore()
|
|
{
|
|
if (scoreLabel != null)
|
|
scoreLabel.text = Result.score.ToString();
|
|
}
|
|
}
|