Mirror of Svelto.ECS because we're a fan of it
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

119 行
4.1KB

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