Files

80 lines
2.0 KiB
C#
Raw Permalink Normal View History

2025-05-20 18:27:40 +02:00
using Godot;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class ItemOnGround2D : Node
{
private ItemInstance _itemInstance;
2025-05-24 23:16:41 +02:00
2025-07-17 20:44:28 +02:00
[Export] public bool IsActive = true;
2025-05-24 23:16:41 +02:00
[Export] private bool _infiniteSupply = false;
[Export] private int _finiteSupply = 1;
2025-05-20 18:27:40 +02:00
2025-05-24 23:16:41 +02:00
private int pickUpCounter = 0;
2025-07-05 13:18:19 +02:00
[Signal] public delegate void SuccessfulPickUpEventHandler();
2025-05-20 18:27:40 +02:00
private Label _itemLabel => GetNode<Label>("ItemLabel");
private Label _pickupErrorLabel => GetNode<Label>("PickupErrorLabel");
2025-05-20 20:42:32 +02:00
private Sprite2D _iconSprite => GetNode<Sprite2D>("Icon");
2025-05-20 18:27:40 +02:00
public ItemInstance itemInstance
{
get => _itemInstance;
set
{
_itemInstance = value;
UpdateVisuals();
}
}
2025-05-20 20:42:32 +02:00
public override void _Ready()
{
UpdateVisuals();
_pickupErrorLabel.Text = "";
}
2025-05-20 18:27:40 +02:00
public void TryPickUp()
{
2025-05-24 23:16:41 +02:00
if (!IsActive)
return;
2025-05-20 18:27:40 +02:00
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
2025-07-05 13:18:19 +02:00
EmitSignal(SignalName.SuccessfulPickUp);
2025-05-20 18:27:40 +02:00
if (result == InventoryActionResult.Success)
{
if (!_infiniteSupply)
{
2025-05-24 23:16:41 +02:00
pickUpCounter++;
if (pickUpCounter >= _finiteSupply)
{
QueueFree();
}
2025-05-20 18:27:40 +02:00
}
}
else
{
_pickupErrorLabel.Text = "Inventory Full";
var tween = GetTree().CreateTween();
tween.TweenInterval(2);
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
}
}
public void UpdateVisuals()
{
2025-05-24 23:16:41 +02:00
if (!IsActive)
return;
2025-05-20 20:42:32 +02:00
_iconSprite.Texture = itemInstance?.blueprint?.icon;
if (_iconSprite.Texture == null)
{
_itemLabel.Text = itemInstance?.blueprint?.name ?? "Error Item";
}
else
{
_itemLabel.Text = "";
}
}
}