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.
Babushka/scripts/CSharp/GameEntity/Management/EntityManager.cs

59 lines
1.8 KiB

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<Entity> _allEntities = new();
public IEnumerable<Entity> AllEntities => _allEntities;
public IEnumerable<PositionalEntity> AllPositionalEntities => _allEntities.OfType<PositionalEntity>();
public T NewPositionalEntity<T>(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<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;
}
public void LoadScene(EntitySceneManager sceneManager)
{
_currentEntitySceneManager = sceneManager;
foreach (var entity in AllPositionalEntities)
{
_currentEntitySceneManager.AddIfNeeded(entity);
}
}
}