Added basic entity scripts
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://umop2b1m1qm8
|
||||
@@ -0,0 +1,14 @@
|
||||
using PositionalEntity = Babushka.scripts.CSharp.GameEntity.Entities.PositionalEntity;
|
||||
|
||||
namespace Babushka.scripts.CSharp.GameEntity.Management;
|
||||
|
||||
public static class EntityManagerUtil
|
||||
{
|
||||
public static void AddIfNeeded(this EntitySceneManager? self, PositionalEntity entity)
|
||||
{
|
||||
if(self == null) return;
|
||||
if(self.sceneName != entity.sceneName) return;
|
||||
|
||||
self.AddEntity(entity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dc3283h7sx4cl
|
||||
@@ -0,0 +1,16 @@
|
||||
using Babushka.scripts.CSharp.GameEntity.Types;
|
||||
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;
|
||||
|
||||
public T Create<T>(EntityType type) where T:Entity
|
||||
{
|
||||
return _entityPrefabs[type].Instantiate<T>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bogqp274y1pgr
|
||||
@@ -0,0 +1,25 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://ca1pg6k3gn47y
|
||||
Reference in New Issue
Block a user