/// <summary> /// Adds an <see cref="IEntitySystem"/> to the manager with the specified <see cref="SystemExecutionType"/>and priority. /// </summary> /// <typeparam name="T">Type of <see cref="IEntitySystem"/> being added.</typeparam> /// <param name="system"><see cref="IEntitySystem"/> instance to add.</param> /// <param name="executionType">Type of execution.</param> /// <param name="priority">Execution priority. Systems with lower priority values are executed first.</param> /// <returns>The <see cref="IEntitySystem"/> instance passed, with the system bit flag set.</returns> public T SetSystem <T>(T system, SystemExecutionType executionType, int priority) where T : IEntitySystem { system.World = _world; _systems.Add(typeof(T), system); if (executionType == SystemExecutionType.Draw) { if (!_drawLayers.ContainsKey(priority)) { _drawLayers.Add(priority, new List <IEntitySystem>()); } List <IEntitySystem> drawBag = _drawLayers[priority]; if (drawBag == null) { drawBag = new List <IEntitySystem>(); _drawLayers[priority] = drawBag; } if (!drawBag.Contains(system)) { drawBag.Add(system); } } else if (executionType == SystemExecutionType.Update) { if (!_updateLayers.ContainsKey(priority)) { _updateLayers.Add(priority, new List <IEntitySystem>()); } List <IEntitySystem> updateBag = _updateLayers[priority]; if (updateBag == null) { updateBag = new List <IEntitySystem>(); _updateLayers[priority] = updateBag; } if (!updateBag.Contains(system)) { updateBag.Add(system); } } if (!_mergedBag.Contains(system)) { _mergedBag.Add(system); } system.SystemBit = SystemBitManager.GetBitFor(system); return(system); }
/// <summary> /// Asynchronously updates all <see cref="IEntitySystem"/> instances of the specified execution type. /// </summary> /// <param name="executionType">Execution type of systems to update.</param> public void UpdateAsynchronous(SystemExecutionType executionType) { if (executionType == SystemExecutionType.Draw) { foreach (List <IEntitySystem> bag in _drawLayers.Values) { UpdateBagAsync(bag); } } else if (executionType == SystemExecutionType.Update) { foreach (List <IEntitySystem> bag in _updateLayers.Values) { UpdateBagAsync(bag); } } }
private void AddSystemTo(ref SystemLayer[] layers, EntitySystem system, int layerIndex, SystemExecutionType executionType) { Debug.Assert(layers != null); _dummyLayer.LayerIndex = layerIndex; var index = Array.BinarySearch(layers, _dummyLayer); SystemLayer layer; if (index >= 0) { layer = layers[index]; } else { layer = new SystemLayer(layerIndex); Array.Resize(ref layers, layers.Length + 1); layers[layers.Length - 1] = layer; Array.Sort(layers); } switch (executionType) { case SystemExecutionType.Synchronous: layer.SynchronousSystems.Add(system); break; //case SystemExecutionType.Asynchronous: // layer.AsynchronousSystems.Attach(system); // break; default: throw new ArgumentOutOfRangeException(nameof(executionType), executionType, null); } }
private EntitySystem AddSystem(Type systemType, EntitySystem system, GameLoopType gameLoopType, int layer, SystemExecutionType executionType) { Debug.Assert(systemType != null); Debug.Assert(system != null); if (Systems.Contains(system)) { throw new InvalidOperationException($"System '{systemType}' has already been added."); } system.Manager = _manager; Systems.Add(system); var processingSystem = system as EntityProcessingSystem; if (processingSystem != null) { processingSystem.Index = ProcessingSystems.Count; ProcessingSystems.Add(processingSystem); } switch (gameLoopType) { case GameLoopType.Draw: AddSystemTo(ref _drawLayers, system, layer, executionType); break; case GameLoopType.Update: AddSystemTo(ref _updateLayers, system, layer, executionType); break; default: throw new ArgumentOutOfRangeException(nameof(gameLoopType), gameLoopType, null); } return(system); }
//// ReSharper disable once ParameterTypeCanBeEnumerable.Local //private static void ProcessSystemsAsynchronous(Bag<System> systems) //{ // // block // // does not garantee async... // Parallel.ForEach(systems, system => system.ProcessInternal()); //} internal T AddSystem <T>(T system, GameLoopType gameLoopType, int layer, SystemExecutionType executionType) where T : EntitySystem { Debug.Assert(system != null); return((T)AddSystem(system.GetType(), system, gameLoopType, layer, executionType)); }
/// <summary> /// Adds an <see cref="IEntitySystem"/> to the manager with the specified <see cref="SystemExecutionType"/> at the default priority. /// </summary> /// <typeparam name="T">Type of <see cref="IEntitySystem"/> being added.</typeparam> /// <param name="system"><see cref="IEntitySystem"/> instance to add.</param> /// <param name="executionType">Type of execution.</param> /// <returns>The <see cref="IEntitySystem"/> instance passed, with the system bit flag set.</returns> public T SetSystem <T>(T system, SystemExecutionType executionType) where T : IEntitySystem { return(SetSystem <T>(system, executionType, 0)); }
/// <summary> /// Sets the <see cref="SystemExecutionType"/> of the group. /// </summary> /// <param name="executionType">The execution type the system should follow.</param> /// <returns></returns> public SystemGroup SetExecutionType(SystemExecutionType executionType) { Execution = executionType; return(this); }
/// <summary> /// Creates an object which stores an array of <see cref="MorroSystem"/>'s, and provides functionality to execute said systems. /// </summary> /// <param name="capacity">The total amount of systems allowed in this group.</param> public SystemGroup(int capacity) { Systems = new MorroSystem[capacity]; Execution = SystemExecutionType.Synchronous; }