using System; using System.Collections.Generic; using Svelto.DataStructures; namespace Svelto.ECS { public class EngineNodeDB : IEngineNodeDB { internal EngineNodeDB( Dictionary> nodesDB, Dictionary> nodesDBdic, Dictionary> metaNodesDB) { _nodesDB = nodesDB; _nodesDBdic = nodesDBdic; _metaNodesDB = metaNodesDB; } public FasterReadOnlyListCast QueryNodes() where T:INode { var type = typeof(T); FasterList nodes; if (_nodesDB.TryGetValue(type, out nodes) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(nodes); } public ReadOnlyDictionary QueryIndexableNodes() where T:INode { var type = typeof(T); Dictionary nodes; if (_nodesDBdic.TryGetValue(type, out nodes) == false) return _defaultEmptyNodeDict; return new ReadOnlyDictionary(nodes); } public T QueryMetaNode(int metaEntityID) where T : INode { return QueryNode(metaEntityID); } public bool TryQueryMetaNode(int metaEntityID, out T node) where T : INode { return TryQueryNode(metaEntityID, out node); } public FasterReadOnlyListCast QueryMetaNodes() where T : INode { var type = typeof(T); FasterList nodes; if (_metaNodesDB.TryGetValue(type, out nodes) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(nodes); } public bool TryQueryNode(int ID, out T node) where T:INode { var type = typeof(T); INode internalNode; Dictionary nodes; if (_nodesDBdic.TryGetValue(type, out nodes) && nodes.TryGetValue(ID, out internalNode)) { node = (T)internalNode; return true; } node = default(T); return false; } public T QueryNode(int ID) where T:INode { var type = typeof(T); INode internalNode; Dictionary nodes; if (_nodesDBdic.TryGetValue(type, out nodes) && nodes.TryGetValue(ID, out internalNode)) return (T)internalNode; throw new Exception("Node Not Found"); } static FasterReadOnlyListCast RetrieveEmptyNodeList() where T : INode { return FasterReadOnlyListCast.DefaultList; } readonly Dictionary> _nodesDB; readonly Dictionary> _nodesDBdic; readonly Dictionary> _metaNodesDB; readonly ReadOnlyDictionary _defaultEmptyNodeDict = new ReadOnlyDictionary(new Dictionary()); } }