You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.4 KiB
174 lines
5.4 KiB
#nullable enable
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
public partial class InventoryUi : Control
|
|
{
|
|
private Control _slots;
|
|
private Control _slotsMover;
|
|
private InventoryInstance _playerInventory;
|
|
private Control _slotSelect;
|
|
|
|
private int? _slotOnMouse;
|
|
|
|
private int _selectedSlot = 0;
|
|
|
|
private bool _inventoryExtended = false;
|
|
private Tween? _inventoryExtensionTween;
|
|
|
|
[Export]
|
|
private float _inventoryClosedOffset = 0f;
|
|
|
|
[Export]
|
|
private float _inventoryOpenedOffset = 200f;
|
|
|
|
public override void _Ready()
|
|
{
|
|
GD.Print("Ready inventory ui");
|
|
_slots = GetNode<Control>("SlotsContainer/SlotsMover/Slots");
|
|
_slotsMover = GetNode<Control>("SlotsContainer/SlotsMover");
|
|
_playerInventory = InventoryManager.Instance.playerInventory;
|
|
_slotSelect = GetNode<Control>("SlotsContainer/SlotSelectContainer/Selector");
|
|
//PopulateSlots();
|
|
SubscribeSlots();
|
|
SetSlotContent();
|
|
SetSlotSelectPosition();
|
|
InventoryManager.Instance.playerInventory.InventoryContentsChanged += 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;
|
|
|
|
var amountText = inventorySlot.itemInstance != null &&
|
|
inventorySlot.itemInstance.amount != 1
|
|
? inventorySlot.itemInstance.amount.ToString()
|
|
: "";
|
|
uiSlot!.amountLabel.Text = amountText;
|
|
}
|
|
}
|
|
|
|
private void SetSlotSelectPosition()
|
|
{
|
|
_slotSelect.Position = new Vector2(_selectedSlot * 100, 0);
|
|
}
|
|
|
|
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 SubscribeSlots()
|
|
{
|
|
for (var index = 0; index < _playerInventory.Slots.Count; index++)
|
|
{
|
|
var slotInstance = _slots.GetChild<SlotUi>(index);
|
|
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)
|
|
{
|
|
if (!_inventoryExtended) return;
|
|
|
|
GD.Print($"Clicked slot {index}");
|
|
if (_slotOnMouse == null)
|
|
{
|
|
if (_playerInventory.Slots[index].IsEmpty()) return;
|
|
_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();
|
|
}
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (Input.IsActionJustPressed("ui_inventory_open_close"))
|
|
{
|
|
InputInventoryOpenClose();
|
|
}
|
|
|
|
if (Input.IsActionJustPressed("ui_inventory_disadvance"))
|
|
{
|
|
_selectedSlot++;
|
|
if (_selectedSlot > 9)
|
|
_selectedSlot = 0;
|
|
SetSlotSelectPosition();
|
|
}
|
|
|
|
if (Input.IsActionJustPressed("ui_inventory_advance"))
|
|
{
|
|
_selectedSlot--;
|
|
if (_selectedSlot < 0)
|
|
_selectedSlot = 9;
|
|
SetSlotSelectPosition();
|
|
}
|
|
}
|
|
|
|
private void InputInventoryOpenClose()
|
|
{
|
|
_inventoryExtensionTween?.Kill();
|
|
|
|
_inventoryExtended = !_inventoryExtended;
|
|
if (_inventoryExtended)
|
|
{
|
|
//GD.Print("Open inventory");
|
|
_inventoryExtensionTween = GetTree().CreateTween();
|
|
_slotSelect.Hide();
|
|
_inventoryExtensionTween
|
|
.TweenProperty(_slotsMover, "offset_bottom", _inventoryOpenedOffset, 0.4)
|
|
.SetTrans(Tween.TransitionType.Quad)
|
|
.SetEase(Tween.EaseType.Out);
|
|
}
|
|
else
|
|
{
|
|
//GD.Print("Close inventory");
|
|
_inventoryExtensionTween = GetTree().CreateTween();
|
|
_slotSelect.Show();
|
|
_inventoryExtensionTween
|
|
.TweenProperty(_slotsMover, "offset_bottom", _inventoryClosedOffset, 0.4)
|
|
.SetTrans(Tween.TransitionType.Quad)
|
|
.SetEase(Tween.EaseType.Out);
|
|
|
|
_slotOnMouse = null;
|
|
}
|
|
}
|
|
} |