using System; using System.Collections.Generic; namespace MoonTools.ECS { internal class RelationDepot { private Dictionary storages = new Dictionary(); private void Register() where TRelationKind : struct { if (!storages.ContainsKey(typeof(TRelationKind))) { storages.Add(typeof(TRelationKind), new RelationStorage()); } } private RelationStorage Lookup() where TRelationKind : struct { Register(); return (RelationStorage) storages[typeof(TRelationKind)]; } public void Add(Relation relation, TRelationKind relationData) where TRelationKind : struct { Lookup().Add(relation, relationData); } public void Remove(Relation relation) where TRelationKind : struct { Lookup().Remove(relation); } // FIXME: optimize this public void OnEntityDestroy(int entityID) { foreach (var storage in storages.Values) { storage.OnEntityDestroy(entityID); } } public IEnumerable<(Entity, Entity, TRelationKind)> Relations() where TRelationKind : struct { return Lookup().All(); } public bool Related(int idA, int idB) where TRelationKind : struct { return Lookup().Has(new Relation(idA, idB)); } public IEnumerable<(Entity, TRelationKind)> RelatedToA(int entityID) where TRelationKind : struct { return Lookup().RelatedToA(entityID); } public IEnumerable<(Entity, TRelationKind)> RelatedToB(int entityID) where TRelationKind : struct { return Lookup().RelatedToB(entityID); } } }