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> groupNodesDB) { _nodesDB = nodesDB; _nodesDBdic = nodesDBdic; _metaNodesDB = metaNodesDB; _groupNodesDB = groupNodesDB; } public FasterReadOnlyList QueryNodes() { var type = typeof(T); ITypeSafeList nodes; if (_nodesDB.TryGetValue(type, out nodes) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyList((FasterList)nodes); } public FasterReadOnlyList QueryGroupedNodes(int @group) { return new FasterReadOnlyList(_groupNodesDB[group] as FasterList); } public T[] QueryNodesAsArray(out int count) where T : struct { var type = typeof(T); count = 0; ITypeSafeList nodes; if (_nodesDB.TryGetValue(type, out nodes) == false) return null; var castedNodes = (FasterList)nodes; count = castedNodes.Count; return castedNodes.ToArrayFast(); } public ReadOnlyDictionary QueryIndexableNodes() where T:NodeWithID { var type = typeof(T); ITypeSafeDictionary nodes; if (_nodesDBdic.TryGetValue(type, out nodes) == false) return TypeSafeDictionary.Default; return new ReadOnlyDictionary(nodes as Dictionary); } public T QueryMetaNode(int metaEntityID) where T:NodeWithID { return QueryNode(metaEntityID); } public bool TryQueryMetaNode(int metaEntityID, out T node) where T:NodeWithID { return TryQueryNode(metaEntityID, out node); } public FasterReadOnlyList QueryMetaNodes() { var type = typeof(T); ITypeSafeList nodes; if (_metaNodesDB.TryGetValue(type, out nodes) == false) return RetrieveEmptyNodeList(); return new FasterReadOnlyList((FasterList)nodes); } public bool TryQueryNode(int ID, out T node) where T:NodeWithID { var type = typeof(T); T internalNode; ITypeSafeDictionary nodes; TypeSafeDictionary casted; _nodesDBdic.TryGetValue(type, out nodes); casted = nodes as TypeSafeDictionary; if (casted != null && casted.TryGetValue(ID, out internalNode)) { node = (T) internalNode; return true; } node = default(T); return false; } public T QueryNode(int ID) where T:NodeWithID { var type = typeof(T); T internalNode; ITypeSafeDictionary nodes; TypeSafeDictionary casted; _nodesDBdic.TryGetValue(type, out nodes); casted = nodes as TypeSafeDictionary; if (casted != null && casted.TryGetValue(ID, out internalNode)) return (T)internalNode; throw new Exception("Node Not Found"); } static FasterReadOnlyList RetrieveEmptyNodeList() { return FasterReadOnlyList.DefaultList; } readonly Dictionary _nodesDB; readonly Dictionary _nodesDBdic; readonly Dictionary _metaNodesDB; readonly Dictionary> _groupNodesDB; } }