From 70383fc16ead8a9006172e87f56f2235979e7496 Mon Sep 17 00:00:00 2001 From: kziolkowski Date: Tue, 25 Nov 2025 17:00:50 +0100 Subject: [PATCH] :sparkles: Implemented load check for unique inventory items --- .../generic_item_on_ground_2d.tscn | 2 +- resources/items/rake.tres | 1 + resources/items/wateringcan.tres | 1 + .../Common/Inventory/InventoryInstance.cs | 2 +- .../CSharp/Common/Inventory/ItemOnGround2D.cs | 75 +++++++++++++++---- .../CSharp/Common/Inventory/ItemResource.cs | 3 + 6 files changed, 69 insertions(+), 15 deletions(-) diff --git a/prefabs/interactions/generic_item_on_ground_2d.tscn b/prefabs/interactions/generic_item_on_ground_2d.tscn index 17b25e2..fedd814 100644 --- a/prefabs/interactions/generic_item_on_ground_2d.tscn +++ b/prefabs/interactions/generic_item_on_ground_2d.tscn @@ -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") diff --git a/resources/items/rake.tres b/resources/items/rake.tres index 4df7eb0..d6959db 100644 --- a/resources/items/rake.tres +++ b/resources/items/rake.tres @@ -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" diff --git a/resources/items/wateringcan.tres b/resources/items/wateringcan.tres index d92cf6d..4159edf 100644 --- a/resources/items/wateringcan.tres +++ b/resources/items/wateringcan.tres @@ -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" diff --git a/scripts/CSharp/Common/Inventory/InventoryInstance.cs b/scripts/CSharp/Common/Inventory/InventoryInstance.cs index 498b297..a05f778 100644 --- a/scripts/CSharp/Common/Inventory/InventoryInstance.cs +++ b/scripts/CSharp/Common/Inventory/InventoryInstance.cs @@ -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"; /// /// The total amount of Inventoryslots in the inventory (empty and occupied). diff --git a/scripts/CSharp/Common/Inventory/ItemOnGround2D.cs b/scripts/CSharp/Common/Inventory/ItemOnGround2D.cs index b313dae..2bbed06 100644 --- a/scripts/CSharp/Common/Inventory/ItemOnGround2D.cs +++ b/scripts/CSharp/Common/Inventory/ItemOnGround2D.cs @@ -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 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(); + } + } + } + + } + } + } } diff --git a/scripts/CSharp/Common/Inventory/ItemResource.cs b/scripts/CSharp/Common/Inventory/ItemResource.cs index fd1b427..01d6ae2 100644 --- a/scripts/CSharp/Common/Inventory/ItemResource.cs +++ b/scripts/CSharp/Common/Inventory/ItemResource.cs @@ -17,6 +17,9 @@ public partial class ItemResource : Resource [Export] public int maxStack; + [Export] + public bool isUnique; + public ItemResource() { name = "";