103 lines
3.0 KiB
C#
103 lines
3.0 KiB
C#
using System.Collections.Generic;
|
||
using Unity.Cinemachine;
|
||
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
|
||
public class MusicGame : MonoBehaviour
|
||
{
|
||
public static MusicGame Instance { get; private set; }
|
||
public MusicData musicData;
|
||
private bool isPlaying = false;
|
||
public Camera mainCamera;
|
||
private int tickId = 0;
|
||
private int noteId = 0;
|
||
|
||
public Vector3 judgeLine;
|
||
private List<GameObject> tickNote = new List<GameObject>();
|
||
double startDsp;
|
||
|
||
void Start()
|
||
{
|
||
mainCamera = Camera.main;
|
||
Instance = this;
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (Keyboard.current.qKey.wasPressedThisFrame) Play();
|
||
}
|
||
|
||
public void Play()
|
||
{
|
||
mainCamera.transform.position = new Vector3(0, 100, -10);
|
||
mainCamera.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
|
||
mainCamera.GetComponent<CinemachineBrain>().enabled = false;
|
||
isPlaying = true;
|
||
judgeLine = mainCamera.transform.position + new Vector3(0, -2f, 8);
|
||
startDsp = AudioSettings.dspTime; // 音频原点
|
||
gameObject.GetComponent<AudioSource>().Play();
|
||
tickNote.ForEach(x => Destroy(x));
|
||
tickNote.Clear();
|
||
foreach (MusicTick tick in musicData.musicTicks)
|
||
ActivateTick(tick);
|
||
}
|
||
|
||
private void ActivateTick(MusicTick tick)
|
||
{
|
||
// 清除旧音符
|
||
// tickNote.ForEach(x => Destroy(x));
|
||
// tickNote.Clear();
|
||
|
||
// double startDsp = AudioSettings.dspTime; // 音频原点
|
||
|
||
for (int i = 0; i < tick.array.Length; i++)
|
||
{
|
||
if (tick.array[i] != null)
|
||
{
|
||
GameObject obj = Instantiate(tick.array[i], GetSpawnPosition(i), Quaternion.identity);
|
||
Note note = obj.GetComponent<Note>();
|
||
note.beatTime = startDsp + tick.times[i]; // 直接给绝对时刻
|
||
note.Uid = i;
|
||
tickNote.Add(obj);
|
||
}
|
||
}
|
||
// 不排队、不LaunchNext,每个音符自己到点动
|
||
}
|
||
|
||
Vector3 GetSpawnPosition(int index)
|
||
{
|
||
return mainCamera.transform.position + new Vector3(-2 + index * 1.5f, 6, 8);
|
||
}
|
||
|
||
public void onNoteHit(int uid)
|
||
{
|
||
if (!isPlaying) return;
|
||
noteId++;
|
||
// 顺序判定仍保留,只用于得分
|
||
if (uid != -1 && musicData.musicTicks[tickId].array[noteId].GetComponent<Note>().Uid == uid)
|
||
{
|
||
|
||
Debug.Log($"击中正确按键: {uid}");
|
||
}
|
||
if (noteId >= musicData.musicTicks[tickId].array.Length) FinshTick();
|
||
}
|
||
|
||
public void FinshTick()
|
||
{
|
||
Debug.Log($"完成tick: {tickId}");
|
||
tickId++;
|
||
noteId = 0;
|
||
if (tickId < musicData.musicTicks.Count)
|
||
{
|
||
}
|
||
// ActivateTick(musicData.musicTicks[tickId]);
|
||
else
|
||
{
|
||
isPlaying = false;
|
||
tickId = 0;
|
||
noteId = 0;
|
||
mainCamera.GetComponent<CinemachineBrain>().enabled = true;
|
||
Debug.Log("完成所有tick");
|
||
}
|
||
}
|
||
} |