Inventory close and open by button

This commit is contained in:
Marco 2025-03-01 20:50:47 +01:00
commit 80a13d047d
11 changed files with 165 additions and 53 deletions

View file

@ -7,6 +7,14 @@ 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()
{
@ -16,48 +24,68 @@ public partial class ItemsMenu : ItemList
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("inventory"))
if (Input.IsActionJustPressed(InventoryActionName) || Input.IsActionJustPressed(PauseActionName))
{
if (!Visible)
if (Visible)
{
ShowInventory();
}
else
{
HideInventory();
CallDeferred(MethodName.HideInventory);
}
// else
// {
// ShowInventory();
// }
}
}
private void DeferredInitialize()
{
_inventoryManager = GameManager.Instance.GetInventoryManager();
_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);
_inventoryManager.UseItem(item);
HideInventory();
_inventoryManager.UseItem(item);
}
public void HideInventory()
private void HideInventory()
{
if (!Visible) return;
GD.Print("Hiding inventory");
this.Hide();
Clear();
_itemsDic.Clear();
GameManager.Instance.ChangeState(GameState.Playing);
}
public void ShowInventory()
private void ShowInventory()
{
if (Visible) return;
GD.Print("Showing inventory");
this.Show();
foreach (var item in _inventoryManager.Items)
{