using System; using UnityEngine; /// /// Менеджер сюжета и 4-значного случайного кода доступа к лифту на 2-й уровень. /// Генерирует уникальный случайный код при каждом старте игры и отслеживает найденные записки. /// public class LevelCodeManager : MonoBehaviour { private static LevelCodeManager _instance; public static LevelCodeManager Instance { get { if (_instance == null) { _instance = UnityEngine.Object.FindAnyObjectByType(FindObjectsInactive.Include); } return _instance; } } [Header("Сгенерированный код (4 цифры)")] [SerializeField] private int[] secretDigits = new int[4]; [SerializeField] private bool[] discoveredDigits = new bool[4]; public event Action OnDigitDiscovered; // (позиция 0..3, значение 0..9) public event Action OnElevatorUnlocked; public bool IsElevatorUnlocked { get; private set; } = false; private void Awake() { _instance = this; GenerateSecretCode(); } /// /// Генерирует новый 4-значный случайный код (например, 7-2-4-9) /// public void GenerateSecretCode() { IsElevatorUnlocked = false; for (int i = 0; i < 4; i++) { secretDigits[i] = UnityEngine.Random.Range(0, 10); discoveredDigits[i] = false; } Debug.Log($"[LevelCodeManager] Сгенерирован код лифта для Уровня 1: " + $"{secretDigits[0]} - {secretDigits[1]} - {secretDigits[2]} - {secretDigits[3]}"); } public int GetDigit(int position) { if (position >= 0 && position < 4) return secretDigits[position]; return 0; } public bool IsDigitDiscovered(int position) { if (position >= 0 && position < 4) return discoveredDigits[position]; return false; } public void DiscoverDigit(int position) { if (position >= 0 && position < 4) { bool wasDiscovered = discoveredDigits[position]; discoveredDigits[position] = true; if (!wasDiscovered) { OnDigitDiscovered?.Invoke(position, secretDigits[position]); Debug.Log($"[LevelCodeManager] Открыта цифра #{position + 1}: {secretDigits[position]}"); } } } public bool ValidateCode(string enteredCode) { if (string.IsNullOrEmpty(enteredCode) || enteredCode.Length != 4) return false; string correctCode = $"{secretDigits[0]}{secretDigits[1]}{secretDigits[2]}{secretDigits[3]}"; bool isCorrect = enteredCode.Trim().Equals(correctCode, StringComparison.OrdinalIgnoreCase); if (isCorrect) { IsElevatorUnlocked = true; OnElevatorUnlocked?.Invoke(); Debug.Log("[LevelCodeManager] Код лифта введен ВЕРНО! Доступ к Уровню 2 открыт."); } else { Debug.Log($"[LevelCodeManager] Неверный код: '{enteredCode}' (ожидался '{correctCode}')"); } return isCorrect; } public string GetFullCodeString() { return $"{secretDigits[0]}{secretDigits[1]}{secretDigits[2]}{secretDigits[3]}"; } }