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.

EngineNodeDB.cs 3.0KB

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