Implemented Save and Load functionality
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
using Godot;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Entity = Babushka.scripts.CSharp.GameEntity.Entities.Entity;
|
||||
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||
|
||||
@@ -15,6 +18,7 @@ public partial class EntityManager : Node
|
||||
public static EntityManager Instance;
|
||||
|
||||
[Export] private EntityNodeCreator _nodeCreator = null!;
|
||||
[Export] private string saveDirectory = "user://save_data/";
|
||||
|
||||
private EntitySceneContainer? _currentEntitySceneContainer;
|
||||
private readonly List<Entity> _allEntities = new();
|
||||
@@ -27,6 +31,7 @@ public partial class EntityManager : Node
|
||||
public override void _EnterTree()
|
||||
{
|
||||
Instance = this;
|
||||
Load();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
@@ -40,6 +45,58 @@ public partial class EntityManager : Node
|
||||
GD.Print(entity.EntityType + " " + entity.id);
|
||||
}
|
||||
}
|
||||
|
||||
if(@event.IsActionPressed("SaveGame")) Save();
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
JArray array = new JArray();
|
||||
foreach (var entity in AllEntities)
|
||||
{
|
||||
JObject saveData = new JObject();
|
||||
entity.SaveEntity(saveData);
|
||||
array.Add(saveData);
|
||||
}
|
||||
|
||||
using var SaveFile = FileAccess.Open(saveDirectory + "save.json", FileAccess.ModeFlags.Write);
|
||||
SaveFile.StoreString(array.ToString());
|
||||
}
|
||||
|
||||
public void Load()
|
||||
{
|
||||
using var saveFile = FileAccess.Open(saveDirectory + "save.json", FileAccess.ModeFlags.Read);
|
||||
if (saveFile == null) return;
|
||||
|
||||
JArray array = JArray.Parse(saveFile.GetAsText());
|
||||
|
||||
foreach (var token in array)
|
||||
{
|
||||
var jobj = (JObject)token;
|
||||
|
||||
if (jobj == null) continue;
|
||||
|
||||
if (jobj.TryGetValue("type", out var entityType))
|
||||
{
|
||||
string entityTypeString = (string) entityType!;
|
||||
Entity entity = InitializeEntity(entityTypeString);
|
||||
entity.LoadEntity(jobj);
|
||||
AddEntity(entity);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Entity InitializeEntity(string type)
|
||||
{
|
||||
Entity entity = type switch
|
||||
{
|
||||
TrashEntity.OWN_TYPE_NAME => new TrashEntity(),
|
||||
LoadedScenesEntity.OWN_TYPE_NAME => new LoadedScenesEntity(),
|
||||
_ => throw new Exception($"Trying to load unknown entity type: {type}")
|
||||
};
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
#region ENTITY MANAGEMENT
|
||||
|
||||
@@ -16,14 +16,20 @@ public partial class EntitySceneContainer : Node2D
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
EntityManager.Instance.Save();
|
||||
EntityManager.Instance.UnsetSceneContainer();
|
||||
var loadedScenesEntity = EntityManager.Instance.GetUniqueEntity<LoadedScenesEntity>();
|
||||
loadedScenesEntity.AddScene(sceneName);
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
AddAllEntities();
|
||||
CallDeferred(nameof(RegisterWithScenesEntity));
|
||||
}
|
||||
|
||||
private void RegisterWithScenesEntity()
|
||||
{
|
||||
var loadedScenesEntity = EntityManager.Instance.GetUniqueEntity<LoadedScenesEntity>();
|
||||
loadedScenesEntity.AddScene(sceneName);
|
||||
}
|
||||
|
||||
public void AddAllEntities()
|
||||
|
||||
Reference in New Issue
Block a user