|
|
|
@ -1,5 +1,4 @@
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using Godot;
|
|
|
|
using Godot;
|
|
|
|
using Entity = Babushka.scripts.CSharp.GameEntity.Entities.Entity;
|
|
|
|
using Entity = Babushka.scripts.CSharp.GameEntity.Entities.Entity;
|
|
|
|
@ -7,21 +6,23 @@ using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalE
|
|
|
|
|
|
|
|
|
|
|
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
|
|
|
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Manages the lifecycle and interactions of all entities within the game, including their creation, retrieval,
|
|
|
|
|
|
|
|
/// and organization. The EntityManager serves as a centralized hub for managing both standard and positional entities.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
public partial class EntityManager : Node
|
|
|
|
public partial class EntityManager : Node
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[Export] private EntityNodeCreator _nodeCreator = null!;
|
|
|
|
public static EntityManager Instance;
|
|
|
|
|
|
|
|
|
|
|
|
public EntityNodeCreator NodeCreator => _nodeCreator;
|
|
|
|
[Export] private EntityNodeCreator _nodeCreator = null!;
|
|
|
|
|
|
|
|
|
|
|
|
private EntitySceneContainer? _currentEntitySceneContainer;
|
|
|
|
private EntitySceneContainer? _currentEntitySceneContainer;
|
|
|
|
|
|
|
|
|
|
|
|
public static EntityManager Instance;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly List<Entity> _allEntities = new();
|
|
|
|
private readonly List<Entity> _allEntities = new();
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Entity> AllEntities => _allEntities;
|
|
|
|
public IEnumerable<Entity> AllEntities => _allEntities;
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
|
|
|
|
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
|
|
|
|
|
|
|
|
public EntitySceneContainer? CurrentEntitySceneContainer => _currentEntitySceneContainer;
|
|
|
|
|
|
|
|
public EntityNodeCreator NodeCreator => _nodeCreator;
|
|
|
|
|
|
|
|
|
|
|
|
public override void _EnterTree()
|
|
|
|
public override void _EnterTree()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -30,6 +31,7 @@ public partial class EntityManager : Node
|
|
|
|
|
|
|
|
|
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
public override void _Input(InputEvent @event)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
// for debugging purposes
|
|
|
|
if (@event.IsActionPressed("DebugEntities"))
|
|
|
|
if (@event.IsActionPressed("DebugEntities"))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
GD.Print("Entities:");
|
|
|
|
GD.Print("Entities:");
|
|
|
|
@ -40,36 +42,48 @@ public partial class EntityManager : Node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void UnloadScene()
|
|
|
|
#region ENTITY MANAGEMENT
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_currentEntitySceneContainer == null) return;
|
|
|
|
|
|
|
|
_currentEntitySceneContainer = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void LoadScene(EntitySceneContainer sceneContainer)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_currentEntitySceneContainer = sceneContainer;
|
|
|
|
|
|
|
|
foreach (var entity in AllPositionalEntities)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_currentEntitySceneContainer.AddIfNeeded(entity);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Adds an entity to the list of managed entities. If the entity is a positional entity
|
|
|
|
|
|
|
|
/// and its scene matches the current scene container, it is also instantiated in the scene.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <param name="entity">The entity to be added to the manager.</param>
|
|
|
|
public void AddEntity(Entity entity)
|
|
|
|
public void AddEntity(Entity entity)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!_allEntities.Contains(entity))
|
|
|
|
if (!_allEntities.Contains(entity))
|
|
|
|
_allEntities.Add(entity);
|
|
|
|
_allEntities.Add(entity);
|
|
|
|
if(entity is PositionalEntity positionalEntity && positionalEntity.sceneName == _currentEntitySceneContainer?.sceneName)
|
|
|
|
if(entity is PositionalEntity positionalEntity && positionalEntity.sceneName == _currentEntitySceneContainer?.sceneName)
|
|
|
|
CreateEntityNode(positionalEntity);
|
|
|
|
InstantiatePositionalEntityNode(positionalEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void CreateEntityNode(PositionalEntity entity)
|
|
|
|
private void InstantiatePositionalEntityNode(PositionalEntity entity)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(_currentEntitySceneContainer == null) return;
|
|
|
|
if(_currentEntitySceneContainer == null) return;
|
|
|
|
entity.AddEntity(_currentEntitySceneContainer);
|
|
|
|
entity.InstantiateEntityNode(_currentEntitySceneContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public EntitySceneContainer? CurrentEntitySceneContainer => _currentEntitySceneContainer;
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// Retrieves the first entity of the specified type from the list of managed entities.
|
|
|
|
|
|
|
|
/// If no such entity exists, creates a new instance of the specified type, adds it to the manager, and returns it.
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
/// <typeparam name="T">The type of entity to retrieve or create. Must inherit from the Entity class and have a parameterless constructor.</typeparam>
|
|
|
|
|
|
|
|
/// <returns>The first entity of the specified type or a newly created entity of that type if none were found.</returns>
|
|
|
|
|
|
|
|
public T GetUniqueEntity<T>() where T : Entity, new()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var result = AllEntities.OfType<T>().FirstOrDefault();
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var newEntity = new T();
|
|
|
|
|
|
|
|
AddEntity(newEntity);
|
|
|
|
|
|
|
|
result = newEntity;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region SCENE CONTAINER ACCESS
|
|
|
|
|
|
|
|
|
|
|
|
public void SetSceneContainer(EntitySceneContainer sceneContainer)
|
|
|
|
public void SetSceneContainer(EntitySceneContainer sceneContainer)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -80,4 +94,7 @@ public partial class EntityManager : Node
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_currentEntitySceneContainer = null;
|
|
|
|
_currentEntitySceneContainer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|