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.

60 lines
1.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ES
  5. {
  6. class EngineNodeDB : IEngineNodeDB
  7. {
  8. internal EngineNodeDB(Dictionary<Type, FasterList<INode>> nodesDB, Dictionary<Type, Dictionary<int, INode>> nodesDBdic)
  9. {
  10. this._nodesDB = nodesDB;
  11. this._nodesDBdic = nodesDBdic;
  12. }
  13. public FasterReadOnlyList<INode> QueryNodes<T>() where T:INode
  14. {
  15. var type = typeof(T);
  16. if (_nodesDB.ContainsKey(type) == false)
  17. return _defaultEmptyNodeList;
  18. return new FasterReadOnlyList<INode>(_nodesDB[type]);
  19. }
  20. public ReadOnlyDictionary<int, INode> QueryIndexableNodes<T>() where T:INode
  21. {
  22. var type = typeof(T);
  23. if (_nodesDBdic.ContainsKey(type) == false)
  24. return _defaultEmptyNodeDict;
  25. return new ReadOnlyDictionary<int, INode>(_nodesDBdic[type]);
  26. }
  27. public bool QueryNode<T>(int ID, out T node) where T:INode
  28. {
  29. var type = typeof(T);
  30. INode internalNode;
  31. if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode))
  32. {
  33. node = (T)internalNode;
  34. return true;
  35. }
  36. node = default(T);
  37. return false;
  38. }
  39. Dictionary<Type, FasterList<INode>> _nodesDB;
  40. Dictionary<Type, Dictionary<int, INode>> _nodesDBdic;
  41. FasterReadOnlyList<INode> _defaultEmptyNodeList = new FasterReadOnlyList<INode>(new FasterList<INode>());
  42. ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  43. }
  44. }