Kursach_StealthAction/Assets/Scripts/LevelCodeManager.cs

110 lines
3.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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