Implemented load check for unique inventory items

pull/32/head
kziolkowski 2 months ago
parent 88f3c90eec
commit 70383fc16e

@ -30,7 +30,7 @@ stream_1/stream = ExtResource("7_edjam")
stream_2/stream = ExtResource("8_kflfw")
stream_3/stream = ExtResource("9_dltn0")
[node name="GenericItemOnGround" type="Node2D"]
[node name="GenericItemOnGround" type="Node2D" groups=["Saveable"]]
z_index = 1
y_sort_enabled = true
script = ExtResource("1_tlhp6")

@ -13,4 +13,5 @@ name = "Hoe"
color = Color(0.751421, 0.329615, 0.570911, 1)
icon = SubResource("AtlasTexture_i5wdx")
maxStack = 1
isUnique = true
metadata/_custom_type_script = "uid://cbskymrxs6ksu"

@ -13,4 +13,5 @@ name = "Can"
color = Color(0.336269, 0.489145, 0.825324, 1)
icon = SubResource("AtlasTexture_tqw18")
maxStack = 1
isUnique = true
metadata/_custom_type_script = "uid://cbskymrxs6ksu"

@ -18,7 +18,7 @@ public partial class InventoryInstance : Node, ISaveable
[Signal]
public delegate void InventoryContentsChangedEventHandler();
private const string ID = "inventoryInstance";
public static string ID = "inventoryInstance";
/// <summary>
/// The total amount of Inventoryslots in the inventory (empty and occupied).

@ -1,8 +1,10 @@
using Babushka.scripts.CSharp.Common.Savegame;
using Godot;
using Godot.Collections;
namespace Babushka.scripts.CSharp.Common.Inventory;
public partial class ItemOnGround2D : Node
public partial class ItemOnGround2D : Node, ISaveable
{
private ItemInstance _itemInstance;
@ -30,6 +32,7 @@ public partial class ItemOnGround2D : Node
public override void _Ready()
{
LoadFromSaveData();
UpdateVisuals();
_pickupErrorLabel.Text = "";
}
@ -43,24 +46,34 @@ public partial class ItemOnGround2D : Node
EmitSignal(SignalName.SuccessfulPickUp);
if (result == InventoryActionResult.Success)
{
if (!_infiniteSupply)
{
pickUpCounter++;
if (pickUpCounter >= _finiteSupply)
{
QueueFree();
}
}
Pickup();
}
else
{
_pickupErrorLabel.Text = "Inventory Full";
var tween = GetTree().CreateTween();
tween.TweenInterval(2);
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
FailToPickup();
}
}
private void Pickup()
{
if (!_infiniteSupply)
{
pickUpCounter++;
if (pickUpCounter >= _finiteSupply)
{
QueueFree();
}
}
}
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)
@ -76,4 +89,40 @@ public partial class ItemOnGround2D : Node
_itemLabel.Text = "";
}
}
public void UpdateSaveData()
{
// do nothing?
}
public void LoadFromSaveData()
{
if (_infiniteSupply)
return;
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();
}
}
}
}
}
}
}

@ -17,6 +17,9 @@ public partial class ItemResource : Resource
[Export]
public int maxStack;
[Export]
public bool isUnique;
public ItemResource()
{
name = "";

Loading…
Cancel
Save