2019-12-05 04:50:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-12-05 20:10:33 +00:00
|
|
|
|
using System.Linq;
|
2019-12-05 04:50:08 +00:00
|
|
|
|
|
|
|
|
|
namespace Encompass
|
|
|
|
|
{
|
|
|
|
|
internal class ComponentStore
|
|
|
|
|
{
|
2019-12-06 01:07:06 +00:00
|
|
|
|
private Dictionary<Type, TypedComponentStore> Stores = new Dictionary<Type, TypedComponentStore>(512);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
|
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable()
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2019-12-05 22:59:55 +00:00
|
|
|
|
foreach (var entry in Stores)
|
2019-12-05 20:10:33 +00:00
|
|
|
|
{
|
2019-12-05 22:59:55 +00:00
|
|
|
|
yield return (entry.Key, entry.Value);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
|
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Stores.ContainsKey(typeof(TComponent)))
|
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
var store = new TypedComponentStore<TComponent>();
|
|
|
|
|
Stores.Add(typeof(TComponent), store);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
2019-12-05 20:10:33 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2019-12-06 00:19:35 +00:00
|
|
|
|
return Stores.ContainsKey(type) && Stores[type].Has(entity);
|
2019-12-05 20:10:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TComponent Get<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
|
|
|
{
|
|
|
|
|
return Lookup<TComponent>().Get(entity);
|
|
|
|
|
}
|
2019-12-05 04:50:08 +00:00
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public void Set<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
|
|
|
{
|
|
|
|
|
Lookup<TComponent>().Set(entity, component);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
|
public bool Set<TComponent>(Entity entity, TComponent component, int priority) where TComponent : struct, IComponent
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 22:59:55 +00:00
|
|
|
|
return Lookup<TComponent>().Set(entity, component, priority);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public void Remove<TComponent>(Entity entity) where TComponent : struct, IComponent
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
Lookup<TComponent>().Remove(entity);
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public void Remove(Entity entity)
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
foreach (var entry in Stores.Values)
|
|
|
|
|
{
|
|
|
|
|
entry.Remove(entity);
|
|
|
|
|
}
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public bool Any<TComponent>() where TComponent : struct, IComponent
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
return Lookup<TComponent>().Count > 0;
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
// public IEnumerable<TComponent> All<TComponent>() where TComponent : struct, IComponent
|
|
|
|
|
// {
|
|
|
|
|
// return Lookup<TComponent>().All<TComponent>();
|
|
|
|
|
// }
|
|
|
|
|
|
2019-12-05 22:59:55 +00:00
|
|
|
|
public IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped()
|
|
|
|
|
{
|
|
|
|
|
foreach (var store in Stores.Values)
|
|
|
|
|
{
|
|
|
|
|
foreach (var thing in store.AllInterfaceTyped())
|
|
|
|
|
{
|
|
|
|
|
yield return thing;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-05 20:10:33 +00:00
|
|
|
|
public IEnumerable<(Entity, TComponent)> All<TComponent>() where TComponent : struct, IComponent
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
return Lookup<TComponent>().All();
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear<TComponent>() where TComponent : struct, IComponent
|
|
|
|
|
{
|
|
|
|
|
Lookup<TComponent>().Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearAll()
|
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
foreach (var store in Stores.Values)
|
2019-12-05 04:50:08 +00:00
|
|
|
|
{
|
2019-12-05 20:10:33 +00:00
|
|
|
|
store.Clear();
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-05 22:59:55 +00:00
|
|
|
|
|
|
|
|
|
public void SwapWith(ComponentStore other)
|
|
|
|
|
{
|
|
|
|
|
(Stores, other.Stores) = (other.Stores, Stores);
|
|
|
|
|
}
|
2019-12-05 04:50:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|