mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-01 08:35:34 +00:00
103 lines
2.6 KiB
C#
103 lines
2.6 KiB
C#
using Godot;
|
|
using System;
|
|
using Cirno.Scripts;
|
|
using Godot.Collections;
|
|
|
|
public partial class ItemsMenu : ItemList
|
|
{
|
|
private InventoryManager _inventoryManager;
|
|
private Dictionary<long, string> _itemsDic = new();
|
|
|
|
private GameManager _gameManager;
|
|
|
|
[Export]
|
|
public string InventoryActionName { get; private set; } = "inventory";
|
|
|
|
[Export]
|
|
public string PauseActionName { get; private set; } = "pause";
|
|
|
|
public override void _Ready()
|
|
{
|
|
CallDeferred(MethodName.DeferredInitialize);
|
|
this.Hide();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (Input.IsActionJustPressed(InventoryActionName) || Input.IsActionJustPressed(PauseActionName))
|
|
{
|
|
if (Visible)
|
|
{
|
|
CallDeferred(MethodName.HideInventory);
|
|
}
|
|
// else
|
|
// {
|
|
// ShowInventory();
|
|
// }
|
|
}
|
|
}
|
|
|
|
private void DeferredInitialize()
|
|
{
|
|
_gameManager = GameManager.Instance;
|
|
_inventoryManager = _gameManager.GetInventoryManager();
|
|
|
|
ItemSelected += OnItemSelected;
|
|
Clear();
|
|
|
|
ItemActivated += OnItemSelected;
|
|
|
|
_gameManager.GameStateChange += state =>
|
|
{
|
|
switch (state)
|
|
{
|
|
case GameState.Inventory:
|
|
CallDeferred(MethodName.ShowInventory);
|
|
break;
|
|
default:
|
|
CallDeferred(MethodName.HideInventory);
|
|
//HideInventory();
|
|
break;
|
|
}
|
|
};
|
|
}
|
|
|
|
private void OnItemSelected(long index)
|
|
{
|
|
var item = _itemsDic[index];
|
|
GD.Print("Item: " + item);
|
|
HideInventory();
|
|
|
|
_inventoryManager.UseItem(item);
|
|
}
|
|
|
|
private void HideInventory()
|
|
{
|
|
if (!Visible) return;
|
|
GD.Print("Hiding inventory");
|
|
this.Hide();
|
|
Clear();
|
|
_itemsDic.Clear();
|
|
|
|
GameManager.Instance.ChangeState(GameState.Playing);
|
|
}
|
|
|
|
private void ShowInventory()
|
|
{
|
|
if (Visible) return;
|
|
GD.Print("Showing inventory");
|
|
this.Show();
|
|
foreach (var item in _inventoryManager.Items)
|
|
{
|
|
if (item.Count <= 0) continue;
|
|
|
|
var index = this.AddItem($"{item.Item.ItemName} x{item.Count}", item.Item.InventorySprite,
|
|
item.Item.Selectable);
|
|
|
|
this.SetItemTooltip(index, item.Item.ItemDescription);
|
|
|
|
_itemsDic.Add(index, item.Item.ItemKey);
|
|
|
|
}
|
|
}
|
|
}
|