/// <summary> /// Queue a command to instantiate a GameObject /// </summary> /// <param name="name"> /// Name of the GameObject. Must not be null. /// </param> /// <param name="result"> /// Result to be filled in when the command executes. Must not be null. /// </param> public void NewGameObject( string name, Result <GameObject> result) { Assert.IsTrue(name != null); Assert.IsTrue(result != null); result.Reset(); NewGameObjectCommand cmd = GetFromPool(newGameObjectPool); cmd.Name = name; cmd.Result = result; QueueCommand(cmd); }
/// <summary> /// Execute commands until there are none left or a maximum time is used /// </summary> /// <param name="maxMilliseconds"> /// Maximum number of milliseconds to execute for. Must be positive. /// </param> public void Execute(int maxMilliseconds = int.MaxValue) { Assert.IsTrue(maxMilliseconds > 0); // Process commands until we run out of time executeLimitStopwatch.Reset(); executeLimitStopwatch.Start(); while (executeLimitStopwatch.ElapsedMilliseconds < maxMilliseconds) { // Get the next queued command, but stop if the queue is empty BaseCommand baseCmd; lock (commandQueue) { if (commandQueue.Count == 0) { break; } baseCmd = commandQueue.Dequeue(); } // Process the command. These steps are followed for each command: // 1. Extract the command's fields // 2. Reset the command's fields // 3. Do the work // 4. Return the command to its pool // 5. Make the result ready switch (baseCmd.Type) { case CommandType.NewGameObject: { // Extract the command's fields NewGameObjectCommand cmd = (NewGameObjectCommand)baseCmd; string name = cmd.Name; Result <GameObject> result = cmd.Result; // Reset the command's fields cmd.Name = null; cmd.Result = null; // Return the command to its pool ReturnToPool(newGameObjectPool, cmd); // Do the work GameObject go = new GameObject(name); // Make the result ready result.Ready(go); break; } case CommandType.GetTransform: { // Extract the command's fields GetTransformCommand cmd = (GetTransformCommand)baseCmd; GameObject go = cmd.GameObject; Result <Transform> result = cmd.Result; // Reset the command's fields cmd.GameObject = null; cmd.Result = null; // Return the command to its pool ReturnToPool(getTransformPool, cmd); // Do the work Transform transform = go.transform; // Make the result ready result.Ready(transform); break; } case CommandType.SetPosition: { // Extract the command's fields SetPositionCommand cmd = (SetPositionCommand)baseCmd; Transform transform = cmd.Transform; Vector3 position = cmd.Position; Result result = cmd.Result; // Reset the command's fields cmd.Transform = null; cmd.Position = Vector3.zero; cmd.Result = null; // Return the command to its pool ReturnToPool(setPositionPool, cmd); // Do the work transform.position = position; // Make the result ready result.Ready(); break; } } } }