Rewrite and simplify Inventory Menu, various fixes for item effects

This commit is contained in:
2025-11-04 01:12:16 -08:00
parent 7b7fc910bd
commit a5846e08dc
49 changed files with 1070 additions and 826 deletions

View File

@@ -1,32 +1,52 @@
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using Godot;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Zennysoft.Ma.Adapter;
namespace Zennysoft.Game.Ma;
public interface IInventoryMenu : IControl
{
public Task RefreshInventoryScreen();
public Task DisplayMessage(string message);
public void RemoveItem(InventoryItem item);
}
[Meta(typeof(IAutoNode))]
public partial class InventoryMenu : Control, IInventoryMenu
{
public override void _Notification(int what) => this.Notify(what);
[Node] public VBoxContainer ItemsPage { get; set; }
[Node] public Label ATKValue { get; set; }
[Node] public Label ATKBonusLabel { get; set; }
[Node] public Label DEFValue { get; set; }
[Node] public Label DEFBonusLabel { get; set; }
[Node] public Button UseButton { get; set; }
[Node] public Button ThrowButton { get; set; }
[Node] public Button DropButton { get; set; }
[Node] public Label ItemDescriptionTitle { get; set; }
[Node] public Label UseItemPrompt { get; set; }
[Node] public Label ItemEffectLabel { get; set; }
[Node] public Label BackArrow { get; set; } = default!;
[Node] public Label ForwardArrow { get; set; } = default!;
[Node] public ItemSlot ItemSlot1 { get; set; }
[Node] public ItemSlot ItemSlot2 { get; set; }
[Node] public ItemSlot ItemSlot3 { get; set; }
[Node] public ItemSlot ItemSlot4 { get; set; }
[Node] public ItemSlot ItemSlot5 { get; set; }
[Node] public ItemSlot ItemSlot6 { get; set; }
[Node] public ItemSlot ItemSlot7 { get; set; }
[Node] public ItemSlot ItemSlot8 { get; set; }
[Node] public ItemSlot ItemSlot9 { get; set; }
[Node] public ItemSlot ItemSlot10 { get; set; }
[Dependency] private IPlayer _player => this.DependOn<IPlayer>();
[Dependency] private IGame _game => this.DependOn<IGame>();
[Dependency] private IGameRepo _gameRepo => this.DependOn<IGameRepo>();
[Dependency] public IGame Game => this.DependOn<IGame>();
[Dependency] public IPlayer Player => this.DependOn<IPlayer>();
private List<IItemSlot> ItemSlots;
private InventoryPageNumber _currentPageNumber = InventoryPageNumber.FirstPage;
@@ -34,204 +54,208 @@ public partial class InventoryMenu : Control, IInventoryMenu
private const int _itemsPerPage = 10;
private int _currentIndex = 0;
private IItemSlot _currentlySelectedItem = null;
private IItemSlot[] ItemSlots => [.. ItemsPage.GetChildren().OfType<IItemSlot>()];
#region Control Nodes
[Node] public Label ATKValue { get; set; } = default!;
[Node] public Label ATKBonusLabel { get; set; } = default!;
[Node] public Label DEFValue { get; set; } = default!;
[Node] public Label DEFBonusLabel { get; set; } = default!;
[Node] public Label ItemDescriptionTitle { get; set; } = default!;
[Node] public Label ItemEffectLabel { get; set; } = default!;
// Item Menu
[Node] public Label BackArrow { get; set; } = default!;
[Node] public Label ForwardArrow { get; set; } = default!;
[Node] public Control ItemsPage { get; set; } = default!;
// User Prompt Menu
[Node] public Label UseItemPrompt { get; set; } = default!;
[Node] public Button UseButton { get; set; } = default!;
[Node] public Button ThrowButton { get; set; } = default!;
[Node] public Button DropButton { get; set; } = default!;
[Node] public AnimationPlayer AnimationPlayer { get; set; } = default!;
#endregion
public InventoryMenu()
public override void _EnterTree()
{
SetProcessInput(false);
SetProcess(false);
}
public void OnResolved()
{
ItemSlots = [ItemSlot1, ItemSlot2, ItemSlot3, ItemSlot4, ItemSlot5, ItemSlot6, ItemSlot7, ItemSlot8, ItemSlot9, ItemSlot10];
_currentlySelectedItem = ItemSlot1;
foreach (var item in ItemSlots)
{
item.ItemPressed += Item_Pressed;
item.ItemEnterFocus += Item_FocusEntered;
item.ItemExitFocus += Item_ItemExitFocus;
}
_player.AttackComponent.CurrentAttack.Sync += Attack_Sync;
_player.AttackComponent.MaximumAttack.Sync += Attack_Sync;
_player.DefenseComponent.CurrentDefense.Sync += Defense_Sync;
_player.DefenseComponent.MaximumDefense.Sync += Defense_Sync;
_player.EquipmentComponent.EquipmentChanged += EquipmentComponent_EquipmentChanged;
_player.Inventory.InventoryChanged += Inventory_InventoryChanged;
UseButton.Pressed += UseButtonPressed;
ThrowButton.Pressed += ThrowButtonPressed;
DropButton.Pressed += DropButtonPressed;
Player.AttackComponent.CurrentAttack.Sync += AttackSync;
Player.AttackComponent.MaximumAttack.Sync += AttackSync;
Player.EquipmentComponent.EquippedWeapon.Sync += BonusSync;
Player.EquipmentComponent.EquippedArmor.Sync += BonusSync;
Player.EquipmentComponent.EquippedAccessory.Sync += BonusSync;
Player.DefenseComponent.CurrentDefense.Sync += DefenseSync;
Player.DefenseComponent.MaximumDefense.Sync += DefenseSync;
VisibilityChanged += InventoryMenu2_VisibilityChanged;
}
public void OnExitTree()
{
Player.AttackComponent.CurrentAttack.Sync -= AttackSync;
Player.AttackComponent.MaximumAttack.Sync -= AttackSync;
Player.EquipmentComponent.EquippedWeapon.Sync -= BonusSync;
Player.EquipmentComponent.EquippedArmor.Sync -= BonusSync;
Player.EquipmentComponent.EquippedAccessory.Sync -= BonusSync;
Player.DefenseComponent.CurrentDefense.Sync -= DefenseSync;
Player.DefenseComponent.MaximumDefense.Sync -= DefenseSync;
}
public async Task DisplayMessage(string message)
{
SetProcessInput(false);
await HideUserActionPrompt();
await ShowInventoryInfo();
ItemEffectLabel.Text = message;
await ToSignal(GetTree().CreateTimer(1f), "timeout");
await RefreshInventoryScreen();
SetProcessInput(true);
}
private void AttackSync(int obj) => ATKValue.Text = $"{Player.AttackComponent.CurrentAttack.Value}/{Player.AttackComponent.MaximumAttack.Value}";
private void BonusSync(EquipableItem equip)
{
ATKBonusLabel.Text = $"{Player.EquipmentComponent.BonusAttack:+0;-#;\\.\\.\\.}";
DEFBonusLabel.Text = $"{Player.EquipmentComponent.BonusDefense:+0;-#;\\.\\.\\.}";
}
private void DefenseSync(int obj) => DEFValue.Text = $"{Player.DefenseComponent.CurrentDefense.Value}/{Player.DefenseComponent.MaximumDefense.Value}";
public async Task RefreshInventoryScreen()
{
await ClearItems();
PopulateInventory();
PopulatePlayerInfo();
await HideUserActionPrompt();
await ShowInventoryInfo();
}
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
public override void _Input(InputEvent @event)
{
if (Visible && @event.IsActionPressed(GameInputs.Inventory))
{
if (UseButton.HasFocus() || DropButton.HasFocus() || ThrowButton.HasFocus())
{
HideUserActionPrompt();
ShowInventoryInfo();
Autoload.AudioManager.Play(SoundEffect.Cancel);
}
else
{
AcceptEvent();
Autoload.AudioManager.Play(SoundEffect.Cancel);
_gameRepo.CloseInventory();
}
}
if (ItemSlots.Length == 0 || UseButton.HasFocus() || DropButton.HasFocus() || ThrowButton.HasFocus())
if (!Visible)
return;
if (@event.IsActionPressed(GameInputs.MoveRight) && _currentPageNumber == InventoryPageNumber.FirstPage)
if (Input.IsActionJustPressed(GameInputs.Inventory) && !(UseButton.HasFocus() || DropButton.HasFocus() || ThrowButton.HasFocus()))
{
var inventory = Player.Inventory;
if (inventory.Items.Count > _itemsPerPage)
ChangeInventoryPage(InventoryPageNumber.SecondPage);
AcceptEvent();
_gameRepo.CloseInventory();
}
if (Input.IsActionJustPressed(GameInputs.UiCancel) && (UseButton.HasFocus() || DropButton.HasFocus() || ThrowButton.HasFocus()))
{
AcceptEvent();
HideUserActionPrompt();
}
else if (Input.IsActionJustPressed(GameInputs.UiCancel))
{
AcceptEvent();
_gameRepo.CloseInventory();
}
if (_currentPageNumber == InventoryPageNumber.FirstPage && _player.Inventory.Items.Count > 10 && Input.IsActionJustPressed(GameInputs.MoveRight))
{
_currentPageNumber = InventoryPageNumber.SecondPage;
Inventory_InventoryChanged();
_currentlySelectedItem = ItemSlot1;
ItemSlot1.GrabFocus();
}
else if (_currentPageNumber == InventoryPageNumber.SecondPage && Input.IsActionJustPressed(GameInputs.MoveLeft))
{
_currentPageNumber = InventoryPageNumber.FirstPage;
Inventory_InventoryChanged();
_currentlySelectedItem = ItemSlot1;
ItemSlot1.GrabFocus();
}
if (@event.IsActionPressed(GameInputs.MoveLeft) && _currentPageNumber == InventoryPageNumber.SecondPage)
ChangeInventoryPage(InventoryPageNumber.FirstPage);
if (@event.IsActionPressed(GameInputs.MoveDown))
if (Input.IsActionJustPressed(GameInputs.InventorySort))
{
var oldIndex = _currentIndex;
var newIndex = new[] { _currentIndex + 1, _itemsPerPage - 1, ItemSlots.Length - 1 }.Min();
if (oldIndex == newIndex)
return;
SetToUnselectedStyle(ItemSlots.ElementAt(oldIndex));
SetToSelectedStyle(ItemSlots.ElementAt(newIndex));
Autoload.AudioManager.Play(SoundEffect.MoveThroughOptions);
_currentIndex = newIndex;
}
if (@event.IsActionPressed(GameInputs.MoveUp))
{
var oldIndex = _currentIndex;
var newIndex = new[] { _currentIndex - 1, 0 }.Max();
if (oldIndex == newIndex)
return;
SetToUnselectedStyle(ItemSlots.ElementAt(oldIndex));
SetToSelectedStyle(ItemSlots.ElementAt(newIndex));
Autoload.AudioManager.Play(SoundEffect.MoveThroughOptions);
_currentIndex = newIndex;
}
if (@event.IsActionPressed(GameInputs.Attack))
{
DisplayUserActionPrompt();
Autoload.AudioManager.Play(SoundEffect.Select);
}
if (@event.IsActionPressed(GameInputs.InventorySort))
{
var inventory = Player.Inventory;
inventory.Sort(Player.EquipmentComponent.EquippedWeapon.Value, Player.EquipmentComponent.EquippedArmor.Value, Player.EquipmentComponent.EquippedAccessory.Value);
if (_currentIndex > inventory.Items.Count - 1)
_currentIndex = inventory.Items.Count - 1;
RefreshInventoryScreen();
_player.Inventory.Sort(_player.EquipmentComponent.EquippedWeapon.Value, _player.EquipmentComponent.EquippedArmor.Value, _player.EquipmentComponent.EquippedAccessory.Value);
Inventory_InventoryChanged();
foreach (var slot in ItemSlots)
slot.SetItemStyle();
Item_ItemExitFocus(_currentlySelectedItem);
_currentlySelectedItem = ItemSlot1;
_currentlySelectedItem.GrabFocus();
}
}
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
public async void RemoveItem(InventoryItem item)
private void InventoryMenu2_VisibilityChanged()
{
Player.Inventory.Remove(item);
if (_currentIndex >= ItemSlots.Length - 1)
_currentIndex--;
if (_currentIndex <= 0)
_currentIndex = 0;
_currentlySelectedItem.GrabFocus();
}
private void PopulateItems()
private void Item_ItemExitFocus(IItemSlot itemSlot)
{
PopulateInventory();
PopulatePlayerInfo();
}
private async Task ClearItems()
{
foreach (var item in ItemSlots)
ItemsPage.RemoveChildEx(item);
ItemDescriptionTitle.Text = string.Empty;
ItemEffectLabel.Text = string.Empty;
itemSlot.IsSelected = false;
itemSlot.SetItemStyle();
}
private void PopulatePlayerInfo()
private void Item_FocusEntered(IItemSlot itemSlot)
{
if (ItemSlots.Length != 0)
if (itemSlot.Item.Value == null)
return;
ItemDescriptionTitle.Text = $"{itemSlot.Item.Value.ItemName}";
ItemEffectLabel.Text = $"{itemSlot.Item.Value.Description}";
_currentlySelectedItem = itemSlot;
itemSlot.IsSelected = true;
itemSlot.SetItemStyle();
}
private void Item_Pressed(InventoryItem item) => DisplayUserActionPrompt(item);
private async void Inventory_InventoryChanged()
{
foreach (var slot in ItemSlots)
{
var item = ItemSlots.ElementAt(_currentIndex).Item;
ItemDescriptionTitle.Text = $"{item.ItemName}";
ItemEffectLabel.Text = $"{item.Description}";
slot.Visible = false;
slot.SetItemStyle();
}
if (_currentPageNumber == InventoryPageNumber.SecondPage && _player.Inventory.Items.Count <= 10)
{
_currentPageNumber = InventoryPageNumber.FirstPage;
var elementToSelect = _player.Inventory.Items.IndexOf(_player.Inventory.Items.Last());
_currentlySelectedItem = ItemSlots.ElementAt(elementToSelect);
_currentlySelectedItem.GrabFocus();
}
var itemsToDisplay = new List<InventoryItem>();
if (_currentPageNumber == InventoryPageNumber.FirstPage)
itemsToDisplay = [.. _player.Inventory.Items.Take(_itemsPerPage)];
else
itemsToDisplay = [.. _player.Inventory.Items.TakeLast(_player.Inventory.Items.Count - _itemsPerPage)];
for (var i = 0; i < itemsToDisplay.Count; i++)
{
ItemSlots[i].Item.OnNext(itemsToDisplay[i]);
ItemSlots[i].Visible = true;
}
SetPageIndicators();
if (!_player.Inventory.Items.Contains(_currentlySelectedItem.Item.Value))
{
_currentlySelectedItem.Item.OnNext(null);
var elementToSelect = Mathf.Max(0, ItemSlots.IndexOf(_currentlySelectedItem) - 1);
_currentlySelectedItem = ItemSlots.ElementAt(elementToSelect);
_currentlySelectedItem.GrabFocus();
}
}
private void DisplayUserActionPrompt()
private void SetPageIndicators()
{
if (_player.Inventory.Items.Count > 10 && _currentPageNumber == InventoryPageNumber.FirstPage)
{
ForwardArrow.Text = "►";
BackArrow.Text = "";
}
else if (_player.Inventory.Items.Count > 10 && _currentPageNumber == InventoryPageNumber.SecondPage)
{
ForwardArrow.Text = "";
BackArrow.Text = "◄";
}
}
private void Attack_Sync(int obj) => ATKValue.Text = $"{_player.AttackComponent.CurrentAttack.Value}/{_player.AttackComponent.MaximumAttack.Value}";
private void Defense_Sync(int obj) => DEFValue.Text = $"{_player.DefenseComponent.CurrentDefense.Value}/{_player.DefenseComponent.MaximumDefense.Value}";
private void EquipmentComponent_EquipmentChanged(EquipableItem equipableItem)
{
ATKBonusLabel.Text = $"{_player.EquipmentComponent.BonusAttack:+0;-#;\\.\\.\\.}";
DEFBonusLabel.Text = $"{_player.EquipmentComponent.BonusDefense:+0;-#;\\.\\.\\.}";
}
private async void UseButtonPressed()
{
UseButton.Disabled = true;
if (_currentlySelectedItem.Item.Value is EquipableItem equipable)
await EquipOrUnequipItem(equipable);
else
await _game.UseItem(_currentlySelectedItem.Item.Value);
UseButton.Disabled = false;
HideUserActionPrompt();
await ShowInventoryInfo();
await ToSignal(GetTree().CreateTimer(1f), "timeout");
}
private async void ThrowButtonPressed()
{
_game.ThrowItem(_currentlySelectedItem.Item.Value);
_player.Inventory.Remove(_currentlySelectedItem.Item.Value);
HideUserActionPrompt();
await ShowInventoryInfo();
_gameRepo.CloseInventory();
}
private async void DropButtonPressed()
{
_game.DropItem(_currentlySelectedItem.Item.Value);
_player.Inventory.Remove(_currentlySelectedItem.Item.Value);
HideUserActionPrompt();
await ShowInventoryInfo();
_gameRepo.CloseInventory();
}
private void DisplayUserActionPrompt(InventoryItem item)
{
ItemDescriptionTitle.Hide();
ItemEffectLabel.Hide();
@@ -240,11 +264,9 @@ public partial class InventoryMenu : Control, IInventoryMenu
ThrowButton.Show();
DropButton.Show();
var currentItem = ItemSlots.ElementAt(_currentIndex).Item;
if (currentItem is EquipableItem equipable)
if (item is EquipableItem equipable)
{
var isItemEquipped = Player.EquipmentComponent.IsItemEquipped(equipable);
var isItemEquipped = _player.EquipmentComponent.IsItemEquipped(equipable);
UseButton.Text = isItemEquipped ? "Unequip" : "Equip";
ThrowButton.Disabled = isItemEquipped;
ThrowButton.FocusMode = isItemEquipped ? FocusModeEnum.None : FocusModeEnum.All;
@@ -259,7 +281,7 @@ public partial class InventoryMenu : Control, IInventoryMenu
UseButton.CallDeferred(MethodName.GrabFocus);
}
private async Task HideUserActionPrompt()
private void HideUserActionPrompt()
{
UseItemPrompt.Hide();
UseButton.Hide();
@@ -268,6 +290,23 @@ public partial class InventoryMenu : Control, IInventoryMenu
UseButton.ReleaseFocus();
ThrowButton.ReleaseFocus();
DropButton.ReleaseFocus();
_currentlySelectedItem.GrabFocus();
}
private async Task EquipOrUnequipItem(EquipableItem equipable)
{
if (_player.EquipmentComponent.IsItemEquipped(equipable))
{
ItemEffectLabel.Text = $"{equipable.GetType().Name} unequipped.";
_player.Unequip(equipable);
}
else
{
var itemSlot = _currentlySelectedItem;
ItemEffectLabel.Text = $"{equipable.GetType().Name} equipped.";
_player.Equip(equipable);
_currentlySelectedItem = itemSlot;
}
}
private async Task ShowInventoryInfo()
@@ -276,149 +315,6 @@ public partial class InventoryMenu : Control, IInventoryMenu
ItemEffectLabel.Show();
}
private async Task ChangeInventoryPage(InventoryPageNumber pageToChangeTo)
{
await ClearItems();
await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
_currentIndex = 0;
_currentPageNumber = pageToChangeTo;
await RefreshInventoryScreen();
Autoload.AudioManager.Play(SoundEffect.MoveThroughOptions);
}
private async void PopulateInventory()
{
var inventory = Player.Inventory;
var numberOfItemsToDisplay = _currentPageNumber == InventoryPageNumber.FirstPage ? Mathf.Min(inventory.Items.Count, _itemsPerPage) : Mathf.Min(inventory.Items.Count - _itemsPerPage, _itemsPerPage);
var indexToStart = _currentPageNumber == InventoryPageNumber.FirstPage ? 0 : _itemsPerPage;
ForwardArrow.Text = "";
BackArrow.Text = "";
if (_currentPageNumber == InventoryPageNumber.FirstPage && inventory.Items.Count > _itemsPerPage)
{
ForwardArrow.Text = "►";
BackArrow.Text = "";
}
if (_currentPageNumber == InventoryPageNumber.SecondPage)
{
ForwardArrow.Text = "";
BackArrow.Text = "◄";
}
for (var i = 0; i < numberOfItemsToDisplay; i++)
{
var item = inventory.Items.ElementAt(i + indexToStart);
var itemScene = GD.Load<PackedScene>(ITEM_SLOT_SCENE);
var itemSlot = itemScene.Instantiate<IItemSlot>();
itemSlot.Item = item;
ItemsPage.AddChildEx(itemSlot);
if (Player.EquipmentComponent.IsItemEquipped(itemSlot.Item))
itemSlot.SetEquippedItemStyle();
}
if (ItemSlots.Length != 0)
{
ItemSlots.ElementAt(_currentIndex).SetSelectedItemStyle();
if (Player.EquipmentComponent.IsItemEquipped(ItemSlots.ElementAt(_currentIndex).Item))
ItemSlots.ElementAt(_currentIndex).SetEquippedSelectedItemStyle();
}
}
private async Task SetToUnselectedStyle(IItemSlot itemSlot)
{
await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
itemSlot.SetItemStyle();
if (Player.EquipmentComponent.IsItemEquipped(itemSlot.Item))
itemSlot.SetEquippedItemStyle();
}
private async Task SetToSelectedStyle(IItemSlot itemSlot)
{
await ToSignal(GetTree().CreateTimer(0.1f), "timeout");
itemSlot.SetSelectedItemStyle();
ItemDescriptionTitle.Text = $"{itemSlot.Item.ItemName}";
ItemEffectLabel.Text = $"{itemSlot.Item.Description}";
}
private async Task EquipOrUnequipItem()
{
var itemSlot = ItemSlots[_currentIndex];
if (itemSlot.Item is not EquipableItem)
return;
var equippableItem = (EquipableItem)itemSlot.Item;
if (Player.EquipmentComponent.IsItemEquipped(equippableItem))
{
ItemEffectLabel.Text = $"{equippableItem.GetType().Name} unequipped.";
Player.EquipmentComponent.Unequip(equippableItem);
itemSlot.SetSelectedItemStyle();
if (itemSlot.Item.ItemTag == ItemTag.BreaksOnChange)
Player.Inventory.Remove(equippableItem);
}
else
{
ItemEffectLabel.Text = $"{equippableItem.GetType().Name} equipped.";
Player.Equip(equippableItem);
itemSlot.SetEquippedSelectedItemStyle();
}
RefreshUIAfterUserSelection();
}
private async void UseButtonPressed()
{
UseButton.Disabled = true;
var currentItem = ItemSlots[_currentIndex].Item;
if (currentItem is EquipableItem)
await EquipOrUnequipItem();
else
await Game.UseItem(currentItem);
RefreshUIAfterUserSelection();
UseButton.Disabled = false;
}
private async void ThrowButtonPressed()
{
var currentItem = ItemSlots[_currentIndex].Item;
Game.ThrowItem(currentItem);
Player.Inventory.Remove(currentItem);
if (_currentIndex >= ItemSlots.Length - 1)
_currentIndex--;
if (_currentIndex <= 0)
_currentIndex = 0;
_gameRepo.CloseInventory();
}
private async void DropButtonPressed()
{
var currentItem = ItemSlots[_currentIndex].Item;
Game.DropItem(currentItem);
Player.Inventory.Remove(currentItem);
if (_currentIndex >= ItemSlots.Length - 1)
_currentIndex--;
if (_currentIndex <= 0)
_currentIndex = 0;
_gameRepo.CloseInventory();
}
private async void RefreshUIAfterUserSelection()
{
SetProcessInput(false);
await HideUserActionPrompt();
await ShowInventoryInfo();
await RefreshInventoryScreen();
await ToSignal(GetTree().CreateTimer(1f), "timeout");
SetProcessInput(true);
}
private enum InventoryPageNumber
{
FirstPage,