PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 1ms

/Engine/Game/ProcessManager.cs

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