From d76218f88f9ac693449b95d483b7f0a7c3dcc060 Mon Sep 17 00:00:00 2001 From: cblech Date: Mon, 19 May 2025 22:07:55 +0200 Subject: [PATCH] Moved Bar slot selection handling to inventory manager --- Babushka.csproj | 1 + Babushka.sln.DotSettings | 5 +++ .../Common/Inventory/InventoryManager.cs | 33 ++++++++++++++++--- .../CSharp/Common/Inventory/InventoryUi.cs | 17 ++++------ 4 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 Babushka.sln.DotSettings diff --git a/Babushka.csproj b/Babushka.csproj index e75f04c..70c0d51 100644 --- a/Babushka.csproj +++ b/Babushka.csproj @@ -2,5 +2,6 @@ net8.0 true + enable \ No newline at end of file diff --git a/Babushka.sln.DotSettings b/Babushka.sln.DotSettings new file mode 100644 index 0000000..ed91201 --- /dev/null +++ b/Babushka.sln.DotSettings @@ -0,0 +1,5 @@ + + Godot Signal + [Signal] +public delegate void $SignalName$EventHandler($END$); + suggestVariableName() \ No newline at end of file diff --git a/scripts/CSharp/Common/Inventory/InventoryManager.cs b/scripts/CSharp/Common/Inventory/InventoryManager.cs index e553cfc..edc8c47 100644 --- a/scripts/CSharp/Common/Inventory/InventoryManager.cs +++ b/scripts/CSharp/Common/Inventory/InventoryManager.cs @@ -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); } -} \ No newline at end of file + + 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]; + } +} diff --git a/scripts/CSharp/Common/Inventory/InventoryUi.cs b/scripts/CSharp/Common/Inventory/InventoryUi.cs index f798a18..1c06cf9 100644 --- a/scripts/CSharp/Common/Inventory/InventoryUi.cs +++ b/scripts/CSharp/Common/Inventory/InventoryUi.cs @@ -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(); } }