cirnogodot/Scripts/UI/ItemsMenu.cs

103 lines
2.6 KiB
C#
Raw Normal View History

2025-02-25 18:11:57 +01:00
using Godot;
using System;
using Cirno.Scripts;
using Godot.Collections;
public partial class ItemsMenu : ItemList
{
private InventoryManager _inventoryManager;
private Dictionary<long, string> _itemsDic = new();
2025-03-01 20:50:47 +01:00
private GameManager _gameManager;
[Export]
public string InventoryActionName { get; private set; } = "inventory";
[Export]
public string PauseActionName { get; private set; } = "pause";
2025-02-25 18:11:57 +01:00
public override void _Ready()
{
CallDeferred(MethodName.DeferredInitialize);
this.Hide();
}
public override void _Process(double delta)
{
2025-03-01 20:50:47 +01:00
if (Input.IsActionJustPressed(InventoryActionName) || Input.IsActionJustPressed(PauseActionName))
2025-02-25 18:11:57 +01:00
{
2025-03-01 20:50:47 +01:00
if (Visible)
2025-02-25 18:11:57 +01:00
{
2025-03-01 20:50:47 +01:00
CallDeferred(MethodName.HideInventory);
2025-02-25 18:11:57 +01:00
}
2025-03-01 20:50:47 +01:00
// else
// {
// ShowInventory();
// }
2025-02-25 18:11:57 +01:00
}
}
private void DeferredInitialize()
{
2025-03-01 20:50:47 +01:00
_gameManager = GameManager.Instance;
_inventoryManager = _gameManager.GetInventoryManager();
2025-02-25 18:11:57 +01:00
ItemSelected += OnItemSelected;
Clear();
ItemActivated += OnItemSelected;
2025-03-01 20:50:47 +01:00
_gameManager.GameStateChange += state =>
{
switch (state)
{
case GameState.Inventory:
CallDeferred(MethodName.ShowInventory);
break;
default:
CallDeferred(MethodName.HideInventory);
//HideInventory();
break;
}
};
2025-02-25 18:11:57 +01:00
}
private void OnItemSelected(long index)
{
var item = _itemsDic[index];
GD.Print("Item: " + item);
HideInventory();
2025-03-01 20:50:47 +01:00
_inventoryManager.UseItem(item);
2025-02-25 18:11:57 +01:00
}
2025-03-01 20:50:47 +01:00
private void HideInventory()
2025-02-25 18:11:57 +01:00
{
2025-03-01 20:50:47 +01:00
if (!Visible) return;
GD.Print("Hiding inventory");
2025-02-25 18:11:57 +01:00
this.Hide();
Clear();
_itemsDic.Clear();
2025-03-01 20:50:47 +01:00
GameManager.Instance.ChangeState(GameState.Playing);
2025-02-25 18:11:57 +01:00
}
2025-03-01 20:50:47 +01:00
private void ShowInventory()
2025-02-25 18:11:57 +01:00
{
2025-03-01 20:50:47 +01:00
if (Visible) return;
GD.Print("Showing inventory");
2025-02-25 18:11:57 +01:00
this.Show();
foreach (var item in _inventoryManager.Items)
2025-02-25 18:11:57 +01:00
{
if (item.Count <= 0) continue;
2025-02-25 18:11:57 +01:00
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);
}
}
}