|
|
|
@ -7,21 +7,16 @@ using Babushka.scripts.CSharp.Common.Savegame;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class InventoryInstance : Node, ISaveable
|
|
|
|
public partial class InventoryInstance
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private List<InventorySlot> _slots = new();
|
|
|
|
private readonly List<InventorySlot> _slots;
|
|
|
|
public IReadOnlyList<InventorySlot> Slots => _slots;
|
|
|
|
public IReadOnlyList<InventorySlot> Slots => _slots;
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
public event Action? SlotAmountChanged;
|
|
|
|
public delegate void SlotAmountChangedEventHandler();
|
|
|
|
public event Action? InventoryContentsChanged;
|
|
|
|
|
|
|
|
|
|
|
|
[Signal]
|
|
|
|
|
|
|
|
public delegate void InventoryContentsChangedEventHandler();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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).
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
[Export]
|
|
|
|
[Export]
|
|
|
|
public int SlotAmount
|
|
|
|
public int SlotAmount
|
|
|
|
@ -41,29 +36,20 @@ public partial class InventoryInstance : Node, ISaveable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignal(SignalName.SlotAmountChanged);
|
|
|
|
SlotAmountChanged?.Invoke();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void _EnterTree()
|
|
|
|
public InventoryInstance(int slotCount)
|
|
|
|
{
|
|
|
|
|
|
|
|
LoadFromSaveData();
|
|
|
|
|
|
|
|
InventoryContentsChanged += UpdateSaveData;
|
|
|
|
|
|
|
|
SlotAmountChanged += UpdateSaveData;
|
|
|
|
|
|
|
|
SavegameService.OnSaveGameReset += SaveGameReset;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void _ExitTree()
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
InventoryContentsChanged -= UpdateSaveData;
|
|
|
|
_slots = new();
|
|
|
|
SlotAmountChanged -= UpdateSaveData;
|
|
|
|
SlotAmount = slotCount;
|
|
|
|
SavegameService.OnSaveGameReset -= SaveGameReset;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public InventoryActionResult AddItem(ItemInstance newItem)
|
|
|
|
public InventoryActionResult AddItem(ItemInstance newItem)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var result = AddItemAndStackRecursive(newItem, 0);
|
|
|
|
var result = AddItemAndStackRecursive(newItem, 0);
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
InventoryContentsChanged?.Invoke();
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -137,7 +123,7 @@ public partial class InventoryInstance : Node, ISaveable
|
|
|
|
if (itemInstance.amount == 0)
|
|
|
|
if (itemInstance.amount == 0)
|
|
|
|
_slots[inventorySlot].itemInstance = null;
|
|
|
|
_slots[inventorySlot].itemInstance = null;
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
InventoryContentsChanged?.Invoke();
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -174,7 +160,7 @@ public partial class InventoryInstance : Node, ISaveable
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
InventoryContentsChanged?.Invoke();
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -187,7 +173,7 @@ public partial class InventoryInstance : Node, ISaveable
|
|
|
|
return InventoryActionResult.DestinationFull;
|
|
|
|
return InventoryActionResult.DestinationFull;
|
|
|
|
|
|
|
|
|
|
|
|
_slots[destinationSlot].itemInstance = itemInstance;
|
|
|
|
_slots[destinationSlot].itemInstance = itemInstance;
|
|
|
|
EmitSignal(SignalName.InventoryContentsChanged);
|
|
|
|
InventoryContentsChanged?.Invoke();
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
return InventoryActionResult.Success;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -207,62 +193,4 @@ public partial class InventoryInstance : Node, ISaveable
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return items.All(HasItems);
|
|
|
|
return items.All(HasItems);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region SAVE AND LOAD
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void UpdateSaveData()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var payloadData = new Godot.Collections.Dictionary<string, Variant>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _slots.Count; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!_slots[i].IsEmpty())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string key = i.ToString();
|
|
|
|
|
|
|
|
string[] value = new string[2];
|
|
|
|
|
|
|
|
value[0] = _slots[i].itemInstance.blueprint.ResourcePath;
|
|
|
|
|
|
|
|
value[1] = _slots[i].itemInstance.amount.ToString();
|
|
|
|
|
|
|
|
payloadData.Add(key, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SavegameService.AppendDataToSave(ID, payloadData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void LoadFromSaveData()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var id = ID;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Godot.Collections.Dictionary<string, Variant> save = SavegameService.GetSaveData(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (save.Count > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < _slots.Count; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (save.TryGetValue(i.ToString(), out Variant inventoryItemData))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
string[] savePayload = inventoryItemData.AsStringArray();
|
|
|
|
|
|
|
|
ItemResource resource = ResourceLoader.Load<ItemResource>(savePayload[0]);
|
|
|
|
|
|
|
|
int _amount = int.Parse(savePayload[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ItemInstance instance = new ItemInstance { blueprint = resource, amount = _amount };
|
|
|
|
|
|
|
|
AddItem(instance);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Called when a new save is created.
|
|
|
|
|
|
|
|
/// Needs to do a runtime check because the InventoryInstance is already in existence at the beginning of the first scene.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private void SaveGameReset()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
foreach (var slot in _slots)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
slot.itemInstance = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|