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.

95 lines
3.4KB

  1. using Svelto.Common;
  2. using Svelto.DataStructures;
  3. namespace Svelto.ECS
  4. {
  5. /// <summary>
  6. /// UnsortedEnginesGroup is a practical way to group engines that can be ticked together. As the name suggest
  7. /// there is no way to defines an order, although the engines will run in the same order they are added.
  8. /// It is abstract and it requires a user defined interface to push the user to use recognisable names meaningful
  9. /// to the context where they are used.
  10. /// </summary>
  11. /// <typeparam name="Interface">user defined interface that implements IStepEngine</typeparam>
  12. public abstract class UnsortedEnginesGroup<Interface> : IStepGroupEngine
  13. where Interface : IStepEngine
  14. {
  15. protected UnsortedEnginesGroup()
  16. {
  17. _name = "UnsortedEnginesGroup - "+this.GetType().Name;
  18. _instancedSequence = new FasterList<Interface>();
  19. }
  20. protected UnsortedEnginesGroup(FasterList<Interface> engines)
  21. {
  22. _name = "UnsortedEnginesGroup - "+this.GetType().Name;
  23. _instancedSequence = engines;
  24. }
  25. public void Step()
  26. {
  27. var sequenceItems = _instancedSequence;
  28. using (var profiler = new PlatformProfiler(_name))
  29. {
  30. for (var index = 0; index < sequenceItems.count; index++)
  31. {
  32. var engine = sequenceItems[index];
  33. using (profiler.Sample(engine.name)) engine.Step();
  34. }
  35. }
  36. }
  37. public void Add(Interface engine)
  38. {
  39. _instancedSequence.Add(engine);
  40. }
  41. public string name => _name;
  42. readonly string _name;
  43. readonly FasterList<Interface> _instancedSequence;
  44. }
  45. /// <summary>
  46. /// Similar to UnsortedEnginesGroup except for the fact that an optional parameter can be passed to the engines
  47. /// </summary>
  48. /// <typeparam name="Interface"></typeparam>
  49. /// <typeparam name="Parameter">Specialised Parameter that can be passed to all the engines in the group</typeparam>
  50. public abstract class UnsortedEnginesGroup<Interface, Parameter> : IStepGroupEngine<Parameter>
  51. where Interface : IStepEngine<Parameter>
  52. {
  53. protected UnsortedEnginesGroup()
  54. {
  55. _name = "UnsortedEnginesGroup - "+this.GetType().Name;
  56. _instancedSequence = new FasterList<Interface>();
  57. }
  58. protected UnsortedEnginesGroup(FasterList<Interface> engines)
  59. {
  60. _name = "UnsortedEnginesGroup - "+this.GetType().Name;
  61. _instancedSequence = engines;
  62. }
  63. public void Step(in Parameter param)
  64. {
  65. var sequenceItems = _instancedSequence;
  66. using (var profiler = new PlatformProfiler(_name))
  67. {
  68. for (var index = 0; index < sequenceItems.count; index++)
  69. {
  70. var engine = sequenceItems[index];
  71. using (profiler.Sample(engine.name)) engine.Step(param);
  72. }
  73. }
  74. }
  75. public void Add(Interface engine)
  76. {
  77. _instancedSequence.Add(engine);
  78. }
  79. public string name => _name;
  80. readonly string _name;
  81. readonly FasterList<Interface> _instancedSequence;
  82. }
  83. }