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 2.0KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 FasterReadOnlyListCast<INode, T> QueryNodes<T>() where T:INode
  14. {
  15. var type = typeof(T);
  16. if (_nodesDB.ContainsKey(type) == false)
  17. return new FasterReadOnlyListCast<INode, T>(FasterReadOnlyListCast<INode, T>.DefaultList);
  18. return new FasterReadOnlyListCast<INode, T>(_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. public T QueryNode<T>(int ID) where T:INode
  40. {
  41. var type = typeof(T);
  42. INode internalNode;
  43. if (_nodesDBdic.ContainsKey(type) && _nodesDBdic[type].TryGetValue(ID, out internalNode))
  44. return (T)internalNode;
  45. throw new Exception("Node Not Found");
  46. }
  47. Dictionary<Type, FasterList<INode>> _nodesDB;
  48. Dictionary<Type, Dictionary<int, INode>> _nodesDBdic;
  49. ReadOnlyDictionary<int, INode> _defaultEmptyNodeDict = new ReadOnlyDictionary<int, INode>(new Dictionary<int, INode>());
  50. }
  51. }