112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal class ComponentStore
|
|
{
|
|
private Dictionary<Type, TypedComponentStore> Stores = new Dictionary<Type, TypedComponentStore>(512);
|
|
|
|
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
|
|
{
|
|
foreach (var entry in Stores)
|
|
{
|
|
yield return (entry.Key, entry.Value);
|
|
}
|
|
}
|
|
|
|
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!Stores.ContainsKey(typeof(TComponent)))
|
|
{
|
|
System.Console.WriteLine("register component type in component store");
|
|
var store = new TypedComponentStore<TComponent>();
|
|
Stores.Add(typeof(TComponent), store);
|
|
}
|
|
}
|
|
|
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
//RegisterComponentType<TComponent>();
|
|
return Stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
|
|
}
|
|
|
|
public bool Has<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return Lookup<TComponent>().Has(entity);
|
|
}
|
|
|
|
public bool Has(Type type, Entity entity)
|
|
{
|
|
return Stores.ContainsKey(type) && Stores[type].Has(entity);
|
|
}
|
|
|
|
public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return Lookup<TComponent>().Get(entity);
|
|
}
|
|
|
|
public void Set<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
Lookup<TComponent>().Set(entity, component);
|
|
}
|
|
|
|
public bool Set<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
|
|
{
|
|
return Lookup<TComponent>().Set(entity, component, priority);
|
|
}
|
|
|
|
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
Lookup<TComponent>().Remove(entity);
|
|
}
|
|
|
|
public void Remove(Entity entity)
|
|
{
|
|
foreach (var entry in Stores.Values)
|
|
{
|
|
entry.Remove(entity);
|
|
}
|
|
}
|
|
|
|
public bool Any<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return Lookup<TComponent>().Count > 0;
|
|
}
|
|
|
|
public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped()
|
|
{
|
|
foreach (var store in Stores.Values)
|
|
{
|
|
foreach (var thing in store.AllInterfaceTyped())
|
|
{
|
|
yield return thing;
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<(Entity, TComponent)> All<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return Lookup<TComponent>().All();
|
|
}
|
|
|
|
public void Clear<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
Lookup<TComponent>().Clear();
|
|
}
|
|
|
|
public void ClearAll()
|
|
{
|
|
foreach (var store in Stores.Values)
|
|
{
|
|
store.Clear();
|
|
}
|
|
}
|
|
|
|
public void SwapWith(ComponentStore other)
|
|
{
|
|
(Stores, other.Stores) = (other.Stores, Stores);
|
|
}
|
|
}
|
|
}
|