✨When beetroot is in inventory the player can heal
This commit is contained in:
@@ -17,7 +17,7 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
|
||||
[Signal]
|
||||
public delegate void InventoryContentsChangedEventHandler();
|
||||
|
||||
|
||||
public static string ID = "inventoryInstance";
|
||||
|
||||
/// <summary>
|
||||
@@ -52,7 +52,7 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
SlotAmountChanged += UpdateSaveData;
|
||||
SavegameService.OnSaveGameReset += SaveGameReset;
|
||||
}
|
||||
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
InventoryContentsChanged -= UpdateSaveData;
|
||||
@@ -101,7 +101,8 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
return InventoryActionResult.DestinationFull;
|
||||
}
|
||||
|
||||
var itemInstance = _slots[slotIndex].itemInstance ?? new ItemInstance { blueprint = newItem.blueprint, amount = 0 };
|
||||
var itemInstance = _slots[slotIndex].itemInstance ??
|
||||
new ItemInstance { blueprint = newItem.blueprint, amount = 0 };
|
||||
var maxStack = itemInstance!.blueprint.maxStack;
|
||||
var freeOnStack = maxStack - itemInstance.amount;
|
||||
var moveAmount = Math.Min(freeOnStack, newItem.amount);
|
||||
@@ -130,12 +131,12 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
itemInstance = _slots[inventorySlot].itemInstance;
|
||||
if (itemInstance == null)
|
||||
return InventoryActionResult.SourceDoesNotExist;
|
||||
|
||||
|
||||
itemInstance.amount -= 1;
|
||||
|
||||
if(itemInstance.amount == 0)
|
||||
|
||||
if (itemInstance.amount == 0)
|
||||
_slots[inventorySlot].itemInstance = null;
|
||||
|
||||
|
||||
EmitSignal(SignalName.InventoryContentsChanged);
|
||||
return InventoryActionResult.Success;
|
||||
}
|
||||
@@ -145,6 +146,38 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
return RemoveItem(inventorySlot, out _);
|
||||
}
|
||||
|
||||
public InventoryActionResult TryRemoveAllItems(ItemInstance items)
|
||||
{
|
||||
var hasItemsCount = TotalItemsOfBlueprint(items.blueprint);
|
||||
if (hasItemsCount < items.amount)
|
||||
return InventoryActionResult.SourceDoesNotExist;
|
||||
|
||||
var amountToRemove = items.amount;
|
||||
foreach (var s in _slots)
|
||||
{
|
||||
if (s.IsEmpty() || s.itemInstance!.blueprint != items.blueprint)
|
||||
continue;
|
||||
|
||||
var slotItem = s.itemInstance!;
|
||||
if (slotItem.amount <= amountToRemove)
|
||||
{
|
||||
amountToRemove -= slotItem.amount;
|
||||
s.itemInstance = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
slotItem.amount -= amountToRemove;
|
||||
amountToRemove = 0;
|
||||
}
|
||||
|
||||
if (amountToRemove == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
EmitSignal(SignalName.InventoryContentsChanged);
|
||||
return InventoryActionResult.Success;
|
||||
}
|
||||
|
||||
public InventoryActionResult AddItemToSlot(ItemInstance itemInstance, int destinationSlot)
|
||||
{
|
||||
if (destinationSlot < 0 || destinationSlot >= _slots.Count)
|
||||
@@ -174,8 +207,8 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
{
|
||||
return items.All(HasItems);
|
||||
}
|
||||
|
||||
#region SAVE AND LOAD
|
||||
|
||||
#region SAVE AND LOAD
|
||||
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
@@ -189,17 +222,17 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
string[] value = new string[2];
|
||||
value[0] = _slots[i].itemInstance.blueprint.ResourcePath;
|
||||
value[1] = _slots[i].itemInstance.amount.ToString();
|
||||
payloadData.Add(key,value);
|
||||
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)
|
||||
@@ -210,15 +243,15 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
{
|
||||
string[] savePayload = inventoryItemData.AsStringArray();
|
||||
ItemResource resource = ResourceLoader.Load<ItemResource>(savePayload[0]);
|
||||
int _amount = int.Parse(savePayload[1]);
|
||||
|
||||
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.
|
||||
@@ -230,5 +263,6 @@ public partial class InventoryInstance : Node, ISaveable
|
||||
slot.itemInstance = null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
[GlobalClass]
|
||||
public partial class ItemInstance: Resource
|
||||
{
|
||||
[Export] public ItemResource blueprint;
|
||||
[Export] public required ItemResource blueprint;
|
||||
[Export] public int amount = 1;
|
||||
|
||||
public ItemInstance Clone()
|
||||
|
||||
Reference in New Issue
Block a user