Some changes regarding ownership and making the api better
This commit is contained in:
parent
553b1f4166
commit
a8ac473666
4 changed files with 80 additions and 6 deletions
|
@ -6,7 +6,7 @@ using System.Collections;
|
||||||
|
|
||||||
typealias Component = int_cosize;
|
typealias Component = int_cosize;
|
||||||
|
|
||||||
class ComponentRegistry
|
class ComponentManager
|
||||||
{
|
{
|
||||||
|
|
||||||
private Dictionary<String, Component> _componentLookup = new .() ~ DeleteDictionaryAndKeys!(_);
|
private Dictionary<String, Component> _componentLookup = new .() ~ DeleteDictionaryAndKeys!(_);
|
16
src/DeleteECSAndItems.bf
Normal file
16
src/DeleteECSAndItems.bf
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
namespace Theater_ECS;
|
||||||
|
|
||||||
|
static
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
This exists for ownership reasons
|
||||||
|
If this mixin isnt called the ecs will not delete any allocated data passed into it
|
||||||
|
from the outside.
|
||||||
|
*/
|
||||||
|
public static mixin DeleteECSAndItems(ECS e)
|
||||||
|
{
|
||||||
|
DeleteDictionaryAndKeysAndValues!(e.Systems);
|
||||||
|
e.Systems = null;
|
||||||
|
delete e;
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,7 +20,7 @@ class ECS
|
||||||
//TODO: Entity_GetAllComponents
|
//TODO: Entity_GetAllComponents
|
||||||
|
|
||||||
|
|
||||||
private ComponentRegistry _compRegistry = new .() ~ delete _;
|
private ComponentManager _compRegistry = new .() ~ delete _;
|
||||||
|
|
||||||
///Registers a component but hides some of the work behind generics
|
///Registers a component but hides some of the work behind generics
|
||||||
public Component RegisterComponent<T>() => _compRegistry.RegisterComponent(typeof(T).GetFullName(.. scope .()), strideof(T));
|
public Component RegisterComponent<T>() => _compRegistry.RegisterComponent(typeof(T).GetFullName(.. scope .()), strideof(T));
|
||||||
|
@ -35,4 +35,7 @@ class ECS
|
||||||
public Span<Entity> GetComponentEntities(Component c) => _compRegistry.Components[c].GetAll();
|
public Span<Entity> GetComponentEntities(Component c) => _compRegistry.Components[c].GetAll();
|
||||||
|
|
||||||
[Inline] public void* GetComponentData(Entity e, Component c) => _compRegistry.Components[c].[Unchecked]Get(e);
|
[Inline] public void* GetComponentData(Entity e, Component c) => _compRegistry.Components[c].[Unchecked]Get(e);
|
||||||
|
|
||||||
|
public SystemsManager Systems = new .(this) ~ delete _;
|
||||||
|
public ComponentManager Components = new .() ~ delete _;
|
||||||
}
|
}
|
|
@ -3,13 +3,68 @@ namespace Theater_ECS;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
|
||||||
class SystemsManager
|
class SystemsManager : IEnumerable<(String key, System value)>
|
||||||
{
|
{
|
||||||
private List<System> _registeredSystems = new .() ~ delete _;
|
private ECS _owner;
|
||||||
|
private Dictionary<String, System> _registeredSystems = new .() ~ delete _;
|
||||||
|
|
||||||
public void RegisterSystem()
|
public this(ECS ecs)
|
||||||
{
|
{
|
||||||
|
_owner = ecs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///Registers a system and returns false if its already contained
|
||||||
|
public bool RegisterSystem(System sys, StringView id)
|
||||||
|
{
|
||||||
|
if (_registeredSystems.ContainsKeyAlt<StringView>(id))
|
||||||
|
return false;
|
||||||
|
_registeredSystems.Add(new .(id), sys);
|
||||||
|
sys.RegisterSystem(_owner);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
///Deregister a single system
|
||||||
|
public Result<System> DeregisterSystem(StringView id)
|
||||||
|
{
|
||||||
|
if (!_registeredSystems.ContainsKeyAlt<StringView>(id))
|
||||||
|
return .Err;
|
||||||
|
return .Ok(_registeredSystems.GetAndRemoveAlt<StringView>(id).Value.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
///Remove all systems from use and return the amount of systems cleared
|
||||||
|
public int ClearAllSystems()
|
||||||
|
{
|
||||||
|
var c = _registeredSystems.Count;
|
||||||
|
_registeredSystems.Clear();
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
///Runs all systems once
|
||||||
|
public void RunAllSystems(ECS ecs)
|
||||||
|
{
|
||||||
|
for (var i in _registeredSystems)
|
||||||
|
i.value.RunSystem(ecs);
|
||||||
|
}
|
||||||
|
|
||||||
|
///Run a single system
|
||||||
|
public Result<void> RunSystem(StringView id, ECS ecs)
|
||||||
|
{
|
||||||
|
if (!_registeredSystems.ContainsKeyAlt<StringView>(id))
|
||||||
|
return .Err;
|
||||||
|
|
||||||
|
_registeredSystems[scope .(id)].RunSystem(ecs);
|
||||||
|
|
||||||
|
return .Ok;
|
||||||
|
}
|
||||||
|
|
||||||
|
///Runs a function over all systems
|
||||||
|
public void EnumerateSystems(delegate void(System) func)
|
||||||
|
{
|
||||||
|
for (var i in _registeredSystems)
|
||||||
|
func.Invoke(i.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count => _registeredSystems.Count;
|
||||||
|
|
||||||
|
public Dictionary<String, System>.Enumerator GetEnumerator() => _registeredSystems.GetEnumerator();
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue