Moved Bar slot selection handling to inventory manager
This commit is contained in:
@@ -1,13 +1,27 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryManager : Node
|
||||
{
|
||||
public static InventoryManager Instance { get; private set; }
|
||||
[Signal]
|
||||
public delegate void SlotIndexChangedEventHandler(int newIndex);
|
||||
|
||||
public static InventoryManager Instance { get; private set; } = null!;
|
||||
public int CurrentSelectedSlotIndex
|
||||
{
|
||||
get => _currentSelectedSlotIndex;
|
||||
set
|
||||
{
|
||||
_currentSelectedSlotIndex = value;
|
||||
EmitSignalSlotIndexChanged(_currentSelectedSlotIndex);
|
||||
}
|
||||
}
|
||||
|
||||
public InventoryInstance playerInventory;
|
||||
public InventoryInstance playerInventory = new InventoryInstance();
|
||||
|
||||
private int _currentSelectedSlotIndex = 0;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
@@ -16,7 +30,6 @@ public partial class InventoryManager : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
playerInventory = new InventoryInstance();
|
||||
playerInventory.SlotAmount = 37;
|
||||
}
|
||||
|
||||
@@ -68,4 +81,14 @@ public partial class InventoryManager : Node
|
||||
{
|
||||
return playerInventory.AddItem(itemInstance);
|
||||
}
|
||||
}
|
||||
|
||||
public InventorySlot GetCurrentSelectedSlot()
|
||||
{
|
||||
if (CurrentSelectedSlotIndex < 0 || CurrentSelectedSlotIndex > 8)
|
||||
throw new ArgumentOutOfRangeException(
|
||||
nameof(CurrentSelectedSlotIndex),
|
||||
"currentInventoryBarIndex must be between 0 and 8 (inclusively)");
|
||||
|
||||
return playerInventory.Slots[CurrentSelectedSlotIndex];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#nullable enable
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
@@ -12,8 +11,6 @@ public partial class InventoryUi : Control
|
||||
|
||||
private int? _slotOnMouse;
|
||||
|
||||
private int _selectedSlot = 0;
|
||||
|
||||
private bool _inventoryExtended = false;
|
||||
private Tween? _inventoryExtensionTween;
|
||||
|
||||
@@ -63,7 +60,7 @@ public partial class InventoryUi : Control
|
||||
|
||||
private void SetSlotSelectPosition()
|
||||
{
|
||||
_slotSelect.Position = new Vector2(_selectedSlot * 100, 0);
|
||||
_slotSelect.Position = new Vector2(InventoryManager.Instance.CurrentSelectedSlotIndex * 100, 0);
|
||||
}
|
||||
|
||||
private void PopulateSlots()
|
||||
@@ -128,17 +125,17 @@ public partial class InventoryUi : Control
|
||||
|
||||
if (Input.IsActionJustPressed("ui_inventory_disadvance"))
|
||||
{
|
||||
_selectedSlot++;
|
||||
if (_selectedSlot > 9)
|
||||
_selectedSlot = 0;
|
||||
InventoryManager.Instance.CurrentSelectedSlotIndex++;
|
||||
if (InventoryManager.Instance.CurrentSelectedSlotIndex > 8)
|
||||
InventoryManager.Instance.CurrentSelectedSlotIndex = 0;
|
||||
SetSlotSelectPosition();
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("ui_inventory_advance"))
|
||||
{
|
||||
_selectedSlot--;
|
||||
if (_selectedSlot < 0)
|
||||
_selectedSlot = 9;
|
||||
InventoryManager.Instance.CurrentSelectedSlotIndex--;
|
||||
if (InventoryManager.Instance.CurrentSelectedSlotIndex < 0)
|
||||
InventoryManager.Instance.CurrentSelectedSlotIndex = 8;
|
||||
SetSlotSelectPosition();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user