Advertisement
dmitryzenevich

Sequence-Parallel Collections

May 1st, 2020
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. namespace Test
  7. {
  8.     public class TestMonoBehaviour : MonoBehaviour
  9.     {
  10.         public readonly string Name;
  11.         public int Count => _actions.Count;
  12.  
  13.         private readonly Queue<QueueAction> _actions = new Queue<QueueAction>();
  14.  
  15.         public IEnumerator Execute()
  16.         {
  17.             var prepare = ColorUtility.ToHtmlStringRGB(Color.magenta);
  18.             var go = ColorUtility.ToHtmlStringRGB(Color.yellow);
  19.             Debug.Log($"<color=#{prepare}>PREPARE....</color>");
  20.             yield return new WaitForSeconds(2f);
  21.             Debug.Log($"<color=#{go}>GO!!!</color>");
  22.            
  23.            
  24.             var sequence = StartCoroutine(_actions.Where(action => action.IsParallel == false)
  25.                 .Select(action => action.Execute()).GetEnumerator());
  26.  
  27.             var parallel = StartCoroutine(_actions.Where(action => action.IsParallel == true)
  28.                 .Select(action => StartCoroutine(action.Execute())).ToList().GetEnumerator());
  29.  
  30.             yield return sequence;
  31.             yield return parallel;
  32.         }
  33.  
  34.         private void Awake()
  35.         {
  36.             for (int i = 0; i < 4; i++)
  37.             {
  38.                 _actions.Enqueue(new QueueAction($"{nameof(Method)}{i}") {LongAction = Method(3, $"I'm parallel{i}, YES", Color.green), IsParallel = true});
  39.                 _actions.Enqueue(new QueueAction($"{nameof(Method)}{i}") {LongAction = Method(3, $"I'm NOT parallel{i}", Color.red), IsParallel = false});
  40.             }
  41.         }
  42.  
  43.         private IEnumerator Start()
  44.         {
  45.             Debug.Log("<color=#FFFF00>Start</color>");
  46.             yield return StartCoroutine(Execute());
  47.             Debug.Log("<color=#FFFF00>Done</color>");
  48.         }
  49.         private IEnumerator Method(float sec, string message, Color color)
  50.         {
  51.             var name = ColorUtility.ToHtmlStringRGB(color);
  52.             var start = ColorUtility.ToHtmlStringRGB(Color.magenta);
  53.             var end = ColorUtility.ToHtmlStringRGB(Color.cyan);
  54.             Debug.Log($"<color=#{name}>{message}</color> - <color=#{start}>START</color>");
  55.            
  56.             yield return new WaitForSeconds(sec);
  57.            
  58.             Debug.Log($"<color=#{name}>{message}</color> - <color=#{end}>END</color>");
  59.         }
  60.     }
  61. }
  62.  
  63. using System;
  64. using System.Collections;
  65. using System.Collections.Generic;
  66. using UnityEngine;
  67.  
  68. [Serializable]
  69. public class QueueAction
  70. {
  71.     public readonly string Name;
  72.     public Action FastAction;
  73.     public IEnumerator LongAction;
  74.     public bool IsParallel = false;
  75.  
  76.     public QueueAction(string name)
  77.     {
  78.         Debug.Log($"Create action {name}");
  79.         Name = name;
  80.     }
  81.  
  82.     public IEnumerator Execute()
  83.     {
  84.         Debug.Log($"Start action {Name}");
  85.         if (FastAction != null)
  86.         {
  87.             FastAction.Invoke();
  88.         }
  89.  
  90.         if (LongAction != null)
  91.         {
  92.             yield return LongAction;
  93.         }
  94.  
  95.         Debug.Log($"End action {Name}");
  96.         yield return null;
  97.     }
  98.  
  99.     public QueueAction Clone()
  100.     {
  101.         return new QueueAction(Name)
  102.         {
  103.             FastAction = FastAction,
  104.             LongAction = LongAction,
  105.             IsParallel = IsParallel,
  106.         };
  107.     }
  108. }
  109.  
  110. public class CoroutineQueue
  111. {
  112.     public readonly string Name;
  113.  
  114.     public int Count => _actions.Count;
  115.  
  116.     private readonly Queue<QueueAction> _actions = new Queue<QueueAction>();
  117.  
  118.     public CoroutineQueue(string name)
  119.     {
  120.         Debug.Log($"Create queue {name}");
  121.         Name = name;
  122.     }
  123.  
  124.     private void AppendAction(QueueAction action)
  125.     {
  126.         Debug.Log($"Queue {Name}. Append action {action.Name}");
  127.         _actions.Enqueue(action);
  128.     }
  129.  
  130. //      public void AppendAction(string name, Action fastAction, IEnumerator longAction = null) =>
  131. //          AppendAction(new QueueAction(name){
  132. //              FastAction = fastAction,
  133. //              LongAction = longAction,
  134. //          });
  135.  
  136.     public void AppendAction(string name, Action fastAction, bool isParallel = false) =>
  137.         AppendAction(new QueueAction(name)
  138.         {
  139.             FastAction = fastAction,
  140.             IsParallel = isParallel,
  141.         });
  142.  
  143.     public void AppendAction(string name, IEnumerator longAction, bool isParallel = false) =>
  144.         AppendAction(new QueueAction(name)
  145.         {
  146.             LongAction = longAction,
  147.             IsParallel = isParallel,
  148.         });
  149.  
  150.     public void AppendQueue(CoroutineQueue queue, bool isParallel = false)
  151.     {
  152.         Debug.Log($"Queue {Name}. Append queue {queue.Name}");
  153.  
  154.         bool isFirst = true;
  155.         foreach (QueueAction queueAction in queue._actions)
  156.         {
  157.             if (isFirst)
  158.             {
  159.                 isFirst = false;
  160.                 QueueAction firstAction = queueAction.Clone();
  161.                 firstAction.IsParallel = isParallel;
  162.                 _actions.Enqueue(firstAction);
  163.             }
  164.             else
  165.             {
  166.                 _actions.Enqueue(queueAction);
  167.             }
  168.         }
  169.     }
  170.  
  171.     public IEnumerator Execute()
  172.     {
  173. //          Debug.Log($"Start queue {Name}");
  174.         while (_actions.Count > 0)
  175.         {
  176.             List<QueueAction> parallelActions = new List<QueueAction>();
  177.             do
  178.             {
  179.                 parallelActions.Add(_actions.Dequeue());
  180.             } while (_actions.Count > 0 && _actions.Peek().IsParallel);
  181.  
  182.             if (parallelActions.Count > 0)
  183.             {
  184.                 if (parallelActions.Count == 1)
  185.                 {
  186.                     yield return parallelActions[0].Execute();
  187.                 }
  188.                 else
  189.                 {
  190.                     Debug.Log($"Queue {Name}. Start parallel actions");
  191.                     List<IEnumerator> parallels = new List<IEnumerator>();
  192.                     foreach (QueueAction parallelAction in parallelActions)
  193.                     {
  194.                         //                  Debug.Log($"Queue {Name}. Start action {parallelAction.Name}");
  195.                         parallels.Add(parallelAction.Execute());
  196.                         //                  Debug.Log($"Queue {Name}. End action {parallelAction.Name}");
  197.                     }
  198.  
  199.                     foreach (IEnumerator parallel in parallels)
  200.                     {
  201.                         yield return parallel; //ВЫПОЛНЯЕТСЯ ПОСЛЕДОВАТЕЛЬНО !!!!!!!!!!!
  202.                     }
  203.  
  204.                     Debug.Log($"Queue {Name}. End parallel actions");
  205.                 }
  206.             }
  207.         }
  208.  
  209.         yield return null;
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement