Refactor inventory menu
Add user prompt and implement equip state
This commit is contained in:
@@ -40,10 +40,15 @@ public partial class InventoryMenu : Control, IInventoryMenu
|
||||
[Node] public Label ForwardArrow { get; set; } = default!;
|
||||
[Node] public Control ItemsPage { get; set; } = default!;
|
||||
|
||||
private enum InventoryPageNumber
|
||||
// User Prompt Menu
|
||||
[Node] public Label UseItemPrompt { get; set; } = default!;
|
||||
[Node] public Button UseButton { get; set; } = default!;
|
||||
[Node] public Button EquipButton { get; set; } = default!;
|
||||
[Node] public Button DropButton { get; set; } = default!;
|
||||
|
||||
public void OnReady()
|
||||
{
|
||||
FirstPage,
|
||||
SecondPage
|
||||
EquipButton.Pressed += EquipButtonPressed;
|
||||
}
|
||||
|
||||
private InventoryPageNumber _currentPageNumber = InventoryPageNumber.FirstPage;
|
||||
@@ -62,6 +67,14 @@ public partial class InventoryMenu : Control, IInventoryMenu
|
||||
PopulatePlayerInfo();
|
||||
}
|
||||
|
||||
public void ClearItems()
|
||||
{
|
||||
foreach (var item in ItemSlots)
|
||||
ItemsPage.RemoveChildEx(item);
|
||||
|
||||
HideUserActionPrompt();
|
||||
}
|
||||
|
||||
public void PopulatePlayerInfo()
|
||||
{
|
||||
FloorLabel.Text = $"Level {GameRepo.CurrentFloor:D2}";
|
||||
@@ -107,6 +120,91 @@ public partial class InventoryMenu : Control, IInventoryMenu
|
||||
}
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (ItemSlots.Length == 0)
|
||||
return;
|
||||
|
||||
var inventory = GameRepo.InventoryItems.Value;
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.UiRight) && _currentPageNumber == InventoryPageNumber.FirstPage && inventory.Count > _itemsPerPage)
|
||||
ChangeInventoryPage(InventoryPageNumber.SecondPage);
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.UiLeft) && _currentPageNumber == InventoryPageNumber.SecondPage)
|
||||
ChangeInventoryPage(InventoryPageNumber.FirstPage);
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.UiDown))
|
||||
{
|
||||
SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex));
|
||||
_currentIndex = new[] { _currentIndex + 1, _itemsPerPage - 1, ItemSlots.Length - 1 }.Min();
|
||||
SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex));
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.UiUp))
|
||||
{
|
||||
if (ItemSlots.Length == 0)
|
||||
return;
|
||||
|
||||
SetToUnselectedStyle(ItemSlots.ElementAt(_currentIndex));
|
||||
_currentIndex = new[] { _currentIndex - 1, 0 }.Max();
|
||||
SetToSelectedStyle(ItemSlots.ElementAt(_currentIndex));
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.UiAccept))
|
||||
{
|
||||
if (ItemSlots.Length == 0 || GetViewport().GuiGetFocusOwner() is Button)
|
||||
return;
|
||||
DisplayUserActionPrompt();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisplayUserActionPrompt()
|
||||
{
|
||||
ItemDescriptionTitle.Hide();
|
||||
ItemEffectLabel.Hide();
|
||||
UseItemPrompt.Show();
|
||||
UseButton.Show();
|
||||
EquipButton.Show();
|
||||
EquipButton.Disabled = false;
|
||||
DropButton.Show();
|
||||
|
||||
var currentItem = ItemSlots.ElementAt(_currentIndex).Item;
|
||||
|
||||
if (currentItem is IEquipable equipable)
|
||||
{
|
||||
EquipButton.Disabled = false;
|
||||
EquipButton.FocusMode = FocusModeEnum.All;
|
||||
EquipButton.Text = GameRepo.IsItemEquipped(equipable) ? "Unequip" : "Equip";
|
||||
}
|
||||
else
|
||||
{
|
||||
EquipButton.Disabled = true;
|
||||
EquipButton.FocusMode = FocusModeEnum.None;
|
||||
}
|
||||
|
||||
UseButton.Text = currentItem is ThrowableItem ? "Throw" : "Use";
|
||||
|
||||
UseButton.GrabFocus();
|
||||
}
|
||||
|
||||
private void HideUserActionPrompt()
|
||||
{
|
||||
ItemDescriptionTitle.Show();
|
||||
ItemEffectLabel.Show();
|
||||
UseItemPrompt.Hide();
|
||||
UseButton.Hide();
|
||||
EquipButton.Hide();
|
||||
DropButton.Hide();
|
||||
}
|
||||
|
||||
private void ChangeInventoryPage(InventoryPageNumber pageToChangeTo)
|
||||
{
|
||||
ClearItems();
|
||||
_currentIndex = 0;
|
||||
_currentPageNumber = pageToChangeTo;
|
||||
PopulateItems();
|
||||
}
|
||||
|
||||
private void PopulateInventory()
|
||||
{
|
||||
var inventory = GameRepo.InventoryItems.Value;
|
||||
@@ -148,103 +246,69 @@ public partial class InventoryMenu : Control, IInventoryMenu
|
||||
}
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
private void SetToUnselectedStyle(IItemSlot itemSlot)
|
||||
{
|
||||
var inventory = GameRepo.InventoryItems.Value;
|
||||
if (ItemSlots.Any() && _currentPageNumber == InventoryPageNumber.FirstPage && inventory.Count > _itemsPerPage && Input.IsActionJustPressed(GameInputs.UiRight))
|
||||
itemSlot.SetItemStyle();
|
||||
if (itemSlot.Item is IEquipable equipable && GameRepo.IsItemEquipped(equipable))
|
||||
itemSlot.SetEquippedItemStyle();
|
||||
}
|
||||
|
||||
private void SetToSelectedStyle(IItemSlot itemSlot)
|
||||
{
|
||||
itemSlot.SetSelectedItemStyle();
|
||||
if (itemSlot.Item is IEquipable newEquipable && GameRepo.IsItemEquipped(newEquipable))
|
||||
itemSlot.SetEquippedSelectedItemStyle();
|
||||
ItemDescriptionTitle.Text = $"{itemSlot.Item.Info.Name}";
|
||||
ItemEffectLabel.Text = $"{itemSlot.Item.Info.Description}";
|
||||
}
|
||||
|
||||
private void EquipOrUnequipItem()
|
||||
{
|
||||
var itemSlot = ItemSlots[_currentIndex];
|
||||
if (itemSlot.Item is IEquipable equipableItem)
|
||||
{
|
||||
ClearItems();
|
||||
_currentIndex = 0;
|
||||
_currentPageNumber = InventoryPageNumber.SecondPage;
|
||||
PopulateItems();
|
||||
}
|
||||
|
||||
if (ItemSlots.Any() && _currentPageNumber == InventoryPageNumber.SecondPage && Input.IsActionJustPressed(GameInputs.UiLeft))
|
||||
{
|
||||
ClearItems();
|
||||
_currentIndex = 0;
|
||||
_currentPageNumber = InventoryPageNumber.FirstPage;
|
||||
PopulateItems();
|
||||
}
|
||||
|
||||
if (ItemSlots.Any() && Input.IsActionJustPressed(GameInputs.UiDown))
|
||||
{
|
||||
if (!inventory.Any())
|
||||
return;
|
||||
|
||||
var oldItem = ItemSlots.ElementAt(_currentIndex);
|
||||
ItemSlots.ElementAt(_currentIndex).SetItemStyle();
|
||||
var index = new[] { _currentIndex + 1, _itemsPerPage - 1, ItemSlots.Count() - 1 };
|
||||
_currentIndex = index.Min();
|
||||
var newItem = ItemSlots.ElementAt(_currentIndex);
|
||||
newItem.SetSelectedItemStyle();
|
||||
if (oldItem.Item is IEquipable equipable && GameRepo.IsItemEquipped(equipable))
|
||||
oldItem.SetEquippedItemStyle();
|
||||
if (newItem.Item is IEquipable newEquipable && GameRepo.IsItemEquipped(newEquipable))
|
||||
newItem.SetEquippedSelectedItemStyle();
|
||||
|
||||
ItemDescriptionTitle.Text = $"{newItem.Item.Info.Name}";
|
||||
ItemEffectLabel.Text = $"{newItem.Item.Info.Description}";
|
||||
}
|
||||
|
||||
if (ItemSlots.Any() && Input.IsActionJustPressed(GameInputs.UiUp))
|
||||
{
|
||||
if (!inventory.Any())
|
||||
return;
|
||||
|
||||
var oldItem = ItemSlots.ElementAt(_currentIndex);
|
||||
ItemSlots.ElementAt(_currentIndex).SetItemStyle();
|
||||
var index = new[] { _currentIndex - 1, 0 };
|
||||
_currentIndex = index.Max();
|
||||
var newItem = ItemSlots.ElementAt(_currentIndex);
|
||||
newItem.SetSelectedItemStyle();
|
||||
if (oldItem.Item is IEquipable equipable && GameRepo.IsItemEquipped(equipable))
|
||||
oldItem.SetEquippedItemStyle();
|
||||
if (newItem.Item is IEquipable newEquipable && GameRepo.IsItemEquipped(newEquipable))
|
||||
newItem.SetEquippedSelectedItemStyle();
|
||||
ItemDescriptionTitle.Text = $"{newItem.Item.Info.Name}";
|
||||
ItemEffectLabel.Text = $"{newItem.Item.Info.Description}";
|
||||
}
|
||||
|
||||
if (ItemSlots.Any() && Input.IsActionJustPressed(GameInputs.UiAccept))
|
||||
{
|
||||
var itemSlot = ItemSlots[_currentIndex];
|
||||
if (itemSlot.Item is IEquipable equipableItem)
|
||||
if (GameRepo.IsItemEquipped(equipableItem))
|
||||
{
|
||||
if (GameRepo.IsItemEquipped(equipableItem))
|
||||
{
|
||||
GameRepo.UnequipItem(equipableItem);
|
||||
itemSlot.SetSelectedItemStyle();
|
||||
}
|
||||
else
|
||||
{
|
||||
GameRepo.EquipItem(equipableItem);
|
||||
itemSlot.SetEquippedSelectedItemStyle();
|
||||
}
|
||||
|
||||
var atkBonus = GameRepo.PlayerStatInfo.Value.BonusAttack;
|
||||
ATKBonusLabel.Text = atkBonus != 0 ? $"{atkBonus:+0;-#}" : "...";
|
||||
var defBonus = GameRepo.PlayerStatInfo.Value.BonusDefense;
|
||||
DEFBonusLabel.Text = defBonus != 0 ? $"{defBonus:+0;-#}" : "...";
|
||||
|
||||
var currentHP = GameRepo.PlayerStatInfo.Value.CurrentHP;
|
||||
var maxHP = GameRepo.PlayerStatInfo.Value.MaximumHP;
|
||||
|
||||
HPValue.Text = $"{currentHP}/{maxHP}";
|
||||
|
||||
var currentVT = GameRepo.PlayerStatInfo.Value.CurrentVT;
|
||||
var maxVT = GameRepo.PlayerStatInfo.Value.MaximumVT;
|
||||
|
||||
VTValue.Text = $"{currentVT}/{maxVT}";
|
||||
GameRepo.UnequipItem(equipableItem);
|
||||
itemSlot.SetSelectedItemStyle();
|
||||
}
|
||||
else
|
||||
{
|
||||
GameRepo.EquipItem(equipableItem);
|
||||
itemSlot.SetEquippedSelectedItemStyle();
|
||||
}
|
||||
|
||||
UpdateStatBonusInfo();
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearItems()
|
||||
private void UpdateStatBonusInfo()
|
||||
{
|
||||
foreach (var item in ItemSlots)
|
||||
{
|
||||
ItemsPage.RemoveChildEx(item);
|
||||
}
|
||||
var atkBonus = GameRepo.PlayerStatInfo.Value.BonusAttack;
|
||||
ATKBonusLabel.Text = atkBonus != 0 ? $"{atkBonus:+0;-#}" : "...";
|
||||
var defBonus = GameRepo.PlayerStatInfo.Value.BonusDefense;
|
||||
DEFBonusLabel.Text = defBonus != 0 ? $"{defBonus:+0;-#}" : "...";
|
||||
|
||||
var currentHP = GameRepo.PlayerStatInfo.Value.CurrentHP;
|
||||
var maxHP = GameRepo.PlayerStatInfo.Value.MaximumHP;
|
||||
|
||||
HPValue.Text = $"{currentHP}/{maxHP}";
|
||||
|
||||
var currentVT = GameRepo.PlayerStatInfo.Value.CurrentVT;
|
||||
var maxVT = GameRepo.PlayerStatInfo.Value.MaximumVT;
|
||||
|
||||
VTValue.Text = $"{currentVT}/{maxVT}";
|
||||
}
|
||||
|
||||
private void EquipButtonPressed()
|
||||
{
|
||||
EquipOrUnequipItem();
|
||||
HideUserActionPrompt();
|
||||
}
|
||||
|
||||
private enum InventoryPageNumber
|
||||
{
|
||||
FirstPage,
|
||||
SecondPage
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user