Files
ProjectMD/Assets/Scripts/GameBase/GameManager.cs
Dal4segno db5db4b106 코드 리팩토링
재사용성 및 확장성을 고려하여 코드 전반을 리팩토링함
2026-01-21 01:45:15 +09:00

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);
}
}