59 lines
1.3 KiB
C#
59 lines
1.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
/// <summary>
|
|
/// Manages game state and responds to core destruction.
|
|
/// </summary>
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private Core coreReference;
|
|
|
|
private bool _isGameOver = false;
|
|
private HealthComponent _coreHealth;
|
|
|
|
private void Start()
|
|
{
|
|
// Find Core if not assigned
|
|
if (coreReference == null)
|
|
{
|
|
coreReference = FindFirstObjectByType<Core>();
|
|
}
|
|
|
|
// Subscribe to core's health component
|
|
if (coreReference != null)
|
|
{
|
|
_coreHealth = coreReference.Health;
|
|
if (_coreHealth != null)
|
|
{
|
|
_coreHealth.OnDeath += GameOver;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
// Unsubscribe to prevent memory leaks
|
|
if (_coreHealth != null)
|
|
{
|
|
_coreHealth.OnDeath -= GameOver;
|
|
}
|
|
}
|
|
|
|
private void GameOver()
|
|
{
|
|
if (_isGameOver) return;
|
|
|
|
_isGameOver = true;
|
|
Debug.Log("Game Over! Core has been destroyed.");
|
|
|
|
// Show defeat UI here
|
|
// Example: restart game after 3 seconds
|
|
Invoke(nameof(RestartGame), 3f);
|
|
}
|
|
|
|
private void RestartGame()
|
|
{
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
|
|
}
|
|
}
|