173 lines
5.3 KiB
C#
173 lines
5.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class GridDisplaySpawner : MonoBehaviour
|
|
{
|
|
public GameObject displayPrefab;
|
|
public int rows = 6;
|
|
public int columns = 5;
|
|
public Sprite imgNoteTap;
|
|
public Sprite imgNoteHold;
|
|
public Sprite imgNoteSlide;
|
|
public GridDisplay[,] displayCells;
|
|
|
|
// 音符過期未被按(x, y, 音符種類)
|
|
public event System.Action<int, int, string> NoteExpired;
|
|
|
|
[Header("Base Layout Settings")]
|
|
public float baseWidth = 1080f;
|
|
public float cellBase = 180f;
|
|
public float spaceBase = 20f;
|
|
public float heightBase = 90f;
|
|
|
|
// 格子上的文字(例如選歌頁的難度數字)
|
|
private readonly Dictionary<(int, int), Text> labels = new Dictionary<(int, int), Text>();
|
|
private float cellSize;
|
|
|
|
void Awake()
|
|
{
|
|
float scale = Screen.width / baseWidth;
|
|
|
|
cellSize = cellBase * scale;
|
|
float spacing = spaceBase * scale;
|
|
float heightOffset = heightBase * scale;
|
|
|
|
displayCells = new GridDisplay[columns, rows];
|
|
|
|
RectTransform rt = GetComponent<RectTransform>();
|
|
if (rt == null) rt = gameObject.AddComponent<RectTransform>();
|
|
|
|
rt.anchorMin = new Vector2(0.5f, 0f);
|
|
rt.anchorMax = new Vector2(0.5f, 0f);
|
|
rt.pivot = new Vector2(0.5f, 0f);
|
|
|
|
float totalWidth = columns * cellSize + (columns - 1) * spacing;
|
|
float startX = -totalWidth / 2f;
|
|
float startY = heightOffset;
|
|
|
|
for (int i = 0; i < rows * columns; i++)
|
|
{
|
|
GameObject obj = Instantiate(displayPrefab, transform);
|
|
|
|
int x = i % columns;
|
|
int y = i / columns;
|
|
|
|
RectTransform cellRT = obj.GetComponent<RectTransform>();
|
|
cellRT.anchorMin = new Vector2(0f, 0f);
|
|
cellRT.anchorMax = new Vector2(0f, 0f);
|
|
cellRT.pivot = new Vector2(0f, 0f);
|
|
|
|
cellRT.sizeDelta = new Vector2(cellSize, cellSize);
|
|
|
|
float posX = startX + x * (cellSize + spacing);
|
|
float posY = startY + y * (cellSize + spacing);
|
|
cellRT.anchoredPosition = new Vector2(posX, posY);
|
|
|
|
GridDisplay cell = obj.GetComponent<GridDisplay>();
|
|
cell.cellX = x;
|
|
cell.cellY = y;
|
|
cell.OnExpired = (c, note) => NoteExpired?.Invoke(c.cellX, c.cellY, note);
|
|
displayCells[x, y] = cell;
|
|
}
|
|
}
|
|
|
|
public void SetImage(int x, int y, Sprite image)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
displayCells[x, y].SetImage(image);
|
|
}
|
|
|
|
public void Clear(int x, int y)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
displayCells[x, y].Clear();
|
|
RemoveLabel((x, y));
|
|
}
|
|
|
|
// 在格子上顯示文字;text 為 null 或空字串時移除
|
|
public void SetLabel(int x, int y, string text, Color color, float fontScale = 0.55f)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
var key = (x, y);
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
RemoveLabel(key);
|
|
return;
|
|
}
|
|
|
|
if (!labels.TryGetValue(key, out var label) || label == null)
|
|
{
|
|
var go = new GameObject("Label", typeof(RectTransform));
|
|
go.transform.SetParent(displayCells[x, y].transform, false);
|
|
label = go.AddComponent<Text>();
|
|
label.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf");
|
|
label.fontStyle = FontStyle.Bold;
|
|
label.alignment = TextAnchor.MiddleCenter;
|
|
label.horizontalOverflow = HorizontalWrapMode.Overflow;
|
|
label.verticalOverflow = VerticalWrapMode.Overflow;
|
|
label.raycastTarget = false;
|
|
var rt = label.rectTransform;
|
|
rt.anchorMin = Vector2.zero;
|
|
rt.anchorMax = Vector2.one;
|
|
rt.offsetMin = rt.offsetMax = Vector2.zero;
|
|
labels[key] = label;
|
|
}
|
|
label.text = text;
|
|
label.color = color;
|
|
label.fontSize = Mathf.Max(1, Mathf.RoundToInt(cellSize * fontScale));
|
|
}
|
|
|
|
private void RemoveLabel((int, int) key)
|
|
{
|
|
if (labels.TryGetValue(key, out var label) && label != null) Destroy(label.gameObject);
|
|
labels.Remove(key);
|
|
}
|
|
|
|
public void PlayNoteTap(int x, int y, double hitTime)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
displayCells[x, y].ShowNote(imgNoteTap, "Tap", 0, hitTime);
|
|
}
|
|
|
|
public void PlayNoteHold(int x, int y, int durationMs, double hitTime)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
displayCells[x, y].ShowNote(imgNoteHold, "Hold", durationMs, hitTime);
|
|
}
|
|
|
|
public void PlayNoteSlide(int x, int y, double hitTime)
|
|
{
|
|
if (!InBound(x, y)) return;
|
|
displayCells[x, y].ShowNote(imgNoteSlide, "Slide", 0, hitTime);
|
|
}
|
|
|
|
public bool HasNote(int x, int y)
|
|
{
|
|
return InBound(x, y) && displayCells[x, y].HasNote();
|
|
}
|
|
|
|
public double GetHitTime(int x, int y)
|
|
{
|
|
if (!InBound(x, y)) return 0;
|
|
return displayCells[x, y].GetHitTime();
|
|
}
|
|
|
|
public string GetCurrentNote(int x, int y)
|
|
{
|
|
if (!InBound(x, y)) return "None";
|
|
return displayCells[x, y].GetNote();
|
|
}
|
|
|
|
public int GetCurrentSec(int x, int y)
|
|
{
|
|
if (!InBound(x, y)) return 0;
|
|
return displayCells[x, y].GetSec();
|
|
}
|
|
|
|
private bool InBound(int x, int y)
|
|
{
|
|
return x >= 0 && y >= 0 && x < columns && y < rows;
|
|
}
|
|
}
|