93 lines
2.8 KiB
C#
93 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Encompass
|
|
{
|
|
internal abstract class TypedComponentStore
|
|
{
|
|
public abstract int Count { get; }
|
|
public abstract IEnumerable<(int, Type, IComponent)> AllInterfaceTyped();
|
|
public abstract bool Has(int entity);
|
|
public abstract bool Remove(int entity, int priority);
|
|
public abstract void ForceRemove(int entity);
|
|
public abstract void Clear();
|
|
}
|
|
|
|
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct, IComponent
|
|
{
|
|
private readonly Dictionary<int, TComponent> store = new Dictionary<int, TComponent>(128);
|
|
private readonly Dictionary<int, int> priorities = new Dictionary<int, int>(128);
|
|
|
|
public override int Count { get => store.Count; }
|
|
|
|
public TComponent Get(int entityID)
|
|
{
|
|
if (!store.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
|
|
return store[entityID];
|
|
}
|
|
|
|
public void Set(int entityID, TComponent component)
|
|
{
|
|
store[entityID] = component;
|
|
}
|
|
|
|
public bool Set(int entityID, TComponent component, int priority)
|
|
{
|
|
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID])
|
|
{
|
|
store[entityID] = component;
|
|
priorities[entityID] = priority;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override bool Remove(int entityID, int priority)
|
|
{
|
|
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID])
|
|
{
|
|
priorities[entityID] = priority;
|
|
store.Remove(entityID);
|
|
priorities.Remove(entityID);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public override void ForceRemove(int entityID)
|
|
{
|
|
store.Remove(entityID);
|
|
priorities.Remove(entityID);
|
|
}
|
|
|
|
public override bool Has(int entityID)
|
|
{
|
|
return store.ContainsKey(entityID);
|
|
}
|
|
|
|
public override void Clear()
|
|
{
|
|
store.Clear();
|
|
priorities.Clear();
|
|
}
|
|
|
|
public IEnumerable<(TComponent, int)> All()
|
|
{
|
|
foreach (var kvp in store)
|
|
{
|
|
yield return (kvp.Value, kvp.Key);
|
|
}
|
|
}
|
|
|
|
public override IEnumerable<(int, Type, IComponent)> AllInterfaceTyped()
|
|
{
|
|
foreach (var kvp in store)
|
|
{
|
|
yield return (kvp.Key, typeof(TComponent), (IComponent)kvp.Value);
|
|
}
|
|
}
|
|
}
|
|
}
|