Overhaul item and inventory and clean up bits and pieces
This commit is contained in:
139
Zennysoft.Game.Ma/src/ui/inventory_menu/InventoryMenu.cs
Normal file
139
Zennysoft.Game.Ma/src/ui/inventory_menu/InventoryMenu.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Zennysoft.Game.Ma;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public partial class InventoryMenu : Control, IInventoryMenu
|
||||
{
|
||||
public override void _Notification(int what) => this.Notify(what);
|
||||
|
||||
[Dependency] private IPlayer _player => this.DependOn<IPlayer>();
|
||||
|
||||
[Dependency] private IGame _game => this.DependOn<IGame>();
|
||||
|
||||
[Dependency] private IGameRepo _gameRepo => this.DependOn<IGameRepo>();
|
||||
|
||||
[Node] public Label ItemName { get; set; }
|
||||
|
||||
[Node] public Label ItemFlavor { get; set; }
|
||||
|
||||
[Node] public Label ItemStats { get; set; }
|
||||
|
||||
[Node] public ActionPanel ActionPanel { get; set; }
|
||||
|
||||
[Node] public VBoxContainer Inventory { get; set; }
|
||||
|
||||
[Node] public Control MenuPanel { get; set; }
|
||||
|
||||
[Node] public AugmentableItemsMenu AugmentMenu { get; set; }
|
||||
|
||||
private List<IItemSlot> ItemSlots;
|
||||
|
||||
private IItemSlot _currentlySelected;
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
ItemSlots = [.. Inventory.GetChildren().OfType<IItemSlot>()];
|
||||
ItemSlots.ForEach(x => x.ItemPressed += ItemPressed);
|
||||
ItemSlots.ForEach(x => x.ItemSelected += ItemSelected);
|
||||
VisibilityChanged += ResetInventoryState;
|
||||
ActionPanel.ActionPanelClosing += ActionPanel_ActionPanelClosing;
|
||||
ActionPanel.ReturnToGameAction += ActionPanel_ReturnToGameAction;
|
||||
ActionPanel.AugmentMenuRequested += ActionPanel_AugmentMenuRequested;
|
||||
AugmentMenu.AugmentMenuClosing += AugmentMenu_AugmentMenuClosing;
|
||||
AugmentMenu.FocusMode = FocusModeEnum.None;
|
||||
ClearDescriptionBox();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (Input.IsActionJustPressed(GameInputs.MoveUp) && _currentlySelected != ItemSlots.First())
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
if (Input.IsActionJustPressed(GameInputs.MoveDown) && _currentlySelected != ItemSlots.Last(x => x.Item.Value != null))
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
}
|
||||
|
||||
private void ActionPanel_AugmentMenuRequested()
|
||||
{
|
||||
ReleaseFocus();
|
||||
ItemSlots.ForEach(x => x.ItemPressed -= ItemPressed);
|
||||
ItemSlots.ForEach(x => x.ItemSelected -= ItemSelected);
|
||||
MenuPanel.Hide();
|
||||
MenuPanel.FocusMode = FocusModeEnum.None;
|
||||
AugmentMenu.FocusMode = FocusModeEnum.All;
|
||||
SetProcessInput(false);
|
||||
AugmentMenu.SetProcessInput(true);
|
||||
AugmentMenu.OpenAugmentMenu(_currentlySelected.Item.Value as IAugmentItem);
|
||||
}
|
||||
|
||||
private void ActionPanel_ReturnToGameAction()
|
||||
{
|
||||
_gameRepo.CloseInventory();
|
||||
}
|
||||
|
||||
private void ItemPressed(IItemSlot selectedItem)
|
||||
{
|
||||
SetProcessInput(false);
|
||||
ActionPanel.SetProcessInput(true);
|
||||
ActionPanel.ShowPanel(selectedItem.Item.Value);
|
||||
ActionPanel.FocusActionPanel();
|
||||
SfxDatabase.Instance.Play(SoundEffect.SelectUI);
|
||||
}
|
||||
|
||||
private void ItemSelected(IItemSlot selectedItem)
|
||||
{
|
||||
_currentlySelected = selectedItem;
|
||||
ItemName.Text = selectedItem.Item.Value.ItemName;
|
||||
ItemFlavor.Text = selectedItem.Item.Value.Description;
|
||||
}
|
||||
|
||||
private void ResetInventoryState()
|
||||
{
|
||||
var inventory = _player.Inventory.Items;
|
||||
ItemSlots.ForEach(x => x.SetEmpty());
|
||||
ClearDescriptionBox();
|
||||
|
||||
for (var i = 0; i < inventory.Count; i++)
|
||||
ItemSlots[i].SetItemToSlot(inventory[i]);
|
||||
|
||||
if (_currentlySelected == null && inventory.Any())
|
||||
_currentlySelected = ItemSlots.First();
|
||||
if (inventory.Any())
|
||||
_currentlySelected.FocusItem();
|
||||
ActionPanel.Hide();
|
||||
}
|
||||
|
||||
private void ClearDescriptionBox()
|
||||
{
|
||||
ItemName.Text = string.Empty;
|
||||
ItemFlavor.Text = string.Empty;
|
||||
ItemStats.Text = string.Empty;
|
||||
}
|
||||
|
||||
private void ActionPanel_ActionPanelClosing()
|
||||
{
|
||||
ActionPanel.Hide();
|
||||
SetProcessInput(true);
|
||||
ActionPanel.SetProcessInput(false);
|
||||
if (!_player.Inventory.Items.Contains(_currentlySelected.Item.Value))
|
||||
_currentlySelected = null;
|
||||
ResetInventoryState();
|
||||
}
|
||||
|
||||
private void AugmentMenu_AugmentMenuClosing()
|
||||
{
|
||||
ItemSlots.ForEach(x => x.ItemPressed += ItemPressed);
|
||||
ItemSlots.ForEach(x => x.ItemSelected += ItemSelected);
|
||||
MenuPanel.Show();
|
||||
MenuPanel.FocusMode = FocusModeEnum.All;
|
||||
AugmentMenu.FocusMode = FocusModeEnum.None;
|
||||
SetProcessInput(true);
|
||||
AugmentMenu.SetProcessInput(false);
|
||||
AugmentMenu.Hide();
|
||||
ResetInventoryState();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user