✨ Implemented load check for unique inventory items
This commit is contained in:
@@ -30,7 +30,7 @@ stream_1/stream = ExtResource("7_edjam")
|
|||||||
stream_2/stream = ExtResource("8_kflfw")
|
stream_2/stream = ExtResource("8_kflfw")
|
||||||
stream_3/stream = ExtResource("9_dltn0")
|
stream_3/stream = ExtResource("9_dltn0")
|
||||||
|
|
||||||
[node name="GenericItemOnGround" type="Node2D"]
|
[node name="GenericItemOnGround" type="Node2D" groups=["Saveable"]]
|
||||||
z_index = 1
|
z_index = 1
|
||||||
y_sort_enabled = true
|
y_sort_enabled = true
|
||||||
script = ExtResource("1_tlhp6")
|
script = ExtResource("1_tlhp6")
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ name = "Hoe"
|
|||||||
color = Color(0.751421, 0.329615, 0.570911, 1)
|
color = Color(0.751421, 0.329615, 0.570911, 1)
|
||||||
icon = SubResource("AtlasTexture_i5wdx")
|
icon = SubResource("AtlasTexture_i5wdx")
|
||||||
maxStack = 1
|
maxStack = 1
|
||||||
|
isUnique = true
|
||||||
metadata/_custom_type_script = "uid://cbskymrxs6ksu"
|
metadata/_custom_type_script = "uid://cbskymrxs6ksu"
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ name = "Can"
|
|||||||
color = Color(0.336269, 0.489145, 0.825324, 1)
|
color = Color(0.336269, 0.489145, 0.825324, 1)
|
||||||
icon = SubResource("AtlasTexture_tqw18")
|
icon = SubResource("AtlasTexture_tqw18")
|
||||||
maxStack = 1
|
maxStack = 1
|
||||||
|
isUnique = true
|
||||||
metadata/_custom_type_script = "uid://cbskymrxs6ksu"
|
metadata/_custom_type_script = "uid://cbskymrxs6ksu"
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ public partial class InventoryInstance : Node, ISaveable
|
|||||||
[Signal]
|
[Signal]
|
||||||
public delegate void InventoryContentsChangedEventHandler();
|
public delegate void InventoryContentsChangedEventHandler();
|
||||||
|
|
||||||
private const string ID = "inventoryInstance";
|
public static string ID = "inventoryInstance";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The total amount of Inventoryslots in the inventory (empty and occupied).
|
/// 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;
|
||||||
|
using Godot.Collections;
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||||
|
|
||||||
public partial class ItemOnGround2D : Node
|
public partial class ItemOnGround2D : Node, ISaveable
|
||||||
{
|
{
|
||||||
private ItemInstance _itemInstance;
|
private ItemInstance _itemInstance;
|
||||||
|
|
||||||
@@ -30,6 +32,7 @@ public partial class ItemOnGround2D : Node
|
|||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
|
LoadFromSaveData();
|
||||||
UpdateVisuals();
|
UpdateVisuals();
|
||||||
_pickupErrorLabel.Text = "";
|
_pickupErrorLabel.Text = "";
|
||||||
}
|
}
|
||||||
@@ -42,6 +45,16 @@ public partial class ItemOnGround2D : Node
|
|||||||
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
|
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
|
||||||
EmitSignal(SignalName.SuccessfulPickUp);
|
EmitSignal(SignalName.SuccessfulPickUp);
|
||||||
if (result == InventoryActionResult.Success)
|
if (result == InventoryActionResult.Success)
|
||||||
|
{
|
||||||
|
Pickup();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
FailToPickup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Pickup()
|
||||||
{
|
{
|
||||||
if (!_infiniteSupply)
|
if (!_infiniteSupply)
|
||||||
{
|
{
|
||||||
@@ -52,14 +65,14 @@ public partial class ItemOnGround2D : Node
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
private void FailToPickup()
|
||||||
{
|
{
|
||||||
_pickupErrorLabel.Text = "Inventory Full";
|
_pickupErrorLabel.Text = "Inventory Full";
|
||||||
var tween = GetTree().CreateTween();
|
var tween = GetTree().CreateTween();
|
||||||
tween.TweenInterval(2);
|
tween.TweenInterval(2);
|
||||||
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
|
tween.TweenCallback(Callable.From(() => _pickupErrorLabel.Text = ""));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateVisuals()
|
public void UpdateVisuals()
|
||||||
{
|
{
|
||||||
@@ -76,4 +89,40 @@ public partial class ItemOnGround2D : Node
|
|||||||
_itemLabel.Text = "";
|
_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]
|
[Export]
|
||||||
public int maxStack;
|
public int maxStack;
|
||||||
|
|
||||||
|
[Export]
|
||||||
|
public bool isUnique;
|
||||||
|
|
||||||
public ItemResource()
|
public ItemResource()
|
||||||
{
|
{
|
||||||
name = "";
|
name = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user