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.

99 lines
2.6KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Svelto.ECS
  5. {
  6. public class Steps : Dictionary<IEngine, IDictionary>
  7. {}
  8. public class To : Dictionary<int, IStep[]>
  9. {
  10. public void Add(IStep engine)
  11. {
  12. Add(Condition.Always, new [] {engine});
  13. }
  14. public void Add(IStep[] engines)
  15. {
  16. Add(Condition.Always, engines);
  17. }
  18. }
  19. public class To<C> : Dictionary<C, IStep[]> where C:struct,IConvertible
  20. {}
  21. public interface IStep
  22. {}
  23. public interface IStep<T>:IStep
  24. {
  25. void Step(ref T token, int condition);
  26. }
  27. public interface IStep<T, in C>:IStep where C:struct,IConvertible
  28. {
  29. void Step(ref T token, C condition);
  30. }
  31. public interface IEnumStep<T>:IStep
  32. {
  33. void Step(ref T token, Enum condition);
  34. }
  35. public interface ISequencer
  36. {
  37. void Next<T>(IEngine engine, ref T param);
  38. void Next<T>(IEngine engine, ref T param, int condition);
  39. void Next<T, C>(IEngine engine, ref T param, C condition) where C : struct, IConvertible;
  40. }
  41. public class Sequencer : ISequencer
  42. {
  43. public void SetSequence(Steps steps)
  44. {
  45. _steps = steps;
  46. }
  47. public void Next<T>(IEngine engine, ref T param)
  48. {
  49. Next(engine, ref param, Condition.Always);
  50. }
  51. public void Next<T>(IEngine engine, ref T param, int condition)
  52. {
  53. int branch = condition;
  54. var steps = (_steps[engine] as Dictionary<int, IStep[]>)[branch];
  55. if (steps != null)
  56. for (int i = 0; i < steps.Length; i++)
  57. ((IStep<T>)steps[i]).Step(ref param, condition);
  58. }
  59. public void Next<T>(IEngine engine, ref T param, Enum condition)
  60. {
  61. int branch = Convert.ToInt32(condition);
  62. var steps = (_steps[engine] as Dictionary<int, IStep[]>)[branch];
  63. if (steps != null)
  64. for (int i = 0; i < steps.Length; i++)
  65. ((IEnumStep<T>)steps[i]).Step(ref param, condition);
  66. }
  67. public void Next<T, C>(IEngine engine, ref T param, C condition) where C:struct,IConvertible
  68. {
  69. C branch = condition;
  70. var steps = (_steps[engine] as Dictionary<C, IStep[]>)[branch];
  71. if (steps != null)
  72. for (int i = 0; i < steps.Length; i++)
  73. ((IStep<T, C>)steps[i]).Step(ref param, condition);
  74. }
  75. Steps _steps;
  76. }
  77. public static class Condition
  78. {
  79. public const int Always = 0;
  80. }
  81. }