using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public static GameManager Instance; [Header("游戏 UI")] public GameObject losePanel; public GameObject winPanel; private BoatController boatController; private bool isGameOver = false; private bool isWin = false; private SeaMonster[] seaMonsters; private PirateShip[] pirateShips; void Awake() { Instance = this; } void Start() { boatController = FindObjectOfType (); seaMonsters = FindObjectsOfType(); pirateShips = FindObjectsOfType(); if (losePanel != null) losePanel.SetActive(false); if (winPanel != null) winPanel.SetActive(false); } public void PauseEnemies() { foreach (var monster in seaMonsters) if (monster != null) monster.SetPaused(true); foreach (var ship in pirateShips) if (ship != null) ship.SetPaused(true); } public void ResumeEnemies() { foreach (var monster in seaMonsters) if (monster != null) monster.SetPaused(false); foreach (var ship in pirateShips) if (ship != null) ship.SetPaused(false); } public void GameOver() { if (isGameOver || isWin) return; isGameOver = true; Debug.Log("💀 游戏结束!"); if (boatController != null) boatController.StopAndTurnAround(); PauseEnemies(); StartCoroutine(ShowLosePanel()); } public void WinGame() { if (isGameOver || isWin) return; isWin = true; Debug.Log("🎉 游戏胜利!"); if (boatController != null) boatController.StopAndTurnAround(); PauseEnemies(); StartCoroutine(ShowWinPanel()); } IEnumerator ShowLosePanel() { yield return new WaitForSeconds(0.5f); if (losePanel != null) losePanel.SetActive(true); Time.timeScale = 0f; } IEnumerator ShowWinPanel() { // 动画已经播完才调用 // 只需短暂等待 yield return new WaitForSeconds(0.5f); if (winPanel != null) winPanel.SetActive(true); Time.timeScale = 0f; } public void RestartGame() { Time.timeScale = 1f; isGameOver = false; isWin = false; SceneManager.LoadScene( SceneManager.GetActiveScene().name ); } }