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.

112 lines
3.4KB

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