first commit
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Inventory : MonoBehaviour
|
||||
{
|
||||
private const string PanelKey = "Inventory";
|
||||
|
||||
public static Inventory Instance { get; private set; }
|
||||
|
||||
[SerializeField] private GameObject inventoryPanel;
|
||||
[SerializeField] private Transform listRoot;
|
||||
[SerializeField] private Button noteButtonTemplate;
|
||||
[SerializeField] private Text emptyStateText;
|
||||
[SerializeField] private Button closeButton;
|
||||
[SerializeField] private List<NoteData> collectedNotes = new List<NoteData>();
|
||||
|
||||
public bool IsOpen => inventoryPanel != null && inventoryPanel.activeSelf;
|
||||
|
||||
public void Configure(GameObject panel, Transform notesRoot, Button templateButton, Text emptyText, Button closePanelButton)
|
||||
{
|
||||
inventoryPanel = panel;
|
||||
listRoot = notesRoot;
|
||||
noteButtonTemplate = templateButton;
|
||||
emptyStateText = emptyText;
|
||||
closeButton = closePanelButton;
|
||||
|
||||
if (inventoryPanel != null)
|
||||
{
|
||||
inventoryPanel.SetActive(false);
|
||||
}
|
||||
|
||||
if (noteButtonTemplate != null)
|
||||
{
|
||||
noteButtonTemplate.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
|
||||
if (closeButton != null)
|
||||
{
|
||||
closeButton.onClick.RemoveListener(CloseInventory);
|
||||
closeButton.onClick.AddListener(CloseInventory);
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!Input.GetKeyDown(KeyCode.I))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsOpen)
|
||||
{
|
||||
CloseInventory();
|
||||
}
|
||||
else
|
||||
{
|
||||
OpenInventory();
|
||||
}
|
||||
}
|
||||
|
||||
public void AddNote(NoteData note)
|
||||
{
|
||||
if (note == null || string.IsNullOrWhiteSpace(note.title))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var storedNote in collectedNotes)
|
||||
{
|
||||
if (storedNote.title == note.title && storedNote.content == note.content)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
collectedNotes.Add(new NoteData(note.title, note.content));
|
||||
RebuildNoteList();
|
||||
}
|
||||
|
||||
public void OpenInventory()
|
||||
{
|
||||
if (inventoryPanel == null || listRoot == null || noteButtonTemplate == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (NoteUI.Instance != null && NoteUI.Instance.IsOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (DialogueUI.Instance != null && DialogueUI.Instance.IsOpen)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RebuildNoteList();
|
||||
inventoryPanel.SetActive(true);
|
||||
GameUIState.SetPanelOpen(PanelKey, true);
|
||||
}
|
||||
|
||||
public void CloseInventory()
|
||||
{
|
||||
if (inventoryPanel == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
inventoryPanel.SetActive(false);
|
||||
GameUIState.SetPanelOpen(PanelKey, false);
|
||||
}
|
||||
|
||||
private void RebuildNoteList()
|
||||
{
|
||||
if (listRoot == null || noteButtonTemplate == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = listRoot.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
var child = listRoot.GetChild(i);
|
||||
|
||||
if (child == noteButtonTemplate.transform)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
|
||||
var hasNotes = collectedNotes.Count > 0;
|
||||
|
||||
if (emptyStateText != null)
|
||||
{
|
||||
emptyStateText.gameObject.SetActive(!hasNotes);
|
||||
emptyStateText.text = "Пока что здесь пусто. Найдите записки на уровне.";
|
||||
}
|
||||
|
||||
foreach (var note in collectedNotes)
|
||||
{
|
||||
var noteCopy = note;
|
||||
var noteButton = Instantiate(noteButtonTemplate, listRoot);
|
||||
noteButton.gameObject.SetActive(true);
|
||||
noteButton.onClick.RemoveAllListeners();
|
||||
noteButton.onClick.AddListener(() => OpenStoredNote(noteCopy));
|
||||
|
||||
var buttonText = noteButton.GetComponentInChildren<Text>();
|
||||
|
||||
if (buttonText != null)
|
||||
{
|
||||
buttonText.text = note.title;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenStoredNote(NoteData note)
|
||||
{
|
||||
CloseInventory();
|
||||
|
||||
if (NoteUI.Instance != null)
|
||||
{
|
||||
NoteUI.Instance.ShowNote(note.title, note.content);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user