using System; using System.Collections.Generic; using System.Linq; namespace Encompass { internal abstract class TypedComponentStore { public abstract int Count { get; } public abstract IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped(); public abstract bool Has(Entity entity); public abstract void Remove(Entity entity); public abstract void Clear(); } internal class TypedComponentStore : TypedComponentStore where TComponent : struct, IComponent { private readonly Dictionary store = new Dictionary(128); private readonly Dictionary priorities = new Dictionary(128); public override int Count { get => store.Count; } public TComponent Get(Entity entity) { return store[entity]; } public void Set(Entity entity, TComponent component) { store[entity] = component; } public bool Set(Entity entity, TComponent component, int priority) { if (!priorities.ContainsKey(entity) || priority < priorities[entity]) { store[entity] = component; return true; } return false; } public override bool Has(Entity entity) { return store.ContainsKey(entity); } public override void Clear() { store.Clear(); } public IEnumerable<(Entity, TComponent)> All() { return store.Select(kvp => (kvp.Key, kvp.Value)); } public override IEnumerable<(Entity, Type, IComponent)> AllInterfaceTyped() { return store.Select(kvp => (kvp.Key, typeof(TComponent), (IComponent)kvp.Value)); } // public override IEnumerable All() // { // return store.Values.Cast(); // } public override void Remove(Entity entity) { store.Remove(entity); priorities.Remove(entity); } } }