Add item spawn menu
Fix game over bug Start adding more implementation for jewels
This commit is contained in:
203
Zennysoft.Game.Ma/src/ui/inventory_menu/ItemRescueMenu.cs
Normal file
203
Zennysoft.Game.Ma/src/ui/inventory_menu/ItemRescueMenu.cs
Normal file
@@ -0,0 +1,203 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Introspection;
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Zennysoft.Game.Implementation;
|
||||
using Zennysoft.Game.Ma;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
[Meta(typeof(IAutoNode))]
|
||||
public partial class ItemRescueMenu : Control
|
||||
{
|
||||
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 Button InteractButton { get; set; }
|
||||
|
||||
[Node] public Button DropButton { get; set; }
|
||||
|
||||
[Node] public Control ActionPanel { get; set; }
|
||||
|
||||
private List<IItemSlot> ItemSlots;
|
||||
|
||||
private List<Label> ItemCountLabels;
|
||||
|
||||
private IItemSlot _currentlySelected;
|
||||
|
||||
public event Action MenuClosing;
|
||||
|
||||
#region ItemSlots
|
||||
[Node] public IItemSlot ItemSlot01 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot02 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot03 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot04 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot05 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot06 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot07 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot08 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot09 { get; set; }
|
||||
[Node] public IItemSlot ItemSlot10 { get; set; }
|
||||
|
||||
[Node] public Label ItemCount01 { get; set; }
|
||||
[Node] public Label ItemCount02 { get; set; }
|
||||
[Node] public Label ItemCount03 { get; set; }
|
||||
[Node] public Label ItemCount04 { get; set; }
|
||||
[Node] public Label ItemCount05 { get; set; }
|
||||
[Node] public Label ItemCount06 { get; set; }
|
||||
[Node] public Label ItemCount07 { get; set; }
|
||||
[Node] public Label ItemCount08 { get; set; }
|
||||
[Node] public Label ItemCount09 { get; set; }
|
||||
[Node] public Label ItemCount10 { get; set; }
|
||||
#endregion
|
||||
|
||||
public void OnResolved()
|
||||
{
|
||||
ItemSlots = [ItemSlot01, ItemSlot02, ItemSlot03, ItemSlot04, ItemSlot05, ItemSlot06, ItemSlot07, ItemSlot08, ItemSlot09, ItemSlot10];
|
||||
ItemCountLabels = [ItemCount01, ItemCount02, ItemCount03, ItemCount04, ItemCount05, ItemCount06, ItemCount07, ItemCount08, ItemCount09, ItemCount10];
|
||||
foreach (var slot in ItemSlots)
|
||||
{
|
||||
slot.ItemSelected += Slot_FocusEntered;
|
||||
slot.ItemPressed += Slot_ItemPressed;
|
||||
}
|
||||
VisibilityChanged += ResetInventoryState;
|
||||
InteractButton.Pressed += InteractButton_Pressed;
|
||||
DropButton.Pressed += DropButton_Pressed;
|
||||
_currentlySelected = ItemSlot01;
|
||||
}
|
||||
|
||||
private void InteractButton_Pressed()
|
||||
{
|
||||
if (_currentlySelected != null)
|
||||
{
|
||||
_player.Inventory.PickUpItem(_currentlySelected.Item.Value);
|
||||
_game.RescuedItems.Items.Remove(_currentlySelected.Item.Value);
|
||||
CloseActionMenu();
|
||||
ResetInventoryState();
|
||||
}
|
||||
}
|
||||
|
||||
private void DropButton_Pressed()
|
||||
{
|
||||
_game.RescuedItems.Items.Remove(_currentlySelected.Item.Value);
|
||||
ResetInventoryState();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (!Visible)
|
||||
return;
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.Inventory))
|
||||
GetViewport().SetInputAsHandled();
|
||||
|
||||
if (ActionPanel.Visible && Input.IsActionJustPressed(GameInputs.Interact))
|
||||
{
|
||||
CloseActionMenu();
|
||||
SfxDatabase.Instance.Play(SoundEffect.CancelUI);
|
||||
GetViewport().SetInputAsHandled();
|
||||
}
|
||||
else if (Input.IsActionJustPressed(GameInputs.Interact))
|
||||
{
|
||||
Hide();
|
||||
SfxDatabase.Instance.Play(SoundEffect.CancelUI);
|
||||
GetViewport().SetInputAsHandled();
|
||||
MenuClosing.Invoke();
|
||||
}
|
||||
if (!ActionPanel.Visible && Input.IsActionJustPressed(GameInputs.MoveUp))
|
||||
{
|
||||
if (ItemSlots.Any() && ItemSlots.First(x => x.Visible) != _currentlySelected)
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
}
|
||||
if (!ActionPanel.Visible && Input.IsActionJustPressed(GameInputs.MoveDown))
|
||||
{
|
||||
if (ItemSlots.Any() && ItemSlots.Last(x => x.Visible) != _currentlySelected)
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed(GameInputs.MoveDown) && ActionPanel.Visible && InteractButton.HasFocus())
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
if (Input.IsActionJustPressed(GameInputs.MoveUp) && ActionPanel.Visible && DropButton.HasFocus())
|
||||
SfxDatabase.Instance.Play(SoundEffect.MoveUI);
|
||||
}
|
||||
|
||||
private void Slot_ItemPressed(IItemSlot slot)
|
||||
{
|
||||
var item = slot.Item.Value;
|
||||
|
||||
InteractButton.Disabled = false;
|
||||
DropButton.Disabled = false;
|
||||
InteractButton.FocusMode = FocusModeEnum.All;
|
||||
DropButton.FocusMode = FocusModeEnum.All;
|
||||
ActionPanel.Show();
|
||||
InteractButton.GrabFocus();
|
||||
}
|
||||
|
||||
private void ResetInventoryState()
|
||||
{
|
||||
foreach (var item in ItemSlots)
|
||||
{
|
||||
item.Hide();
|
||||
item.Disabled = true;
|
||||
item.FocusMode = FocusModeEnum.None;
|
||||
ItemFlavor.Text = string.Empty;
|
||||
ItemName.Text = string.Empty;
|
||||
ItemStats.Text = string.Empty;
|
||||
}
|
||||
|
||||
foreach (var item in ItemCountLabels)
|
||||
item.Text = string.Empty;
|
||||
|
||||
for (var i = 0; i < _game.RescuedItems.Items.Count; i++)
|
||||
{
|
||||
var item = _game.RescuedItems.Items[i];
|
||||
ItemSlots[i].Item.OnNext(item);
|
||||
ItemSlots[i].FocusMode = FocusModeEnum.All;
|
||||
ItemSlots[i].Disabled = false;
|
||||
ItemSlots[i].Show();
|
||||
if (item is IStackable stackable)
|
||||
ItemCountLabels[i].Text = $"x{stackable.Count.Value:D2}";
|
||||
}
|
||||
|
||||
if (_player.Inventory.AtCapacity())
|
||||
{
|
||||
foreach (var item in ItemSlots)
|
||||
{
|
||||
item.Disabled = true;
|
||||
item.FocusMode = FocusModeEnum.None;
|
||||
}
|
||||
}
|
||||
else if (_currentlySelected != null)
|
||||
_currentlySelected.GrabFocus();
|
||||
else
|
||||
_currentlySelected = ItemSlots.First();
|
||||
}
|
||||
|
||||
private void Slot_FocusEntered(IItemSlot slot)
|
||||
{
|
||||
if (_currentlySelected.Item.Value == null)
|
||||
return;
|
||||
_currentlySelected = slot;
|
||||
var item = slot.Item.Value;
|
||||
ItemName.Text = item.ItemName;
|
||||
ItemFlavor.Text = item.Description;
|
||||
}
|
||||
|
||||
private void CloseActionMenu()
|
||||
{
|
||||
_currentlySelected.GrabFocus();
|
||||
ActionPanel.Hide();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user