Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. namespace Svelto.ECS
  6. {
  7. public class EngineNodeDB : IEngineNodeDB
  8. {
  9. internal EngineNodeDB( Dictionary<Type, ITypeSafeList> nodesDB,
  10. Dictionary<Type, ITypeSafeDictionary> nodesDBdic,
  11. Dictionary<Type, ITypeSafeList> metaNodesDB,
  12. Dictionary<int, Dictionary<Type, ITypeSafeList>> groupNodesDB)
  13. {
  14. _nodesDB = nodesDB;
  15. _nodesDBdic = nodesDBdic;
  16. _metaNodesDB = metaNodesDB;
  17. _groupNodesDB = groupNodesDB;
  18. }
  19. public FasterReadOnlyList<T> QueryNodes<T>()
  20. {
  21. var type = typeof(T);
  22. ITypeSafeList nodes;
  23. if (_nodesDB.TryGetValue(type, out nodes) == false)
  24. return RetrieveEmptyNodeList<T>();
  25. return new FasterReadOnlyList<T>((FasterList<T>)nodes);
  26. }
  27. public FasterReadOnlyList<T> QueryGroupedNodes<T>(int @group)
  28. {
  29. return new FasterReadOnlyList<T>(_groupNodesDB[group] as FasterList<T>);
  30. }
  31. public T[] QueryNodesAsArray<T>(out int count) where T : struct
  32. {
  33. var type = typeof(T);
  34. count = 0;
  35. ITypeSafeList nodes;
  36. if (_nodesDB.TryGetValue(type, out nodes) == false)
  37. return null;
  38. var castedNodes = (FasterList<T>)nodes;
  39. count = castedNodes.Count;
  40. return castedNodes.ToArrayFast();
  41. }
  42. public ReadOnlyDictionary<int, T> QueryIndexableNodes<T>() where T:NodeWithID
  43. {
  44. var type = typeof(T);
  45. ITypeSafeDictionary nodes;
  46. if (_nodesDBdic.TryGetValue(type, out nodes) == false)
  47. return TypeSafeDictionary<T>.Default;
  48. return new ReadOnlyDictionary<int, T>(nodes as Dictionary<int, T>);
  49. }
  50. public T QueryMetaNode<T>(int metaEntityID) where T:NodeWithID
  51. {
  52. return QueryNode<T>(metaEntityID);
  53. }
  54. public bool TryQueryMetaNode<T>(int metaEntityID, out T node) where T:NodeWithID
  55. {
  56. return TryQueryNode(metaEntityID, out node);
  57. }
  58. public FasterReadOnlyList<T> QueryMetaNodes<T>()
  59. {
  60. var type = typeof(T);
  61. ITypeSafeList nodes;
  62. if (_metaNodesDB.TryGetValue(type, out nodes) == false)
  63. return RetrieveEmptyNodeList<T>();
  64. return new FasterReadOnlyList<T>((FasterList<T>)nodes);
  65. }
  66. public bool TryQueryNode<T>(int ID, out T node) where T:NodeWithID
  67. {
  68. var type = typeof(T);
  69. T internalNode;
  70. ITypeSafeDictionary nodes;
  71. TypeSafeDictionary<T> casted;
  72. _nodesDBdic.TryGetValue(type, out nodes);
  73. casted = nodes as TypeSafeDictionary<T>;
  74. if (casted != null &&
  75. casted.TryGetValue(ID, out internalNode))
  76. {
  77. node = (T) internalNode;
  78. return true;
  79. }
  80. node = default(T);
  81. return false;
  82. }
  83. public T QueryNode<T>(int ID) where T:NodeWithID
  84. {
  85. var type = typeof(T);
  86. T internalNode; ITypeSafeDictionary nodes;
  87. TypeSafeDictionary<T> casted;
  88. _nodesDBdic.TryGetValue(type, out nodes);
  89. casted = nodes as TypeSafeDictionary<T>;
  90. if (casted != null &&
  91. casted.TryGetValue(ID, out internalNode))
  92. return (T)internalNode;
  93. throw new Exception("Node Not Found");
  94. }
  95. static FasterReadOnlyList<T> RetrieveEmptyNodeList<T>()
  96. {
  97. return FasterReadOnlyList<T>.DefaultList;
  98. }
  99. readonly Dictionary<Type, ITypeSafeList> _nodesDB;
  100. readonly Dictionary<Type, ITypeSafeDictionary> _nodesDBdic;
  101. readonly Dictionary<Type, ITypeSafeList> _metaNodesDB;
  102. readonly Dictionary<int, Dictionary<Type, ITypeSafeList>> _groupNodesDB;
  103. }
  104. }