Move items
This commit is contained in:
@@ -0,0 +1 @@
|
||||
uid://bxre8ecfx3xx
|
||||
@@ -5,7 +5,7 @@ namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryManager : Node
|
||||
{
|
||||
public InventoryManager Instance { get; private set; }
|
||||
public static InventoryManager Instance { get; private set; }
|
||||
|
||||
public InventoryInstance playerInventory;
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
uid://rxaoggm0qxgm
|
||||
@@ -0,0 +1,17 @@
|
||||
using Godot;
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryTestScript : Node
|
||||
{
|
||||
[Export]
|
||||
private ItemResource _testItemToCreate;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
InventoryManager.Instance.CreateItem(_testItemToCreate, InventoryManager.Instance.playerInventory);
|
||||
InventoryManager.Instance.CreateItem(_testItemToCreate, InventoryManager.Instance.playerInventory);
|
||||
InventoryManager.Instance.CreateItem(_testItemToCreate, InventoryManager.Instance.playerInventory);
|
||||
InventoryManager.Instance.CreateItem(_testItemToCreate, InventoryManager.Instance.playerInventory);
|
||||
GD.Print("Added items");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://b2jhdxcrhtm2d
|
||||
@@ -1,6 +0,0 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class InventoryUI : Control
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class InventoryUi : Control
|
||||
{
|
||||
private GridContainer _slots;
|
||||
private InventoryInstance _playerInventory;
|
||||
|
||||
private int? _slotOnMouse;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
GD.Print("Ready inventory ui");
|
||||
_slots = GetNode<GridContainer>("Slots");
|
||||
_playerInventory = InventoryManager.Instance.playerInventory;
|
||||
PopulateSlots();
|
||||
SetSlotContent();
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
UnsubscribeSlots();
|
||||
}
|
||||
|
||||
private void SetSlotContent()
|
||||
{
|
||||
for (var i = 0; i < _playerInventory.Slots.Count; i++)
|
||||
{
|
||||
var inventorySlot = _playerInventory.Slots[i];
|
||||
var uiSlot = _slots.GetChild(i) as SlotUi;
|
||||
|
||||
uiSlot!.nameLabel.Text = inventorySlot.itemInstance?.blueprint.name ?? "";
|
||||
uiSlot!.nameLabel.LabelSettings = uiSlot!.nameLabel.LabelSettings.Duplicate() as LabelSettings;
|
||||
uiSlot!.nameLabel.LabelSettings!.FontColor = inventorySlot.itemInstance?.blueprint.color ?? Colors.White;
|
||||
}
|
||||
}
|
||||
|
||||
private void PopulateSlots()
|
||||
{
|
||||
var slotPackedScene = GD.Load<PackedScene>("res://prefabs/UI/Inventory/Slot.tscn");
|
||||
for (var index = 0; index < _playerInventory.Slots.Count; index++)
|
||||
{
|
||||
var slotInstance = slotPackedScene.Instantiate<SlotUi>();
|
||||
slotInstance.index = index;
|
||||
slotInstance.Clicked += SlotClicked;
|
||||
_slots.AddChild(slotInstance);
|
||||
}
|
||||
}
|
||||
|
||||
private void UnsubscribeSlots()
|
||||
{
|
||||
for (var index = 0; index < _playerInventory.Slots.Count; index++)
|
||||
{
|
||||
var slotInstance = _slots.GetChild(index) as SlotUi;
|
||||
slotInstance!.Clicked -= SlotClicked;
|
||||
}
|
||||
}
|
||||
|
||||
private void SlotClicked(int index)
|
||||
{
|
||||
GD.Print($"Clicked slot {index}");
|
||||
if (_slotOnMouse == null)
|
||||
{
|
||||
_slotOnMouse = index;
|
||||
GD.Print($"Slot on mouse: {_slotOnMouse}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var sourceSlot = _slotOnMouse.Value;
|
||||
var destinationSlot = index;
|
||||
InventoryManager.Instance.MoveItem(_playerInventory, sourceSlot, _playerInventory, destinationSlot);
|
||||
_slotOnMouse = null;
|
||||
SetSlotContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://be54lnb6gg81f
|
||||
@@ -0,0 +1,27 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class SlotUi : Control
|
||||
{
|
||||
public Label nameLabel;
|
||||
public int index;
|
||||
|
||||
[Signal] public delegate void ClickedEventHandler(int index);
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
nameLabel = GetNode<Label>("NameLabel");
|
||||
}
|
||||
|
||||
public void _on_gui_input(InputEvent ev)
|
||||
{
|
||||
if (ev is not InputEventMouseButton mev) return;
|
||||
|
||||
if (mev.Pressed)
|
||||
{
|
||||
EmitSignalClicked(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bh6wcyxh1f0cv
|
||||
Reference in New Issue
Block a user