using System; using UnityEngine; public class LevelManager : MonoBehaviour { public static LevelManager Instance { get; private set; } [SerializeField] private bool _debugMode = false; [SerializeField] private Door _doorReference; private bool _keyCollected = false; private bool _levelComplete = false; public bool KeyCollected => _keyCollected; public bool LevelComplete => _levelComplete; public event EventHandler OnKeyCollected; public event EventHandler OnDoorUnlocked; public event EventHandler OnLevelComplete; private void Awake() { // Singleton pattern if (Instance != null && Instance != this) { Destroy(gameObject); return; } Instance = this; } private void Start() { // Subscribe to GameManager key state changes if (GameManager.Instance != null) { GameManager.Instance.OnKeyStateChanged += HandleKeyStateChanged; } } public void NotifyKeyCollected() { if (_keyCollected) return; _keyCollected = true; if (_debugMode) Debug.Log("[LevelManager] Key collected!"); OnKeyCollected?.Invoke(this, EventArgs.Empty); UnlockDoor(); } public void UnlockDoor() { if (_doorReference == null) { Debug.LogWarning("[LevelManager] Door reference not assigned!"); return; } _doorReference.OpenDoor(); if (_debugMode) Debug.Log("[LevelManager] Door unlocked!"); OnDoorUnlocked?.Invoke(this, EventArgs.Empty); } public void NotifyLevelComplete() { if (_levelComplete) return; _levelComplete = true; if (_debugMode) Debug.Log("[LevelManager] Level complete!"); OnLevelComplete?.Invoke(this, EventArgs.Empty); // Also notify GameManager if (GameManager.Instance != null) { GameManager.Instance.CompletLevel(); } } public void Reset() { _keyCollected = false; _levelComplete = false; if (_debugMode) Debug.Log("[LevelManager] Reset"); } private void HandleKeyStateChanged(object sender, bool hasKey) { if (hasKey && !_keyCollected) { NotifyKeyCollected(); } } private void OnDestroy() { if (GameManager.Instance != null) { GameManager.Instance.OnKeyStateChanged -= HandleKeyStateChanged; } } }