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.

116 lines
3.6KB

  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, FasterList<INode>> nodesDB,
  10. Dictionary<Type, Dictionary<int, INode>> nodesDBdic,
  11. Dictionary<Type, FasterList<INode>> metaNodesDB,
  12. Dictionary<Type, IStructGroupNodes> structNodesDB)
  13. {
  14. _nodesDB = nodesDB;
  15. _nodesDBdic = nodesDBdic;
  16. _metaNodesDB = metaNodesDB;
  17. _structNodesDB = structNodesDB;
  18. }
  19. public FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode
  20. {
  21. var type = typeof(T);
  22. if (_nodesDB.ContainsKey(type) == false)
  23. return RetrieveEmptyNodeList<T>();
  24. return new FasterReadOnlyListCast<INode, T>(_nodesDB[type]);
  25. }
  26. public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode
  27. {
  28. var type = typeof(T);
  29. if (_nodesDBdic.ContainsKey(type) == false)
  30. return _defaultEmptyNodeDict;
  31. return new ReadOnlyDictionary<int, INode>(_nodesDBdic[type]);
  32. }
  33. public T QueryMetaNode<T>(int metaEntityID) where T : INode
  34. {
  35. return QueryNode<T>(metaEntityID);
  36. }
  37. public bool TryQueryMetaNode<T>(int metaEntityID, out T node) where T : INode
  38. {
  39. return TryQueryNode(metaEntityID, out node);
  40. }
  41. public FasterReadOnlyListCast<INode, T> QueryMetaNodes<T>() where T : INode
  42. {
  43. var type = typeof(T);
  44. if (_metaNodesDB.ContainsKey(type) == false)
  45. return RetrieveEmptyNodeList<T>();
  46. return new FasterReadOnlyListCast<INode, T>(_metaNodesDB[type]);
  47. }
  48. public bool TryQueryNode<T>(int ID, out T node) where T:INode
  49. {
  50. var type = typeof(T);
  51. INode internalNode;
  52. if (_nodesDBdic.ContainsKey(type) &&
  53. _nodesDBdic[type].TryGetValue(ID, out internalNode))
  54. {
  55. node = (T)internalNode;
  56. return true;
  57. }
  58. node = default(T);
  59. return false;
  60. }
  61. public T QueryNode<T>(int ID) where T:INode
  62. {
  63. var type = typeof(T);
  64. INode internalNode;
  65. if (_nodesDBdic.ContainsKey(type) &&
  66. _nodesDBdic[type].TryGetValue(ID, out internalNode))
  67. return (T)internalNode;
  68. throw new Exception("Node Not Found");
  69. }
  70. public FasterReadOnlyListCast<INode, T> QueryGroupNodes<T>(int groupID) where T : INode
  71. {
  72. var type = typeof(T);
  73. if (_nodesDB.ContainsKey(type) == false)
  74. return RetrieveEmptyNodeList<T>();
  75. return new FasterReadOnlyListCast<INode, T>(_nodesDB[type]);
  76. }
  77. static FasterReadOnlyListCast<INode, T> RetrieveEmptyNodeList<T>() where T : INode
  78. {
  79. return new FasterReadOnlyListCast<INode, T>(FasterList<INode>.DefaultList);
  80. }
  81. readonly Dictionary<Type, FasterList<INode>> _nodesDB;
  82. readonly Dictionary<Type, Dictionary<int, INode>> _nodesDBdic;
  83. readonly Dictionary<Type, FasterList<INode>> _metaNodesDB;
  84. readonly Dictionary<Type, IStructGroupNodes> _structNodesDB;
  85. readonly ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  86. }
  87. }