using System.IO; using System.Linq; using UnityEngine; using static HoldNote; public enum NoteScore { Perfect, Good, Okay, Miss } public class GameManager : MonoBehaviour { public static GameManager Instance { get; private set; } public BeatmapManager beatmapManager; // 负责加载和管理谱面 public NoteSpawner noteSpawner; // 音符生成器 public AudioSource musicSource; // 音乐播放器 public int CurrentScore { get; private set; } public int MaxCombo { get; private set; } public int CurrentCombo { get; private set; } // 添加计分方法 public void AddScore(NoteScore scoreType) { // 计分逻辑 switch (scoreType) { case NoteScore.Perfect: CurrentScore += 100; Debug.Log("Perfect! +100"); break; case NoteScore.Good: CurrentScore += 80; Debug.Log("Good! +80"); break; case NoteScore.Okay: CurrentScore += 50; Debug.Log("Okay +50"); break; case NoteScore.Miss: CurrentCombo = 0; // 连击中断 Debug.Log("Miss! 连击中断"); return; // 不增加分数 } // 连击处理 CurrentCombo++; if (CurrentCombo > MaxCombo) MaxCombo = CurrentCombo; } private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; DontDestroyOnLoad(gameObject); } } void Start() { //Debug.Log("GameManager Start() 被调用"); // 检查关键组件是否为空 if (beatmapManager == null) Debug.LogError("beatmapManager 未赋值!"); if (noteSpawner == null) Debug.LogError("noteSpawner 未赋值!"); if (musicSource == null) Debug.LogError("musicSource 未赋值!"); // 在游戏开始时加载并初始化谱面 string beatmapFilePath = Path.Combine(Application.streamingAssetsPath, "Emilia_demo.json"); if (!File.Exists(beatmapFilePath)) { //Debug.LogError($"谱面文件不存在: {beatmapFilePath}"); return; } string json = File.ReadAllText(beatmapFilePath); Beatmap beatmap = JsonUtility.FromJson(json); // 解析 JSON 为 Beatmap 对象 if (beatmap == null) { //Debug.LogError("谱面解析失败,JSON 格式可能错误!"); return; } beatmapManager.LoadBeatmap(beatmap); // 传递给 BeatmapManager noteSpawner.LoadBeatmap(beatmap); // 传递给 NoteSpawner // 播放背景音乐 if (!string.IsNullOrEmpty(beatmap.musicFile)) { string musicPath = Path.Combine("song_songIndex/1002001_emilia", Path.GetFileNameWithoutExtension(beatmap.musicFile)); AudioClip musicClip = Resources.Load(musicPath); if (musicClip == null) { //Debug.LogError($"音乐文件加载失败,尝试路径: {musicPath}"); // 列出Resources文件夹下所有音频文件用于调试 var allAudio = Resources.LoadAll(""); //Debug.Log("可用音频文件:" + string.Join(", ", allAudio.Select(a => a.name))); } else { musicSource.clip = musicClip; musicSource.Play(); } } else { //Debug.LogError("谱面中 musicFile 为空!"); } } }