You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.4 KiB
83 lines
2.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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!;
|
|
|
|
public EntityNodeCreator NodeCreator => _nodeCreator;
|
|
|
|
private EntitySceneContainer? _currentEntitySceneContainer;
|
|
|
|
public static EntityManager Instance;
|
|
|
|
private readonly List<Entity> _allEntities = new();
|
|
|
|
public IEnumerable<Entity> AllEntities => _allEntities;
|
|
|
|
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
|
|
|
|
public override void _EnterTree()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (@event.IsActionPressed("DebugEntities"))
|
|
{
|
|
GD.Print("Entities:");
|
|
foreach (var entity in AllEntities)
|
|
{
|
|
GD.Print(entity.EntityType + " " + entity.id);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UnloadScene()
|
|
{
|
|
if (_currentEntitySceneContainer == null) return;
|
|
_currentEntitySceneContainer = null;
|
|
}
|
|
|
|
public void LoadScene(EntitySceneContainer sceneContainer)
|
|
{
|
|
_currentEntitySceneContainer = sceneContainer;
|
|
foreach (var entity in AllPositionalEntities)
|
|
{
|
|
_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;
|
|
}
|
|
} |