Implemented Save and Load functionality
This commit is contained in:
@@ -14,13 +14,13 @@ public class Entity
|
||||
id = new Random().NextInt64();
|
||||
}
|
||||
|
||||
protected virtual void SaveEntity(JObject json)
|
||||
public virtual void SaveEntity(JObject json)
|
||||
{
|
||||
json["id"] = id;
|
||||
json["type"] = EntityType;
|
||||
}
|
||||
|
||||
protected virtual void LoadEntity(JObject json)
|
||||
public virtual void LoadEntity(JObject json)
|
||||
{
|
||||
id = json.GetLongValue("id");
|
||||
}
|
||||
|
||||
@@ -1,13 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Transactions;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public class LoadedScenesEntity : Entity
|
||||
{
|
||||
private HashSet<string> _loadedScenes = new();
|
||||
public override string EntityType => "LoadedScenesEntity";
|
||||
public override string EntityType => OWN_TYPE_NAME;
|
||||
public const string OWN_TYPE_NAME = "LoadedScenesEntity";
|
||||
|
||||
public void AddScene(string sceneName) => _loadedScenes.Add(sceneName);
|
||||
|
||||
public bool WasSceneLoaded(string sceneName) => _loadedScenes.Contains(sceneName);
|
||||
|
||||
public override void SaveEntity(JObject json)
|
||||
{
|
||||
base.SaveEntity(json);
|
||||
json["scenes"] = new JArray(_loadedScenes);
|
||||
}
|
||||
|
||||
public override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
JArray array = (JArray?) json["scenes"] ?? throw new Exception("No scenes found in LoadedScenesEntity.");
|
||||
|
||||
_loadedScenes = array.ToObject<HashSet<string>>()!;
|
||||
}
|
||||
}
|
||||
@@ -6,22 +6,21 @@ namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public abstract class PositionalEntity : Entity
|
||||
{
|
||||
private Node2D _positionalNodeRef;
|
||||
public Vector2 position;
|
||||
public string sceneName = "none";
|
||||
|
||||
protected override void SaveEntity(JObject json)
|
||||
public override void SaveEntity(JObject json)
|
||||
{
|
||||
base.SaveEntity(json);
|
||||
json["posx"] = _positionalNodeRef.Position.X;
|
||||
json["posy"] = _positionalNodeRef.Position.Y;
|
||||
json["posx"] = position.X;
|
||||
json["posy"] = position.Y;
|
||||
json["scene"] = sceneName;
|
||||
}
|
||||
|
||||
protected override void LoadEntity(JObject json)
|
||||
public override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
_positionalNodeRef.Position = new Vector2(
|
||||
position = new Vector2(
|
||||
json.GetFloatValue("posx"),
|
||||
json.GetFloatValue("posy"));
|
||||
sceneName = json.GetStringValue("scene");
|
||||
|
||||
Reference in New Issue
Block a user