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 _allEntities = new(); public IEnumerable AllEntities => _allEntities; public IEnumerable AllPositionalEntities => _allEntities.OfType(); public T NewPositionalEntity(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(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); } } }