Made Player inventory saveable
This commit is contained in:
@@ -4,10 +4,12 @@ using Godot;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Babushka.scripts.CSharp.Common.Savegame;
|
using Babushka.scripts.CSharp.Common.Savegame;
|
||||||
|
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||||
|
|
||||||
public partial class InventoryInstance
|
public partial class InventoryInstance : IJsonSerializable
|
||||||
{
|
{
|
||||||
private readonly List<InventorySlot> _slots;
|
private readonly List<InventorySlot> _slots;
|
||||||
public IReadOnlyList<InventorySlot> Slots => _slots;
|
public IReadOnlyList<InventorySlot> Slots => _slots;
|
||||||
@@ -193,4 +195,27 @@ public partial class InventoryInstance
|
|||||||
{
|
{
|
||||||
return items.All(HasItems);
|
return items.All(HasItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void LoadFromJson(JObject json)
|
||||||
|
{
|
||||||
|
var itemsArray = (JArray?)json["items"];
|
||||||
|
if (itemsArray == null) return;
|
||||||
|
|
||||||
|
foreach (var (itemToken, slot) in itemsArray.Zip(_slots))
|
||||||
|
{
|
||||||
|
var itemObj = (JObject?)itemToken;
|
||||||
|
if (itemObj == null) continue;
|
||||||
|
slot.LoadFromJson(itemObj);
|
||||||
|
}
|
||||||
|
|
||||||
|
InventoryContentsChanged?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JObject SaveToJson()
|
||||||
|
{
|
||||||
|
return new JObject
|
||||||
|
{
|
||||||
|
["items"] = new JArray(_slots.Select(s => s.SaveToJson()))
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
|
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a virtual object wrapper for an item instance.
|
/// Represents a virtual object wrapper for an item instance.
|
||||||
/// Can return the containing item or null.
|
/// Can return the containing item or null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class InventorySlot
|
public class InventorySlot: IJsonSerializable
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The inventory item instance that may or may not be bound to this slot.
|
/// The inventory item instance that may or may not be bound to this slot.
|
||||||
@@ -20,5 +23,27 @@ public class InventorySlot
|
|||||||
{
|
{
|
||||||
return itemInstance == null;
|
return itemInstance == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void LoadFromJson(JObject json)
|
||||||
|
{
|
||||||
|
var itemJson = json.Value<JObject>("item");
|
||||||
|
if (itemJson != null)
|
||||||
|
{
|
||||||
|
itemInstance = new ItemInstance();
|
||||||
|
itemInstance.LoadFromJson(itemJson);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
itemInstance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public JObject SaveToJson()
|
||||||
|
{
|
||||||
|
return new JObject()
|
||||||
|
{
|
||||||
|
["item"] = itemInstance?.SaveToJson()
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Babushka.scripts.CSharp.Common.Inventory;
|
|||||||
[GlobalClass]
|
[GlobalClass]
|
||||||
public partial class ItemInstance : Resource, IJsonSerializable
|
public partial class ItemInstance : Resource, IJsonSerializable
|
||||||
{
|
{
|
||||||
[Export] public required ItemResource blueprint;
|
[Export] public ItemResource blueprint;
|
||||||
[Export] public int amount = 1;
|
[Export] public int amount = 1;
|
||||||
|
|
||||||
public ItemInstance Clone()
|
public ItemInstance Clone()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ using Babushka.scripts.CSharp.Common.Farming;
|
|||||||
using Babushka.scripts.CSharp.Common.Inventory;
|
using Babushka.scripts.CSharp.Common.Inventory;
|
||||||
using Babushka.scripts.CSharp.GameEntity.Management;
|
using Babushka.scripts.CSharp.GameEntity.Management;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||||
|
|
||||||
@@ -33,4 +34,18 @@ public class VesnaEntity : PositionalEntity
|
|||||||
node.player2d.Initialize(this);
|
node.player2d.Initialize(this);
|
||||||
parent.AddChild(node);
|
parent.AddChild(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void SaveEntity(JObject json)
|
||||||
|
{
|
||||||
|
base.SaveEntity(json);
|
||||||
|
json["slot"] = CurrentSelectedSlotIndex;
|
||||||
|
json["inventory"] = inventory.SaveToJson();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void LoadEntity(JObject json)
|
||||||
|
{
|
||||||
|
base.LoadEntity(json);
|
||||||
|
CurrentSelectedSlotIndex = json.Value<int>("slot");
|
||||||
|
inventory.LoadFromJson(json.Value<JObject>("inventory")!);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user