/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
- using System.Collections.Generic;
- using Delta.Engine.Dynamic;
- using Delta.InputSystem;
-
- namespace Delta.Engine.Game
- {
- /// <summary>
- /// The process manager has a list of all processes and runs them.
- /// This simplifies the logic and optimizes performance a lot for each
- /// of the process classes, which are only run if active.
- /// </summary>
- internal class ProcessManager : DynamicModule
- {
- #region Internal
-
- #region Instance (Internal)
- /// <summary>
- /// Helper to make sure to only create the ProcessManager once and via
- /// the Factory to be registered as a DynamicModule.
- /// </summary>
- internal static ProcessManager Instance
- {
- get
- {
- if (instance == null)
- {
- instance = Factory.Create<ProcessManager>();
- }
- return instance;
- }
- }
- #endregion
-
- #region processes (Internal)
- /// <summary>
- /// List of processes we want to run each tick. Some of them might not be
- /// active and will be skipped. This list can change at any time.
- /// </summary>
- internal List<BaseProcess> processes = new List<BaseProcess>();
- #endregion
-
- #endregion
-
- #region Private
-
- #region instance (Private)
- /// <summary>
- /// Remember the created ProcessManager instance
- /// </summary>
- private static ProcessManager instance;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create process manager, only allowed internally via the Instance
- /// property. Done automatically when creating processes.
- /// </summary>
- private ProcessManager()
- : base("ProcessManager", typeof(Input))
- {
- }
- #endregion
-
- #region Run (Public)
- /// <summary>
- /// Run goes through all processes and runs them if they are active.
- /// </summary>
- public override void Run()
- {
- // No need to go any further if there are no more processes alive
- if (processes.Count == 0)
- {
- return;
- }
-
- List<BaseProcess> removeProcesses = null;
- foreach (BaseProcess process in processes)
- {
- // Only run active processes.
- if (process.IsActive &&
- process.Run())
- {
- if (removeProcesses == null)
- {
- removeProcesses = new List<BaseProcess>();
- }
- removeProcesses.Add(process);
- // If we have a next process after this, activate it.
- if (process.Next != null)
- {
- process.Next.IsActive = true;
- }
- }
- }
-
- // Remove all processes that are done
- if (removeProcesses != null)
- {
- foreach (BaseProcess processToRemove in removeProcesses)
- {
- processes.Remove(processToRemove);
- }
- }
- }
- #endregion
- }
- }