WIP
This commit is contained in:
@@ -186,7 +186,7 @@ public partial class PlantBehaviour2D : Node2D
|
||||
|
||||
private void SetActiveHarvestablePlant(bool active)
|
||||
{
|
||||
_harvestablePlant.IsActive = active;
|
||||
//_harvestablePlant.IsActive = active;
|
||||
_harvestablePlant.UpdateVisuals();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
using Godot;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
// Do not instantiate this resource
|
||||
// But it has to be a resource because Godot
|
||||
[GlobalClass]
|
||||
public partial class ItemInstance: Resource
|
||||
public partial class ItemInstance : Resource, IJsonSerializable
|
||||
{
|
||||
[Export] public required ItemResource blueprint;
|
||||
[Export] public int amount = 1;
|
||||
@@ -17,4 +20,18 @@ public partial class ItemInstance: Resource
|
||||
amount = amount
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public void LoadFromJson(JObject json)
|
||||
{
|
||||
var blueprintPath = json.GetStringValue("blueprint");
|
||||
blueprint = GD.Load<ItemResource>(blueprintPath);
|
||||
amount = json.GetIntValue("amount");
|
||||
}
|
||||
|
||||
public JObject SaveToJson()
|
||||
{
|
||||
return new(
|
||||
new JProperty("blueprint", blueprint.ResourcePath),
|
||||
new JProperty("amount", amount));
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,19 @@
|
||||
using Babushka.scripts.CSharp.Common.Savegame;
|
||||
using Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class ItemOnGround2D : Node, ISaveable
|
||||
public partial class ItemOnGround2D : PositionalEntity
|
||||
{
|
||||
private ItemInstance _itemInstance;
|
||||
|
||||
[Export] public bool IsActive = true;
|
||||
[Export] private bool _infiniteSupply = false;
|
||||
[Export] private int _finiteSupply = 1;
|
||||
[Export] private bool _saveToDisk = true;
|
||||
|
||||
private int pickUpCounter = 0;
|
||||
|
||||
[Signal] public delegate void SuccessfulPickUpEventHandler();
|
||||
|
||||
|
||||
|
||||
private Label _itemLabel => GetNode<Label>("ItemLabel");
|
||||
private Label _pickupErrorLabel => GetNode<Label>("PickupErrorLabel");
|
||||
private Sprite2D _iconSprite => GetNode<Sprite2D>("Icon");
|
||||
@@ -34,16 +30,12 @@ public partial class ItemOnGround2D : Node, ISaveable
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
LoadFromSaveData();
|
||||
UpdateVisuals();
|
||||
_pickupErrorLabel.Text = "";
|
||||
}
|
||||
|
||||
public void TryPickUp()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
|
||||
var result = InventoryManager.Instance.CollectItem(itemInstance.Clone());
|
||||
EmitSignal(SignalName.SuccessfulPickUp);
|
||||
if (result == InventoryActionResult.Success)
|
||||
@@ -58,16 +50,7 @@ public partial class ItemOnGround2D : Node, ISaveable
|
||||
|
||||
private void Pickup()
|
||||
{
|
||||
if (!_infiniteSupply)
|
||||
{
|
||||
pickUpCounter++;
|
||||
if (pickUpCounter >= _finiteSupply)
|
||||
{
|
||||
QueueFree();
|
||||
}
|
||||
|
||||
UpdateSaveData();
|
||||
}
|
||||
// remove from entity manager
|
||||
}
|
||||
|
||||
private void FailToPickup()
|
||||
@@ -80,9 +63,6 @@ public partial class ItemOnGround2D : Node, ISaveable
|
||||
|
||||
public void UpdateVisuals()
|
||||
{
|
||||
if (!IsActive)
|
||||
return;
|
||||
|
||||
_iconSprite.Texture = itemInstance?.blueprint?.icon;
|
||||
if (_iconSprite.Texture == null)
|
||||
{
|
||||
@@ -94,7 +74,20 @@ public partial class ItemOnGround2D : Node, ISaveable
|
||||
}
|
||||
}
|
||||
|
||||
// todo: What do we do with instances that are created at runtime?
|
||||
protected override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
_itemInstance.LoadFromJson(json.GetObject("item"));
|
||||
}
|
||||
|
||||
protected override void SaveEntity(JObject json)
|
||||
{
|
||||
base.SaveEntity(json);
|
||||
json["item"] = _itemInstance.SaveToJson();
|
||||
}
|
||||
|
||||
// old save
|
||||
/*
|
||||
public void UpdateSaveData()
|
||||
{
|
||||
if (!_saveToDisk)
|
||||
@@ -157,5 +150,5 @@ public partial class ItemOnGround2D : Node, ISaveable
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Godot;
|
||||
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
using Godot;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
|
||||
@@ -18,11 +18,23 @@ public static class EntityLoadSaveUtil
|
||||
return json.Value<long>(key);
|
||||
}
|
||||
|
||||
public static int GetIntValue(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.Integer);
|
||||
return json.Value<int>(key);
|
||||
}
|
||||
|
||||
public static float GetFloatValue(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.Float);
|
||||
return json.Value<float>(key);
|
||||
}
|
||||
|
||||
public static JObject GetObject(this JObject json, string key)
|
||||
{
|
||||
AssertTokenType(json, key, JTokenType.Object);
|
||||
return json.Value<JObject>(key)!;
|
||||
}
|
||||
|
||||
public static string GetStringValue(this JObject json, string key)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||
|
||||
public interface IJsonSerializable
|
||||
{
|
||||
public void LoadFromJson(JObject json);
|
||||
public JObject SaveToJson();
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cuma3347l55mb
|
||||
Reference in New Issue
Block a user