Kursach_StealthAction/Assets/Scripts/NoteViewerUI.cs

146 lines
4.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
/// <summary>
/// Полноэкранный просмотрщик найденных записок.
/// Отрисовывает крупную цифру на бумажке, её позицию в терминале лифта (1-я, 2-я, 3-я или 4-я),
/// гриф секретности и атмосферный сюжетный текст.
/// </summary>
public class NoteViewerUI : MonoBehaviour
{
private static NoteViewerUI _instance;
public static NoteViewerUI Instance
{
get
{
if (_instance == null)
{
_instance = Object.FindAnyObjectByType<NoteViewerUI>(FindObjectsInactive.Include);
}
return _instance;
}
}
[Header("Корневая панель")]
[SerializeField] private GameObject notePanelRoot;
[Header("Элементы документа")]
[SerializeField] private Text documentHeader;
[SerializeField] private Text sectorText;
[SerializeField] private Text positionNoticeText;
[SerializeField] private Text bigDigitText;
[SerializeField] private Text flavorBodyText;
[SerializeField] private Button closeButton;
private bool isOpen = false;
public bool IsOpen => isOpen;
private void Awake()
{
_instance = this;
if (notePanelRoot == null)
notePanelRoot = gameObject;
if (closeButton != null)
{
closeButton.onClick.RemoveAllListeners();
closeButton.onClick.AddListener(CloseNote);
}
}
private int openFrame = -1;
private void Update()
{
if (!isOpen) return;
if (Time.frameCount == openFrame) return;
bool closePressed = false;
var keyboard = Keyboard.current;
if (keyboard != null)
{
if (keyboard.escapeKey.wasPressedThisFrame ||
keyboard.eKey.wasPressedThisFrame ||
keyboard.spaceKey.wasPressedThisFrame ||
keyboard.enterKey.wasPressedThisFrame)
{
closePressed = true;
}
}
#if ENABLE_LEGACY_INPUT_MANAGER
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Space))
{
closePressed = true;
}
#endif
if (closePressed)
{
CloseNote();
}
}
public void OpenNote(int positionIndex, int digit, string sector, string noteTitle, string storyText)
{
openFrame = Time.frameCount;
if (!gameObject.activeSelf) gameObject.SetActive(true);
if (notePanelRoot != null && !notePanelRoot.activeSelf)
notePanelRoot.SetActive(true);
isOpen = true;
Time.timeScale = 0f;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
if (documentHeader != null)
documentHeader.text = "Секретный протокол";
if (sectorText != null)
sectorText.text = $"Отсек: {sector.ToUpper()}";
string posStr = positionIndex switch
{
0 => "1-я цифра [ _ * * * ]",
1 => "2-я цифра [ * _ * * ]",
2 => "3-я цифра [ * * _ * ]",
3 => "4-я цифра [ * * * _ ]",
_ => $"{positionIndex + 1}-я цифра"
};
if (positionNoticeText != null)
positionNoticeText.text = $"КОД ЛИФТА: {posStr}";
if (bigDigitText != null)
bigDigitText.text = digit.ToString();
if (flavorBodyText != null)
flavorBodyText.text = storyText;
GameAudioSynthesizer.PlayPaperRustle();
if (LevelCodeManager.Instance != null)
{
LevelCodeManager.Instance.DiscoverDigit(positionIndex);
}
}
public void CloseNote()
{
if (!isOpen) return;
isOpen = false;
if (notePanelRoot != null)
notePanelRoot.SetActive(false);
Time.timeScale = 1f;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
GameAudioSynthesizer.PlayKeypadClear();
}
}