using System; using System.Collections.Generic; using Svelto.DataStructures; using Svelto.ECS.Internal; namespace Svelto.ECS { public class EngineNodeDB : IEngineNodeDB { internal EngineNodeDB( Dictionary> nodesDB, Dictionary> nodesDBdic, Dictionary> metaNodesDB, Dictionary structNodesDB) { _nodesDB = nodesDB; _nodesDBdic = nodesDBdic; _metaNodesDB = metaNodesDB; _structNodesDB = structNodesDB; } public FasterReadOnlyListCast QueryNodes() where T:INode { var type = typeof(T); if (_nodesDB.ContainsKey(type) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(_nodesDB[type]); } public ReadOnlyDictionary QueryIndexableNodes() where T:INode { var type = typeof(T); if (_nodesDBdic.ContainsKey(type) == false) return _defaultEmptyNodeDict; return new ReadOnlyDictionary(_nodesDBdic[type]); } 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); if (_metaNodesDB.ContainsKey(type) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(_metaNodesDB[type]); } public bool TryQueryNode(int ID, out T node) where T:INode { var type = typeof(T); INode internalNode; if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].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; if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode)) return (T)internalNode; throw new Exception("Node Not Found"); } public FasterReadOnlyListCast QueryGroupNodes(int groupID) where T : INode { var type = typeof(T); if (_nodesDB.ContainsKey(type) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyListCast(_nodesDB[type]); } static FasterReadOnlyListCast RetrieveEmptyNodeList() where T : INode { return new FasterReadOnlyListCast(FasterList.DefaultList); } readonly Dictionary> _nodesDB; readonly Dictionary> _nodesDBdic; readonly Dictionary> _metaNodesDB; readonly Dictionary _structNodesDB; readonly ReadOnlyDictionary _defaultEmptyNodeDict = new ReadOnlyDictionary(new Dictionary()); } }