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.
Babushka/scripts/CSharp/Common/Inventory/ItemOnGround2D.cs

157 lines
4.3 KiB

using Babushka.scripts.CSharp.Common.Savegame;
using Babushka.scripts.CSharp.GameEntity.Entities;
using Babushka.scripts.CSharp.GameEntity.LoadSave;
using Godot;
using Godot.Collections;
using Newtonsoft.Json.Linq;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class ItemOnGround2D : Node2D
{
private ItemInstance _itemInstance;
[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()
{
UpdateVisuals();
_pickupErrorLabel.Text = "";
}
public void TryPickUp()
{
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
EmitSignal(SignalName.SuccessfulPickUp);
if (result == InventoryActionResult.Success)
{
Pickup();
}
else
{
FailToPickup();
}
}
private void Pickup()
{
// remove from entity manager
}
private void FailToPickup()
{
_pickupErrorLabel.Text = "Inventory Full";
var tween = GetTree().CreateTween();
tween.TweenInterval(2);
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
}
public void UpdateVisuals()
{
_iconSprite.Texture = itemInstance?.blueprint?.icon;
if (_iconSprite.Texture == null)
{
_itemLabel.Text = itemInstance?.blueprint?.name ?? "Error Item";
}
else
{
_itemLabel.Text = "";
}
}
/*
protected override void LoadEntity(JObject json)
{
base.LoadEntity(json);
_itemInstance.LoadFromJson(json.GetObject("item"));
}
protected override void SaveEntity(JObject json)
{
base.SaveEntity(json);
json["item"] = _itemInstance.SaveToJson();
}
*/
// old save
/*
public void UpdateSaveData()
{
if (!_saveToDisk)
return;
var payloadData = new Dictionary<string, Variant>
{
{"pickupCounter", pickUpCounter}
};
string id = GetMeta("SaveID").AsString();
SavegameService.AppendDataToSave( id, payloadData);
}
public void LoadFromSaveData()
{
if (!_saveToDisk)
return;
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();
}
}
}
}
}
}*/
}