PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Engine/Game/ProcessManager.cs

#
C# | 109 lines | 71 code | 11 blank | 27 comment | 11 complexity | 3eb2f4302bcbebcce301046b46fbb5a7 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Engine.Dynamic;
  3. using Delta.InputSystem;
  4. namespace Delta.Engine.Game
  5. {
  6. /// <summary>
  7. /// The process manager has a list of all processes and runs them.
  8. /// This simplifies the logic and optimizes performance a lot for each
  9. /// of the process classes, which are only run if active.
  10. /// </summary>
  11. internal class ProcessManager : DynamicModule
  12. {
  13. #region Internal
  14. #region Instance (Internal)
  15. /// <summary>
  16. /// Helper to make sure to only create the ProcessManager once and via
  17. /// the Factory to be registered as a DynamicModule.
  18. /// </summary>
  19. internal static ProcessManager Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. {
  25. instance = Factory.Create<ProcessManager>();
  26. }
  27. return instance;
  28. }
  29. }
  30. #endregion
  31. #region processes (Internal)
  32. /// <summary>
  33. /// List of processes we want to run each tick. Some of them might not be
  34. /// active and will be skipped. This list can change at any time.
  35. /// </summary>
  36. internal List<BaseProcess> processes = new List<BaseProcess>();
  37. #endregion
  38. #endregion
  39. #region Private
  40. #region instance (Private)
  41. /// <summary>
  42. /// Remember the created ProcessManager instance
  43. /// </summary>
  44. private static ProcessManager instance;
  45. #endregion
  46. #endregion
  47. #region Constructors
  48. /// <summary>
  49. /// Create process manager, only allowed internally via the Instance
  50. /// property. Done automatically when creating processes.
  51. /// </summary>
  52. private ProcessManager()
  53. : base("ProcessManager", typeof(Input))
  54. {
  55. }
  56. #endregion
  57. #region Run (Public)
  58. /// <summary>
  59. /// Run goes through all processes and runs them if they are active.
  60. /// </summary>
  61. public override void Run()
  62. {
  63. // No need to go any further if there are no more processes alive
  64. if (processes.Count == 0)
  65. {
  66. return;
  67. }
  68. List<BaseProcess> removeProcesses = null;
  69. foreach (BaseProcess process in processes)
  70. {
  71. // Only run active processes.
  72. if (process.IsActive &&
  73. process.Run())
  74. {
  75. if (removeProcesses == null)
  76. {
  77. removeProcesses = new List<BaseProcess>();
  78. }
  79. removeProcesses.Add(process);
  80. // If we have a next process after this, activate it.
  81. if (process.Next != null)
  82. {
  83. process.Next.IsActive = true;
  84. }
  85. }
  86. }
  87. // Remove all processes that are done
  88. if (removeProcesses != null)
  89. {
  90. foreach (BaseProcess processToRemove in removeProcesses)
  91. {
  92. processes.Remove(processToRemove);
  93. }
  94. }
  95. }
  96. #endregion
  97. }
  98. }