Display items in inventory

This commit is contained in:
2024-09-09 02:27:03 -07:00
parent 8ac19797f0
commit ad5213a940
11 changed files with 558 additions and 203 deletions

View File

@@ -1,13 +1,13 @@
using Chickensoft.AutoInject;
using Chickensoft.AutoInject;
using Chickensoft.GodotNodeInterfaces;
using Chickensoft.Introspection;
using GameJamDungeon;
using Godot;
using System.Collections.Generic;
using System.Linq;
public interface IInventoryMenu : IControl
{
public void PopulateItems(List<IInventoryItem> items);
public void PopulateItems();
public void ClearItems();
}
@@ -17,188 +17,82 @@ public partial class InventoryMenu : Control, IInventoryMenu
{
public override void _Notification(int what) => this.Notify(what);
[Node] public IVBoxContainer ItemList { get; set; } = default!;
[Dependency]
public IGameRepo GameRepo => this.DependOn<IGameRepo>();
[Node] public TextureRect Cursor { get; set; } = default!;
// Player Info
[Node] public Label FloorLabel { get; set; } = default!;
[Node] public Label CurrentLevelLabel { get; set; } = default!;
[Node] public Label EXPValue { get; set; } = default!;
[Node] public Label HPValue { get; set; } = default!;
[Node] public Label HPBonusLabel { get; set; } = default!;
[Node] public Label VTValue { get; set; } = default!;
[Node] public Label VTBonusLabel { get; set; } = default!;
[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!;
[Dependency] public IGameRepo GameRepo => this.DependOn<IGameRepo>();
// Item Menu
[Node] public Label BackArrow { get; set; } = default!;
[Node] public Label ForwardArrow { get; set; } = default!;
[Node] public Control ItemsPage { get; set; } = default!;
private int _currentSelection = 0;
public void PopulateItems(List<IInventoryItem> items)
private enum InventoryPageNumber
{
foreach (var item in items)
{
var label = new ItemLabel(item) { Text = item.Info.Name };
ItemList.AddChild(label);
}
if (ItemList.GetChildCount() > 0)
CallDeferred(nameof(InitializeInventoryMenu));
FirstPage,
SecondPage
}
public void InitializeInventoryMenu()
private InventoryPageNumber _currentPageNumber = InventoryPageNumber.FirstPage;
private string ITEM_SLOT_SCENE = "res://src/inventory_menu/ItemSlot.tscn";
private const int _itemsPerPage = 10;
public void PopulateItems()
{
if (ItemList.GetChildCount() > 0)
var inventory = GameRepo.InventoryItems.Value;
var numberOfItemsToDisplay = inventory.Count <= _itemsPerPage ? inventory.Count : _itemsPerPage;
var indexToStart = _currentPageNumber == InventoryPageNumber.FirstPage ? 0 : 10;
ForwardArrow.Text = "";
BackArrow.Text = "";
if (_currentPageNumber == InventoryPageNumber.FirstPage && inventory.Count > _itemsPerPage)
{
var currentItem = ItemList.GetChild<ItemLabel>(_currentSelection);
SetCursorLocation(currentItem);
ForwardArrow.Text = "►";
BackArrow.Text = "";
}
if (_currentPageNumber == InventoryPageNumber.SecondPage)
{
ForwardArrow.Text = "";
BackArrow.Text = "◄";
}
foreach (ItemLabel item in ItemList.GetChildren())
for (var i = indexToStart; i < numberOfItemsToDisplay + indexToStart; i++)
{
if (item.InventoryItem == GameRepo.EquippedWeapon || item.InventoryItem == GameRepo.EquippedArmor || item.InventoryItem == GameRepo.EquippedAccessory)
item.EquipItem();
var item = inventory.ElementAt(i);
var itemScene = GD.Load<PackedScene>(ITEM_SLOT_SCENE);
var itemSlot = itemScene.Instantiate<IItemSlot>();
ItemsPage.AddChildEx(itemSlot);
itemSlot.ItemName.Text = item.Info.Name;
itemSlot.ItemTexture.Texture = item.Info.Texture;
itemSlot.EquipBonus.Text = "...";
}
}
public void ClearItems()
{
foreach (var item in ItemList.GetChildren())
ItemList.RemoveChild(item);
}
public void SetCursorLocation(Control menuItem)
{
var position = menuItem.GlobalPosition;
var size = menuItem.Size;
Cursor.GlobalPosition = new Vector2(position.X, position.Y + size.Y / 2.0f) - Cursor.Size / 2.0f - new Vector2(15, -5);
}
public void SetCursorToPrevious()
{
if (ItemList.GetChildCount() == 0)
return;
if (_currentSelection > 0)
var items = ItemsPage.GetChildren().OfType<ItemSlot>();
foreach (var item in items)
{
_currentSelection -= 1;
var selectedMenuItem = ItemList.GetChild<Control>(_currentSelection);
SetCursorLocation(selectedMenuItem);
ItemsPage.RemoveChild(item);
CallDeferred(nameof(item.QueueFree));
}
}
public void SetCursorToNext()
{
if (ItemList.GetChildCount() == 0)
return;
if (_currentSelection < ItemList.GetChildCount() - 1)
{
_currentSelection += 1;
var selectedMenuItem = ItemList.GetChild<Control>(_currentSelection);
SetCursorLocation(selectedMenuItem);
}
}
public override void _Process(double delta)
{
var input = Vector2.Zero;
if (Input.IsActionJustPressed(GameInputs.MoveUp))
SetCursorToPrevious();
if (Input.IsActionJustPressed(GameInputs.MoveDown))
SetCursorToNext();
if (Input.IsActionJustPressed(GameInputs.Attack))
EquipOrUnequipCurrentItem();
if (Input.IsActionJustPressed(GameInputs.Throw))
{
var selectedMenuItem = ItemList.GetChild<ItemLabel>(_currentSelection);
GameRepo.InventoryItems.Value.Remove(selectedMenuItem.InventoryItem);
ItemList.RemoveChild(selectedMenuItem);
}
}
public void EquipOrUnequipCurrentItem()
{
if (ItemList.GetChildCount() == 0)
return;
var currentlySelectedItem = ItemList.GetChild<ItemLabel>(_currentSelection);
if (currentlySelectedItem.InventoryItem is Weapon weapon && GameRepo.EquippedWeapon == weapon)
{
UnequipItem(currentlySelectedItem);
GameRepo.OnWeaponEquipped(new Weapon());
}
else if (currentlySelectedItem.InventoryItem is Armor armor && GameRepo.EquippedArmor == armor)
{
UnequipItem(currentlySelectedItem);
GameRepo.OnArmorEquipped(null);
}
else if (currentlySelectedItem.InventoryItem is Accessory accessory && GameRepo.EquippedAccessory == accessory)
{
UnequipItem(currentlySelectedItem);
GameRepo.OnAccessoryEquipped(null);
}
else if (currentlySelectedItem.InventoryItem is IEquippable)
EquipItem(currentlySelectedItem);
}
private void EquipItem(ItemLabel currentItem) => EquipItemInternal(currentItem, (dynamic)currentItem.InventoryItem);
private void UnequipItem(ItemLabel item)
{
item.UnequipItem();
}
private void EquipItemInternal(ItemLabel currentItem, Accessory accessory)
{
foreach (ItemLabel item in ItemList.GetChildren())
{
if (item.InventoryItem is Accessory)
UnequipItem(item);
}
GameRepo.OnAccessoryEquipped(accessory);
currentItem.EquipItem();
}
private void EquipItemInternal(ItemLabel currentItem, Armor armor)
{
foreach (ItemLabel item in ItemList.GetChildren())
{
if (item.InventoryItem is Armor)
UnequipItem(item);
}
GameRepo.OnArmorEquipped(armor);
currentItem.EquipItem();
}
private void EquipItemInternal(ItemLabel currentItem, Weapon weapon)
{
foreach (ItemLabel item in ItemList.GetChildren())
{
if (item.InventoryItem is Weapon)
UnequipItem(item);
}
GameRepo.OnWeaponEquipped(weapon);
currentItem.EquipItem();
}
}
public partial class ItemLabel : Label
{
public ItemLabel(IInventoryItem inventoryItem)
{
InventoryItem = inventoryItem;
LabelSettings = UnequippedItemFont;
}
public IInventoryItem InventoryItem { get; set; } = default!;
private static LabelSettings UnequippedItemFont => GD.Load<LabelSettings>("res://src/inventory_menu/InventoryLabelSettings.tres");
private static LabelSettings EquippedItemFont => GD.Load<LabelSettings>("res://src/inventory_menu/EquippedInventoryLabelSettings.tres");
public void EquipItem()
{
LabelSettings = EquippedItemFont;
}
public void UnequipItem()
{
LabelSettings = UnequippedItemFont;
}
}