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.
155 lines
4.3 KiB
155 lines
4.3 KiB
using Babushka.scripts.CSharp.Common.Savegame;
|
|
using Godot;
|
|
using Godot.Collections;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
public partial class ItemOnGround2D : Node, ISaveable
|
|
{
|
|
private ItemInstance _itemInstance;
|
|
|
|
[Export] public bool IsActive = true;
|
|
[Export] private bool _infiniteSupply = false;
|
|
[Export] private int _finiteSupply = 1;
|
|
|
|
private int pickUpCounter = 0;
|
|
|
|
[Signal] public delegate void SuccessfulPickUpEventHandler();
|
|
|
|
|
|
private Label _itemLabel => GetNode<Label>("ItemLabel");
|
|
private Label _pickupErrorLabel => GetNode<Label>("PickupErrorLabel");
|
|
private Sprite2D _iconSprite => GetNode<Sprite2D>("Icon");
|
|
|
|
public ItemInstance itemInstance
|
|
{
|
|
get => _itemInstance;
|
|
set
|
|
{
|
|
_itemInstance = value;
|
|
UpdateVisuals();
|
|
}
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
LoadFromSaveData();
|
|
UpdateVisuals();
|
|
_pickupErrorLabel.Text = "";
|
|
}
|
|
|
|
public void TryPickUp()
|
|
{
|
|
if (!IsActive)
|
|
return;
|
|
|
|
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
|
|
EmitSignal(SignalName.SuccessfulPickUp);
|
|
if (result == InventoryActionResult.Success)
|
|
{
|
|
Pickup();
|
|
}
|
|
else
|
|
{
|
|
FailToPickup();
|
|
}
|
|
}
|
|
|
|
private void Pickup()
|
|
{
|
|
if (!_infiniteSupply)
|
|
{
|
|
pickUpCounter++;
|
|
if (pickUpCounter >= _finiteSupply)
|
|
{
|
|
QueueFree();
|
|
}
|
|
|
|
UpdateSaveData();
|
|
}
|
|
}
|
|
|
|
private void FailToPickup()
|
|
{
|
|
_pickupErrorLabel.Text = "Inventory Full";
|
|
var tween = GetTree().CreateTween();
|
|
tween.TweenInterval(2);
|
|
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
|
|
}
|
|
|
|
public void UpdateVisuals()
|
|
{
|
|
if (!IsActive)
|
|
return;
|
|
|
|
_iconSprite.Texture = itemInstance?.blueprint?.icon;
|
|
if (_iconSprite.Texture == null)
|
|
{
|
|
_itemLabel.Text = itemInstance?.blueprint?.name ?? "Error Item";
|
|
}
|
|
else
|
|
{
|
|
_itemLabel.Text = "";
|
|
}
|
|
}
|
|
|
|
// todo: What do we do with instances that are created at runtime?
|
|
public void UpdateSaveData()
|
|
{
|
|
var payloadData = new Dictionary<string, Variant>
|
|
{
|
|
{"pickupCounter", pickUpCounter}
|
|
};
|
|
|
|
string id = GetMeta("SaveID").AsString();
|
|
SavegameService.AppendDataToSave( id, payloadData);
|
|
}
|
|
|
|
public void LoadFromSaveData()
|
|
{
|
|
if (_infiniteSupply)
|
|
return;
|
|
|
|
// standard check: how many times has this item been collected?
|
|
string id = GetMeta("SaveID").AsString();
|
|
|
|
Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
|
if (save.Count > 0)
|
|
{
|
|
if(save.TryGetValue("pickupCounter", out Variant countVar))
|
|
{
|
|
int count = countVar.AsInt32();
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Pickup();
|
|
}
|
|
}
|
|
}
|
|
|
|
//separate check for unique items: If already in inventory, delete this instance.
|
|
ItemResource itemResource = itemInstance.blueprint;
|
|
Dictionary<string, Variant> savegameData = SavegameService.GetSaveData(InventoryInstance.ID);
|
|
if (savegameData.Count > 0)
|
|
{
|
|
foreach (var kvp in savegameData)
|
|
{
|
|
// if it's a unique item, then it can only exist once in the world (either as a pickup OR in the inventory)
|
|
if (itemInstance.blueprint.isUnique)
|
|
{
|
|
//comparing resource path to identify the item
|
|
string[] valuePair = kvp.Value.AsStringArray();
|
|
if (valuePair[0] == itemResource.ResourcePath)
|
|
{
|
|
int amountInInventory = int.Parse(valuePair[1]);
|
|
// comparing amount to see if it's all in the inventory now.
|
|
if (amountInInventory > 0)
|
|
{
|
|
Pickup();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|