Added basic entity scripts
This commit is contained in:
@@ -8,4 +8,7 @@
|
|||||||
<Folder Include="prefabs\UI\Inventory\" />
|
<Folder Include="prefabs\UI\Inventory\" />
|
||||||
<Folder Include="scripts\CSharp\Low Code\Randomizer\" />
|
<Folder Include="scripts\CSharp\Low Code\Randomizer\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||||
|
using Godot;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||||
|
|
||||||
|
public partial class Entity : Node2D
|
||||||
|
{
|
||||||
|
private long _id;
|
||||||
|
protected virtual EntityType Type => EntityType.None;
|
||||||
|
|
||||||
|
protected virtual void SaveEntity(JObject json)
|
||||||
|
{
|
||||||
|
json["id"] = _id;
|
||||||
|
json["type"] = (int)Type;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void LoadEntity(JObject json)
|
||||||
|
{
|
||||||
|
_id = json.GetLongValue("id");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://hnmpt23ovfgl
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
using Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
using Godot;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||||
|
|
||||||
|
public partial class PositionalEntity : Entity
|
||||||
|
{
|
||||||
|
public string sceneName = "none";
|
||||||
|
|
||||||
|
protected override void SaveEntity(JObject json)
|
||||||
|
{
|
||||||
|
base.SaveEntity(json);
|
||||||
|
json["posx"] = Position.X;
|
||||||
|
json["posy"] = Position.Y;
|
||||||
|
json["scene"] = sceneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void LoadEntity(JObject json)
|
||||||
|
{
|
||||||
|
base.LoadEntity(json);
|
||||||
|
Position = new Vector2(
|
||||||
|
json.GetFloatValue("posx"),
|
||||||
|
json.GetFloatValue("posy"));
|
||||||
|
sceneName = json.GetStringValue("scene");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://bs38dulqv7sop
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
|
||||||
|
public static class EntityLoadSaveUtil
|
||||||
|
{
|
||||||
|
private static void AssertTokenType(this JObject json, string key, JTokenType type)
|
||||||
|
{
|
||||||
|
var token = json[key];
|
||||||
|
if (token == null) throw new MalformedJsonException(json, key, "does not exist");
|
||||||
|
if (!token.HasValues) throw new MalformedJsonException(json, key, "has no value");
|
||||||
|
if (token.Type != type) throw new MalformedJsonException(json, key, $"is not of type {type}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long GetLongValue(this JObject json, string key)
|
||||||
|
{
|
||||||
|
AssertTokenType(json, key, JTokenType.Integer);
|
||||||
|
return json.Value<long>(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float GetFloatValue(this JObject json, string key)
|
||||||
|
{
|
||||||
|
AssertTokenType(json, key, JTokenType.Float);
|
||||||
|
return json.Value<float>(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetStringValue(this JObject json, string key)
|
||||||
|
{
|
||||||
|
AssertTokenType(json, key, JTokenType.String);
|
||||||
|
return json.Value<string>(key)!;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://ccu6p418viliu
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.LoadSave;
|
||||||
|
|
||||||
|
public class MalformedJsonException(JObject actualJson, string key, string problem) : Exception
|
||||||
|
{
|
||||||
|
public override string Message => $"JsonObject was malformed: {key} {problem}";
|
||||||
|
public override IDictionary Data => new Dictionary<string, JObject> { { "json", actualJson } };
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://d1o066hh84ow
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||||
|
using Godot;
|
||||||
|
using Entity = Babushka.scripts.CSharp.GameEntity.Entities.Entity;
|
||||||
|
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||||
|
|
||||||
|
public partial class EntityManager : Node
|
||||||
|
{
|
||||||
|
[Export] private EntityNodeCreator _nodeCreator = null!;
|
||||||
|
|
||||||
|
private EntitySceneManager? _currentEntitySceneManager;
|
||||||
|
|
||||||
|
|
||||||
|
private readonly List<Entity> _allEntities = new();
|
||||||
|
|
||||||
|
public IEnumerable<Entity> AllEntities => _allEntities;
|
||||||
|
|
||||||
|
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
|
||||||
|
|
||||||
|
public T NewPositionalEntity<T>(EntityType type, Vector2 position, string? scene = null) where T : PositionalEntity
|
||||||
|
{
|
||||||
|
if (scene == null)
|
||||||
|
{
|
||||||
|
if (_currentEntitySceneManager == null)
|
||||||
|
throw new Exception("No current scene. Specify scene to spawn an entity");
|
||||||
|
|
||||||
|
scene = _currentEntitySceneManager.sceneName;
|
||||||
|
}
|
||||||
|
|
||||||
|
var newEntity = _nodeCreator.Create<T>(type);
|
||||||
|
newEntity.Position = position;
|
||||||
|
newEntity.sceneName = scene;
|
||||||
|
_allEntities.Add(newEntity);
|
||||||
|
|
||||||
|
_currentEntitySceneManager.AddIfNeeded(newEntity);
|
||||||
|
|
||||||
|
return newEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnloadScene()
|
||||||
|
{
|
||||||
|
if (_currentEntitySceneManager == null) return;
|
||||||
|
_currentEntitySceneManager.RemoveAllEntities();
|
||||||
|
_currentEntitySceneManager = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LoadScene(EntitySceneManager sceneManager)
|
||||||
|
{
|
||||||
|
_currentEntitySceneManager = sceneManager;
|
||||||
|
foreach (var entity in AllPositionalEntities)
|
||||||
|
{
|
||||||
|
_currentEntitySceneManager.AddIfNeeded(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://umop2b1m1qm8
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||||
|
|
||||||
|
public static class EntityManagerUtil
|
||||||
|
{
|
||||||
|
public static void AddIfNeeded(this EntitySceneManager? self, PositionalEntity entity)
|
||||||
|
{
|
||||||
|
if(self == null) return;
|
||||||
|
if(self.sceneName != entity.sceneName) return;
|
||||||
|
|
||||||
|
self.AddEntity(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://dc3283h7sx4cl
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||||
|
using Godot;
|
||||||
|
using Godot.Collections;
|
||||||
|
using Entity = Babushka.scripts.CSharp.GameEntity.Entities.Entity;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||||
|
|
||||||
|
public partial class EntityNodeCreator : Node
|
||||||
|
{
|
||||||
|
[Export] private Dictionary<EntityType, PackedScene> _entityPrefabs;
|
||||||
|
|
||||||
|
public T Create<T>(EntityType type) where T:Entity
|
||||||
|
{
|
||||||
|
return _entityPrefabs[type].Instantiate<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://bogqp274y1pgr
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using Godot;
|
||||||
|
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||||
|
|
||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||||
|
|
||||||
|
public partial class EntitySceneManager : Node2D
|
||||||
|
{
|
||||||
|
[Export] public string sceneName = "none";
|
||||||
|
|
||||||
|
public void AddEntity(PositionalEntity entity)
|
||||||
|
{
|
||||||
|
AddChild(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveAllEntities()
|
||||||
|
{
|
||||||
|
foreach (var entity in GetChildren())
|
||||||
|
{
|
||||||
|
if (entity is PositionalEntity positionalEntity)
|
||||||
|
{
|
||||||
|
RemoveChild(entity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://ca1pg6k3gn47y
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Babushka.scripts.CSharp.GameEntity.Types;
|
||||||
|
|
||||||
|
public enum EntityType
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Yeli = 1,
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
uid://cjygyr4lc224m
|
||||||
Reference in New Issue
Block a user