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.

70 lines
1.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Svelto.ES
  4. {
  5. public sealed class EnginesRoot: INodeEnginesRoot
  6. {
  7. public EnginesRoot()
  8. {
  9. _nodeEngines = new Dictionary<Type, List<INodeEngine>>();
  10. }
  11. public void AddEngine(IEngine engine)
  12. {
  13. if (engine is INodeEngine)
  14. AddNodeEngine(engine as INodeEngine);
  15. }
  16. public void Add(INode node)
  17. {
  18. Type nodeType = node.GetType();
  19. List<INodeEngine> value;
  20. if (_nodeEngines.TryGetValue(nodeType, out value))
  21. for (int j = 0; j < value.Count; j++)
  22. value[j].Add(node);
  23. }
  24. public void Remove(INode node)
  25. {
  26. Type nodeType = node.GetType();
  27. List<INodeEngine> value;
  28. if (_nodeEngines.TryGetValue(nodeType, out value))
  29. for (int j = 0; j < value.Count; j++)
  30. value[j].Remove(node);
  31. }
  32. void AddNodeEngine(INodeEngine engine)
  33. {
  34. AddEngine(engine, engine.AcceptedNodes(), _nodeEngines);
  35. }
  36. void AddEngine<T>(T engine, Type[] types, Dictionary<Type, List<T>> engines)
  37. {
  38. for (int i = 0; i < types.Length; i++)
  39. {
  40. List<T> value;
  41. var type = types[i];
  42. if (engines.TryGetValue(type, out value) == false)
  43. {
  44. List<T> list = new List<T>();
  45. list.Add(engine);
  46. engines.Add(type, list);
  47. }
  48. else
  49. value.Add(engine);
  50. }
  51. }
  52. Dictionary<Type, List<INodeEngine>> _nodeEngines;
  53. }
  54. }