First adjustments to the Entity System to make it work with different types
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user