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.

110 lines
3.2KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS
  5. {
  6. public class EngineNodeDB : IEngineNodeDB
  7. {
  8. internal EngineNodeDB( Dictionary<Type, FasterList<INode>> nodesDB,
  9. Dictionary<Type, Dictionary<int, INode>> nodesDBdic,
  10. Dictionary<Type, FasterList<INode>> metaNodesDB)
  11. {
  12. _nodesDB = nodesDB;
  13. _nodesDBdic = nodesDBdic;
  14. _metaNodesDB = metaNodesDB;
  15. }
  16. public FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode
  17. {
  18. var type = typeof(T);
  19. FasterList<INode> nodes;
  20. if (_nodesDB.TryGetValue(type, out nodes) == false)
  21. return RetrieveEmptyNodeList<T>();
  22. return new FasterReadOnlyListCast<INode, T>(nodes);
  23. }
  24. public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode
  25. {
  26. var type = typeof(T);
  27. Dictionary<int, INode> nodes;
  28. if (_nodesDBdic.TryGetValue(type, out nodes) == false)
  29. return _defaultEmptyNodeDict;
  30. return new ReadOnlyDictionary<int, INode>(nodes);
  31. }
  32. public T QueryMetaNode<T>(int metaEntityID) where T : INode
  33. {
  34. return QueryNode<T>(metaEntityID);
  35. }
  36. public bool TryQueryMetaNode<T>(int metaEntityID, out T node) where T : INode
  37. {
  38. return TryQueryNode(metaEntityID, out node);
  39. }
  40. public FasterReadOnlyListCast<INode, T> QueryMetaNodes<T>() where T : INode
  41. {
  42. var type = typeof(T);
  43. FasterList<INode> nodes;
  44. if (_metaNodesDB.TryGetValue(type, out nodes) == false)
  45. return RetrieveEmptyNodeList<T>();
  46. return new FasterReadOnlyListCast<INode, T>(nodes);
  47. }
  48. public bool TryQueryNode<T>(int ID, out T node) where T:INode
  49. {
  50. var type = typeof(T);
  51. INode internalNode;
  52. Dictionary<int, INode> nodes;
  53. if (_nodesDBdic.TryGetValue(type, out nodes) &&
  54. nodes.TryGetValue(ID, out internalNode))
  55. {
  56. node = (T)internalNode;
  57. return true;
  58. }
  59. node = default(T);
  60. return false;
  61. }
  62. public T QueryNode<T>(int ID) where T:INode
  63. {
  64. var type = typeof(T);
  65. INode internalNode; Dictionary<int, INode> nodes;
  66. if (_nodesDBdic.TryGetValue(type, out nodes) &&
  67. nodes.TryGetValue(ID, out internalNode))
  68. return (T)internalNode;
  69. throw new Exception("Node Not Found");
  70. }
  71. static FasterReadOnlyListCast<INode, T> RetrieveEmptyNodeList<T>() where T : INode
  72. {
  73. return FasterReadOnlyListCast<INode, T>.DefaultList;
  74. }
  75. readonly Dictionary<Type, FasterList<INode>> _nodesDB;
  76. readonly Dictionary<Type, Dictionary<int, INode>> _nodesDBdic;
  77. readonly Dictionary<Type, FasterList<INode>> _metaNodesDB;
  78. readonly ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  79. }
  80. }