diff --git a/src/game/Game.cs b/src/game/Game.cs index 04712f48..2da727a7 100644 --- a/src/game/Game.cs +++ b/src/game/Game.cs @@ -102,8 +102,8 @@ public partial class Game : Node3D, IGame { CallDeferred(nameof(SetPauseMode), output.IsPaused); }) - .Handle((in GameLogic.Output.SetInventoryMode _) => { InventoryMenu.PopulateItems(); InventoryMenu.Show(); }) - .Handle((in GameLogic.Output.HideInventory _) => { InventoryMenu.Hide(); InventoryMenu.ClearItems(); }) + .Handle((in GameLogic.Output.SetInventoryMode _) => { InventoryMenu.RedrawInventory(); InventoryMenu.Show(); }) + .Handle((in GameLogic.Output.HideInventory _) => { InventoryMenu.Hide(); }) .Handle((in GameLogic.Output.ShowMiniMap _) => { MiniMap.Show(); }) .Handle((in GameLogic.Output.HideMiniMap _) => { MiniMap.Hide(); }) .Handle((in GameLogic.Output.GameOver _) => { AppRepo.OnGameOver(); }); diff --git a/src/inventory_menu/InventoryMenu.cs b/src/inventory_menu/InventoryMenu.cs index 8dc2e389..22269d86 100644 --- a/src/inventory_menu/InventoryMenu.cs +++ b/src/inventory_menu/InventoryMenu.cs @@ -8,9 +8,7 @@ using System.Threading.Tasks; public interface IInventoryMenu : IControl { - public void PopulateItems(); - - public void ClearItems(); + public Task RedrawInventory(); } [Meta(typeof(IAutoNode))] @@ -67,13 +65,56 @@ public partial class InventoryMenu : Control, IInventoryMenu private IItemSlot[] ItemSlots => ItemsPage.GetChildren().OfType().ToArray(); - public void PopulateItems() + public async Task RedrawInventory() + { + await HideUserActionPrompt(); + ClearItems(); + PopulateInventory(); + PopulatePlayerInfo(); + } + + public override void _UnhandledInput(InputEvent @event) + { + if (ItemSlots.Length == 0) + return; + + var inventory = GameRepo.InventoryItems.Value; + + if (@event.IsActionPressed(GameInputs.UiRight) && _currentPageNumber == InventoryPageNumber.FirstPage && inventory.Count > _itemsPerPage) + ChangeInventoryPage(InventoryPageNumber.SecondPage); + + if (@event.IsActionPressed(GameInputs.UiLeft) && _currentPageNumber == InventoryPageNumber.SecondPage) + ChangeInventoryPage(InventoryPageNumber.FirstPage); + + if (@event.IsActionPressed(GameInputs.UiDown)) + { + SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex)); + _currentIndex = new[] { _currentIndex + 1, _itemsPerPage - 1, ItemSlots.Length - 1 }.Min(); + SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex)); + } + + if (@event.IsActionPressed(GameInputs.UiUp)) + { + SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex)); + _currentIndex = new[] { _currentIndex - 1, 0 }.Max(); + SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex)); + } + + if (@event.IsActionPressed(GameInputs.UiAccept)) + { + if (ItemSlots.Length == 0 || GetViewport().GuiGetFocusOwner() is Button) + return; + DisplayUserActionPrompt(); + } + } + + private void PopulateItems() { PopulateInventory(); PopulatePlayerInfo(); } - public void ClearItems() + private void ClearItems() { foreach (var item in ItemSlots) ItemsPage.RemoveChildEx(item); @@ -81,7 +122,7 @@ public partial class InventoryMenu : Control, IInventoryMenu HideUserActionPrompt(); } - public void PopulatePlayerInfo() + private void PopulatePlayerInfo() { FloorLabel.Text = $"Level {GameRepo.CurrentFloor:D2}"; @@ -126,41 +167,6 @@ public partial class InventoryMenu : Control, IInventoryMenu } } - public override void _UnhandledInput(InputEvent @event) - { - if (ItemSlots.Length == 0) - return; - - var inventory = GameRepo.InventoryItems.Value; - - if (@event.IsActionPressed(GameInputs.UiRight) && _currentPageNumber == InventoryPageNumber.FirstPage && inventory.Count > _itemsPerPage) - ChangeInventoryPage(InventoryPageNumber.SecondPage); - - if (@event.IsActionPressed(GameInputs.UiLeft) && _currentPageNumber == InventoryPageNumber.SecondPage) - ChangeInventoryPage(InventoryPageNumber.FirstPage); - - if (@event.IsActionPressed(GameInputs.UiDown)) - { - SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex)); - _currentIndex = new[] { _currentIndex + 1, _itemsPerPage - 1, ItemSlots.Length - 1 }.Min(); - SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex)); - } - - if (@event.IsActionPressed(GameInputs.UiUp)) - { - SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex)); - _currentIndex = new[] { _currentIndex - 1, 0 }.Max(); - SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex)); - } - - if (@event.IsActionPressed(GameInputs.UiAccept)) - { - if (ItemSlots.Length == 0 || GetViewport().GuiGetFocusOwner() is Button) - return; - DisplayUserActionPrompt(); - } - } - private void DisplayUserActionPrompt() { ItemDescriptionTitle.Hide(); @@ -205,7 +211,7 @@ public partial class InventoryMenu : Control, IInventoryMenu await ToSignal(GetTree().CreateTimer(0.1f), "timeout"); _currentIndex = 0; _currentPageNumber = pageToChangeTo; - PopulateItems(); + await RedrawInventory(); } private void PopulateInventory() @@ -314,10 +320,14 @@ public partial class InventoryMenu : Control, IInventoryMenu if (currentItem is ConsumableItem consumable) consumable.Use(); + if (_currentIndex >= ItemSlots.Length - 1) + _currentIndex--; + if (_currentIndex <= 0) + _currentIndex = 0; + // TODO: Replace with animation player (for visual effects/sound effects?) await ToSignal(GetTree().CreateTimer(0.25f), "timeout"); - await HideUserActionPrompt(); - PopulatePlayerInfo(); + await RedrawInventory(); } private async void ThrowButtonPressed() diff --git a/src/items/consumable/ConsumableItem.cs b/src/items/consumable/ConsumableItem.cs index ba61ed75..0645c4a6 100644 --- a/src/items/consumable/ConsumableItem.cs +++ b/src/items/consumable/ConsumableItem.cs @@ -39,6 +39,8 @@ public partial class ConsumableItem : Node3D, IInventoryItem HealHP(); if (ConsumableItemInfo.HealVTAmount != 0) HealVT(); + + GameRepo.InventoryItems.Value.Remove(this); } private void RaiseHP()