First adjustments to the Entity System to make it work with different types
This commit is contained in:
@@ -7,7 +7,7 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.Common.Inventory;
|
||||
|
||||
public partial class ItemOnGround2D : PositionalEntity
|
||||
public partial class ItemOnGround2D : Node2D
|
||||
{
|
||||
private ItemInstance _itemInstance;
|
||||
|
||||
@@ -74,6 +74,7 @@ public partial class ItemOnGround2D : PositionalEntity
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
protected override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
@@ -85,6 +86,7 @@ public partial class ItemOnGround2D : PositionalEntity
|
||||
base.SaveEntity(json);
|
||||
json["item"] = _itemInstance.SaveToJson();
|
||||
}
|
||||
*/
|
||||
|
||||
// old save
|
||||
/*
|
||||
|
||||
@@ -1,23 +1,27 @@
|
||||
using System;
|
||||
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
|
||||
public class Entity
|
||||
{
|
||||
private long _id;
|
||||
protected virtual EntityType Type => EntityType.None;
|
||||
public long id;
|
||||
public virtual string EntityType => "";
|
||||
|
||||
public Entity()
|
||||
{
|
||||
id = new Random().NextInt64();
|
||||
}
|
||||
|
||||
protected virtual void SaveEntity(JObject json)
|
||||
{
|
||||
json["id"] = _id;
|
||||
json["type"] = (int)Type;
|
||||
json["id"] = id;
|
||||
json["type"] = EntityType;
|
||||
}
|
||||
|
||||
protected virtual void LoadEntity(JObject json)
|
||||
{
|
||||
_id = json.GetLongValue("id");
|
||||
id = json.GetLongValue("id");
|
||||
}
|
||||
}
|
||||
@@ -4,24 +4,30 @@ using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public partial class PositionalEntity : Entity
|
||||
public abstract class PositionalEntity : Entity
|
||||
{
|
||||
private Node2D _positionalNodeRef;
|
||||
public Vector2 position;
|
||||
public string sceneName = "none";
|
||||
|
||||
protected override void SaveEntity(JObject json)
|
||||
{
|
||||
base.SaveEntity(json);
|
||||
json["posx"] = Position.X;
|
||||
json["posy"] = Position.Y;
|
||||
json["posx"] = _positionalNodeRef.Position.X;
|
||||
json["posy"] = _positionalNodeRef.Position.Y;
|
||||
json["scene"] = sceneName;
|
||||
}
|
||||
|
||||
protected override void LoadEntity(JObject json)
|
||||
{
|
||||
base.LoadEntity(json);
|
||||
Position = new Vector2(
|
||||
_positionalNodeRef.Position = new Vector2(
|
||||
json.GetFloatValue("posx"),
|
||||
json.GetFloatValue("posy"));
|
||||
sceneName = json.GetStringValue("scene");
|
||||
}
|
||||
|
||||
// Deals with Instantiation of the node
|
||||
public abstract void AddEntity(Node2D parent);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Management;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
|
||||
public class TrashEntity : PositionalEntity
|
||||
{
|
||||
public override string EntityType => OWN_TYPE_NAME;
|
||||
public const string OWN_TYPE_NAME = "TrashEntity";
|
||||
private EntityNodeCreator _creator;
|
||||
|
||||
public TrashEntity()
|
||||
{
|
||||
}
|
||||
|
||||
public override void AddEntity(Node2D parent)
|
||||
{
|
||||
if(_creator == null) _creator = EntityManager.Instance.NodeCreator;
|
||||
var entityNode = _creator.CreateNode2D(EntityType);
|
||||
parent.AddChild(entityNode);
|
||||
entityNode.GlobalPosition = position;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://c31k34epunk5t
|
||||
@@ -0,0 +1,16 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.EntityNodes;
|
||||
|
||||
public partial class TrashEntityNode : Node2D
|
||||
{
|
||||
private TrashEntity _trashEntity;
|
||||
|
||||
public void Initialize(TrashEntity trashEntity)
|
||||
{
|
||||
_trashEntity = trashEntity;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://d3n8kwva4pxx5
|
||||
@@ -0,0 +1,18 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Entities;
|
||||
using Babushka.scripts.CSharp.GameEntity.Management;
|
||||
using Godot;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.EntityPlacer;
|
||||
|
||||
public partial class TrashEntityPlacer : Node2D
|
||||
{
|
||||
private string _trashEntityType = TrashEntity.OWN_TYPE_NAME;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
TrashEntity entity = new TrashEntity();
|
||||
entity.sceneName = EntityManager.Instance.CurrentEntitySceneContainer!.sceneName;
|
||||
entity.position = GlobalPosition;
|
||||
EntityManager.Instance.AddEntity(entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bilg7e33usxuv
|
||||
@@ -1,7 +1,6 @@
|
||||
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;
|
||||
@@ -11,9 +10,12 @@ namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||
public partial class EntityManager : Node
|
||||
{
|
||||
[Export] private EntityNodeCreator _nodeCreator = null!;
|
||||
|
||||
public EntityNodeCreator NodeCreator => _nodeCreator;
|
||||
|
||||
private EntitySceneManager? _currentEntitySceneManager;
|
||||
|
||||
private EntitySceneContainer? _currentEntitySceneContainer;
|
||||
|
||||
public static EntityManager Instance;
|
||||
|
||||
private readonly List<Entity> _allEntities = new();
|
||||
|
||||
@@ -21,39 +23,61 @@ public partial class EntityManager : Node
|
||||
|
||||
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
|
||||
|
||||
public T NewPositionalEntity<T>(EntityType type, Vector2 position, string? scene = null) where T : PositionalEntity
|
||||
public override void _EnterTree()
|
||||
{
|
||||
if (scene == null)
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event.IsActionPressed("DebugEntities"))
|
||||
{
|
||||
if (_currentEntitySceneManager == null)
|
||||
throw new Exception("No current scene. Specify scene to spawn an entity");
|
||||
|
||||
scene = _currentEntitySceneManager.sceneName;
|
||||
GD.Print("Entities:");
|
||||
foreach (var entity in AllEntities)
|
||||
{
|
||||
GD.Print(entity.EntityType + " " + entity.id);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
if (_currentEntitySceneContainer == null) return;
|
||||
_currentEntitySceneContainer = null;
|
||||
}
|
||||
|
||||
public void LoadScene(EntitySceneManager sceneManager)
|
||||
public void LoadScene(EntitySceneContainer sceneContainer)
|
||||
{
|
||||
_currentEntitySceneManager = sceneManager;
|
||||
_currentEntitySceneContainer = sceneContainer;
|
||||
foreach (var entity in AllPositionalEntities)
|
||||
{
|
||||
_currentEntitySceneManager.AddIfNeeded(entity);
|
||||
_currentEntitySceneContainer.AddIfNeeded(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddEntity(Entity entity)
|
||||
{
|
||||
if(!_allEntities.Contains(entity))
|
||||
_allEntities.Add(entity);
|
||||
if(entity is PositionalEntity positionalEntity && positionalEntity.sceneName == _currentEntitySceneContainer?.sceneName)
|
||||
CreateEntityNode(positionalEntity);
|
||||
}
|
||||
|
||||
public void CreateEntityNode(PositionalEntity entity)
|
||||
{
|
||||
if(_currentEntitySceneContainer == null) return;
|
||||
entity.AddEntity(_currentEntitySceneContainer);
|
||||
}
|
||||
|
||||
public EntitySceneContainer? CurrentEntitySceneContainer => _currentEntitySceneContainer;
|
||||
|
||||
public void SetSceneContainer(EntitySceneContainer sceneContainer)
|
||||
{
|
||||
_currentEntitySceneContainer = sceneContainer;
|
||||
}
|
||||
|
||||
public void UnsetSceneContainer()
|
||||
{
|
||||
_currentEntitySceneContainer = null;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||
|
||||
public static class EntityManagerUtil
|
||||
{
|
||||
public static void AddIfNeeded(this EntitySceneManager? self, PositionalEntity entity)
|
||||
public static void AddIfNeeded(this EntitySceneContainer? self, PositionalEntity entity)
|
||||
{
|
||||
if(self == null) return;
|
||||
if(self.sceneName != entity.sceneName) return;
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||
using Godot;
|
||||
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;
|
||||
[Export] private Dictionary<string, PackedScene> _entityPrefabs;
|
||||
|
||||
public T Create<T>(EntityType type) where T:Entity
|
||||
public Node2D CreateNode2D(string type)
|
||||
{
|
||||
return _entityPrefabs[type].Instantiate<T>();
|
||||
return _entityPrefabs[type].Instantiate<Node2D>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||
|
||||
public partial class EntitySceneContainer : Node2D
|
||||
{
|
||||
[Export] public string sceneName = "none";
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
EntityManager.Instance.SetSceneContainer(this);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
EntityManager.Instance.UnsetSceneContainer();
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
AddAllEntities();
|
||||
}
|
||||
|
||||
public void AddAllEntities()
|
||||
{
|
||||
foreach (var positionalEntity in EntityManager.Instance.AllPositionalEntities.Where(x => x.sceneName == sceneName))
|
||||
{
|
||||
AddEntity(positionalEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddEntity(PositionalEntity entity)
|
||||
{
|
||||
entity.AddEntity(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Types;
|
||||
|
||||
public enum EntityType
|
||||
{
|
||||
None = 0,
|
||||
Yeli = 1,
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://cjygyr4lc224m
|
||||
Reference in New Issue
Block a user