139 lines
3.7 KiB
C#
139 lines
3.7 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
// 單一格子的顯示:靜態圖示(選單)或音符
|
|
// 音符動畫完全由 SongClock 決定,保證在 hitTime 時縮放剛好為 1
|
|
public class GridDisplay : MonoBehaviour
|
|
{
|
|
public Image image;
|
|
|
|
// 音符在判定點前多久出現(秒)
|
|
public const double AppearLead = 0.5;
|
|
// 出現時的圓形填滿動畫長度
|
|
private const double FillDuration = 0.2;
|
|
// 判定點前後的縮放區間:0.5 → 1 → 1.5
|
|
private const double GrowHalf = 0.2;
|
|
|
|
public int cellX;
|
|
public int cellY;
|
|
|
|
// 音符超過判定時間仍未被按 → 通知(Miss)
|
|
public System.Action<GridDisplay, string> OnExpired;
|
|
|
|
private RectTransform rt;
|
|
private Vector2 basePos;
|
|
private Image.Type baseType;
|
|
|
|
private bool active = false;
|
|
private string currentNote = "None";
|
|
private int currentSec = 0;
|
|
private double hitTime;
|
|
|
|
void Awake()
|
|
{
|
|
if (image == null)
|
|
image = GetComponent<Image>();
|
|
rt = GetComponent<RectTransform>();
|
|
baseType = image.type;
|
|
enabled = false; // 沒有音符時不跑 Update
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!active) return;
|
|
|
|
double t = SongClock.Now - hitTime;
|
|
if (t > NoteJudge.BadWindow)
|
|
{
|
|
string note = currentNote;
|
|
Clear();
|
|
OnExpired?.Invoke(this, note);
|
|
return;
|
|
}
|
|
UpdateVisual(t);
|
|
}
|
|
|
|
public void ShowNote(Sprite sprite, string note, int sec, double noteHitTime)
|
|
{
|
|
// 同格還有未處理的音符:視為 Miss,避免音符無聲消失造成統計少算
|
|
if (active)
|
|
{
|
|
string old = currentNote;
|
|
Clear();
|
|
OnExpired?.Invoke(this, old);
|
|
}
|
|
|
|
basePos = rt.anchoredPosition;
|
|
currentNote = note;
|
|
currentSec = sec;
|
|
hitTime = noteHitTime;
|
|
active = true;
|
|
enabled = true;
|
|
|
|
image.sprite = sprite;
|
|
image.enabled = true;
|
|
image.type = Image.Type.Filled;
|
|
image.fillMethod = Image.FillMethod.Radial360;
|
|
image.fillOrigin = 2;
|
|
image.fillClockwise = true;
|
|
|
|
UpdateVisual(SongClock.Now - hitTime);
|
|
}
|
|
|
|
// t = 目前時間 - 判定時間
|
|
private void UpdateVisual(double t)
|
|
{
|
|
double sinceAppear = t + AppearLead;
|
|
float fill = (float)System.Math.Min(1.0, System.Math.Max(0.0, sinceAppear / FillDuration));
|
|
float scale = t < -GrowHalf ? 0.5f : (float)(1.0 + t / (2 * GrowHalf));
|
|
float alpha = scale > 1f ? Mathf.Clamp01(1f - (scale - 1f) / 0.5f) : 1f;
|
|
|
|
image.fillAmount = fill;
|
|
SetAlpha(alpha);
|
|
|
|
Vector2 size = rt.sizeDelta;
|
|
rt.localScale = Vector3.one * scale;
|
|
rt.anchoredPosition = basePos + (1f - scale) * size / 2f;
|
|
}
|
|
|
|
public void SetImage(Sprite sprite)
|
|
{
|
|
if (active) Clear();
|
|
image.sprite = sprite;
|
|
image.enabled = (sprite != null);
|
|
SetAlpha(1f);
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
if (active)
|
|
{
|
|
rt.localScale = Vector3.one;
|
|
rt.anchoredPosition = basePos;
|
|
image.type = baseType;
|
|
image.fillAmount = 1f;
|
|
active = false;
|
|
enabled = false;
|
|
}
|
|
SetAlpha(1f);
|
|
currentSec = 0;
|
|
currentNote = "None";
|
|
image.sprite = null;
|
|
image.enabled = false;
|
|
}
|
|
|
|
public void SetAlpha(float a)
|
|
{
|
|
if (image == null) return;
|
|
|
|
Color c = image.color;
|
|
c.a = a;
|
|
image.color = c;
|
|
}
|
|
|
|
public bool HasNote() => active;
|
|
public string GetNote() => currentNote;
|
|
public int GetSec() => currentSec;
|
|
public double GetHitTime() => hitTime;
|
|
}
|