62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
|
|||
|
namespace Encompass
|
|||
|
{
|
|||
|
internal class ComponentStore
|
|||
|
{
|
|||
|
private readonly Dictionary<Type, object> Stores = new Dictionary<Type, object>();
|
|||
|
private readonly Dictionary<Type, Action> ClearMethods = new Dictionary<Type, Action>();
|
|||
|
|
|||
|
private Dictionary<Guid, TComponent> Lookup<TComponent>() where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
if (!Stores.ContainsKey(typeof(TComponent)))
|
|||
|
{
|
|||
|
var dictionary = new Dictionary<Guid, TComponent>();
|
|||
|
Stores.Add(typeof(TComponent), dictionary);
|
|||
|
ClearMethods.Add(typeof(TComponent), dictionary.Clear);
|
|||
|
}
|
|||
|
|
|||
|
return Stores[typeof(TComponent)] as Dictionary<Guid, TComponent>;
|
|||
|
}
|
|||
|
|
|||
|
public bool Has<TComponent>(Guid id) where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
return Lookup<TComponent>().ContainsKey(id);
|
|||
|
}
|
|||
|
|
|||
|
public TComponent Get<TComponent>(Guid id) where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
return Lookup<TComponent>()[id];
|
|||
|
}
|
|||
|
|
|||
|
public void Set(Type type, Guid id, IComponent component)
|
|||
|
{
|
|||
|
(Stores[type] as Dictionary<Guid, IComponent>)[id] = component;
|
|||
|
}
|
|||
|
|
|||
|
public void Set<TComponent>(Guid id, TComponent component) where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
Lookup<TComponent>()[id] = component;
|
|||
|
}
|
|||
|
|
|||
|
public void Remove<TComponent>(Guid id) where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
Lookup<TComponent>().Remove(id);
|
|||
|
}
|
|||
|
|
|||
|
public void Clear<TComponent>() where TComponent : struct, IComponent
|
|||
|
{
|
|||
|
Lookup<TComponent>().Clear();
|
|||
|
}
|
|||
|
|
|||
|
public void ClearAll()
|
|||
|
{
|
|||
|
foreach (var type in Stores.Keys)
|
|||
|
{
|
|||
|
ClearMethods[type]();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|