Refactor inventory menu

Add user prompt and implement equip state
This commit is contained in:
2024-09-10 12:29:44 -07:00
parent 83e0566ae8
commit 081bde1d9a
2 changed files with 201 additions and 99 deletions

View File

@@ -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
}
}

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=26 format=3 uid="uid://dlj8qdg1c5048"]
[gd_scene load_steps=32 format=3 uid="uid://dlj8qdg1c5048"]
[ext_resource type="Script" path="res://src/inventory_menu/InventoryMenu.cs" id="1_l64wl"]
[ext_resource type="Shader" path="res://src/inventory_menu/InventoryMenu.gdshader" id="2_0fvsh"]
@@ -73,10 +73,22 @@ font_color = Color(0.737255, 0.705882, 0.690196, 1)
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_0kb6l"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ascpt"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_abpb1"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_545ij"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_fu7o2"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_nkvce"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_omlgh"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_uerb4"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_lvcf8"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_ct6ql"]
[sub_resource type="LabelSettings" id="LabelSettings_x4aj3"]
@@ -341,58 +353,84 @@ size_flags_horizontal = 0
label_settings = ExtResource("6_q3oua")
autowrap_mode = 2
[node name="UseItemPrompt" type="Label" parent="InventoryInfo/HBoxContainer/PlayerInfo/HBoxContainer/VBoxContainer"]
unique_name_in_owner = true
visible = false
custom_minimum_size = Vector2(800, 100)
layout_mode = 2
size_flags_horizontal = 0
text = "Use Item?"
label_settings = ExtResource("6_tmdno")
autowrap_mode = 2
[node name="ItemEffectLabel" type="Label" parent="InventoryInfo/HBoxContainer/PlayerInfo/HBoxContainer/VBoxContainer"]
unique_name_in_owner = true
custom_minimum_size = Vector2(800, 100)
layout_mode = 2
size_flags_horizontal = 0
text = "Use Item?
"
label_settings = SubResource("LabelSettings_mundu")
autowrap_mode = 2
[node name="UseButton" type="Button" parent="InventoryInfo/HBoxContainer/PlayerInfo/HBoxContainer/VBoxContainer"]
unique_name_in_owner = true
visible = false
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
size_flags_horizontal = 0
size_flags_vertical = 0
focus_neighbor_top = NodePath("../DropButton")
focus_neighbor_bottom = NodePath("../EquipButton")
theme = ExtResource("8_khyvo")
theme_override_colors/font_disabled_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_pressed_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_focus_color = Color(1, 0.94902, 0, 1)
theme_override_colors/font_color = Color(0.737255, 0.705882, 0.690196, 1)
theme_override_styles/focus = SubResource("StyleBoxEmpty_0kb6l")
theme_override_styles/disabled = SubResource("StyleBoxEmpty_ascpt")
theme_override_styles/pressed = SubResource("StyleBoxEmpty_abpb1")
theme_override_styles/normal = SubResource("StyleBoxEmpty_545ij")
button_mask = 0
text = "Use"
alignment = 0
[node name="EquipButton" type="Button" parent="InventoryInfo/HBoxContainer/PlayerInfo/HBoxContainer/VBoxContainer"]
unique_name_in_owner = true
visible = false
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
size_flags_horizontal = 0
size_flags_vertical = 0
theme = ExtResource("8_khyvo")
theme_override_colors/font_disabled_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_pressed_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_focus_color = Color(1, 0.94902, 0, 1)
theme_override_colors/font_color = Color(0.737255, 0.705882, 0.690196, 1)
theme_override_styles/focus = SubResource("StyleBoxEmpty_0kb6l")
theme_override_styles/disabled = SubResource("StyleBoxEmpty_fu7o2")
theme_override_styles/pressed = SubResource("StyleBoxEmpty_nkvce")
theme_override_styles/normal = SubResource("StyleBoxEmpty_545ij")
button_mask = 0
text = "Equip"
alignment = 0
[node name="DropButton" type="Button" parent="InventoryInfo/HBoxContainer/PlayerInfo/HBoxContainer/VBoxContainer"]
unique_name_in_owner = true
visible = false
custom_minimum_size = Vector2(200, 0)
layout_mode = 2
size_flags_horizontal = 0
size_flags_vertical = 0
focus_neighbor_top = NodePath("../EquipButton")
focus_neighbor_bottom = NodePath("../UseButton")
theme = ExtResource("8_khyvo")
theme_override_colors/font_disabled_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_pressed_color = Color(0.137255, 0.121569, 0.12549, 1)
theme_override_colors/font_focus_color = Color(1, 0.94902, 0, 1)
theme_override_colors/font_color = Color(0.737255, 0.705882, 0.690196, 1)
theme_override_styles/focus = SubResource("StyleBoxEmpty_omlgh")
theme_override_styles/disabled = SubResource("StyleBoxEmpty_uerb4")
theme_override_styles/pressed = SubResource("StyleBoxEmpty_lvcf8")
theme_override_styles/normal = SubResource("StyleBoxEmpty_ct6ql")
button_mask = 0
text = "Drop"
alignment = 0
[node name="ItemInfo" type="VBoxContainer" parent="InventoryInfo/HBoxContainer"]
layout_mode = 2