Last active
September 8, 2018 03:25
-
-
Save GeorgiyRyaposov/5a9b5a802cc9807c25ac559ff078f1b4 to your computer and use it in GitHub Desktop.
Trigger actions\coroutines by order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Assets.Scripts.Domain.Systems | |
{ | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public class ActionsQueue : MonoBehaviour | |
{ | |
protected static ActionsQueue _instance; | |
public static ActionsQueue Instance | |
{ | |
get | |
{ | |
if (_instance != null) | |
{ | |
return _instance; | |
} | |
_instance = FindObjectOfType<ActionsQueue>(); | |
if (_instance != null) | |
{ | |
return _instance; | |
} | |
var actionsQueueGameObject = new GameObject("ActionsQueue"); | |
_instance = actionsQueueGameObject.AddComponent<ActionsQueue>(); | |
return _instance; | |
} | |
} | |
private SortedSet<QueueItem> _actionsQueue; | |
private bool _actionInProgress; | |
private void Awake() | |
{ | |
_actionsQueue = new SortedSet<QueueItem>(new ByOrder()); | |
} | |
public void AddAsFirstAction(IEnumerator action) | |
{ | |
var item = _actionsQueue.LastOrDefault(); | |
AddAction(action, item == null ? 1 : item.Order - 1); | |
} | |
public void AddAsFirstAction(System.Action action) | |
{ | |
var wrapper = ActionWrapper(action); | |
AddAsFirstAction(wrapper); | |
} | |
public void AddAction(IEnumerator action) | |
{ | |
var item = _actionsQueue.LastOrDefault(); | |
AddAction(action, item == null ? 1 : item.Order + 1); | |
} | |
public void AddAction(IEnumerator action, int order) | |
{ | |
_actionsQueue.Add(new QueueItem() { Action = action, Order = order }); | |
} | |
public void AddAction(System.Action action) | |
{ | |
var wrapper = ActionWrapper(action); | |
AddAction(wrapper); | |
} | |
public void AddAction(System.Action action, int order) | |
{ | |
var wrapper = ActionWrapper(action); | |
AddAction(wrapper, order); | |
} | |
private IEnumerator ActionWrapper(System.Action action) | |
{ | |
yield return null; | |
action(); | |
} | |
private void Update() | |
{ | |
if (_actionInProgress || _actionsQueue.Count == 0) | |
{ | |
return; | |
} | |
_actionInProgress = true; | |
var item = _actionsQueue.First(); | |
_actionsQueue.Remove(item); | |
StartCoroutine(ExecuteAction(item)); | |
} | |
private IEnumerator ExecuteAction(QueueItem item) | |
{ | |
yield return item.Action; | |
_actionInProgress = false; | |
} | |
protected class QueueItem | |
{ | |
public int Order; | |
public IEnumerator Action; | |
} | |
protected class ByOrder : IComparer<QueueItem> | |
{ | |
public int Compare(QueueItem x, QueueItem y) | |
{ | |
return x.Order.CompareTo(y.Order); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment