mirror of
https://github.com/Ellpeck/Coroutine.git
synced 2024-11-21 21:33:29 +01:00
allow coroutine handler instancing. Actually solves #2
This commit is contained in:
parent
b4874dd365
commit
f199112d99
2 changed files with 72 additions and 37 deletions
|
@ -5,60 +5,30 @@ using System.Linq;
|
|||
namespace Coroutine {
|
||||
public static class CoroutineHandler {
|
||||
|
||||
private static readonly List<ActiveCoroutine> TickingCoroutines = new List<ActiveCoroutine>();
|
||||
private static readonly List<ActiveCoroutine> EventCoroutines = new List<ActiveCoroutine>();
|
||||
private static readonly CoroutineHandlerInstance Instance = new CoroutineHandlerInstance();
|
||||
|
||||
public static ActiveCoroutine Start(IEnumerable<Wait> coroutine) {
|
||||
return Start(coroutine.GetEnumerator());
|
||||
return Instance.Start(coroutine);
|
||||
}
|
||||
|
||||
public static ActiveCoroutine Start(IEnumerator<Wait> coroutine) {
|
||||
var inst = new ActiveCoroutine(coroutine);
|
||||
if (inst.MoveNext()) {
|
||||
if (inst.IsWaitingForEvent()) {
|
||||
EventCoroutines.Add(inst);
|
||||
} else {
|
||||
TickingCoroutines.Add(inst);
|
||||
}
|
||||
}
|
||||
return inst;
|
||||
return Instance.Start(coroutine);
|
||||
}
|
||||
|
||||
public static void InvokeLater(Wait wait, Action action) {
|
||||
Start(InvokeLaterImpl(wait, action));
|
||||
Instance.InvokeLater(wait, action);
|
||||
}
|
||||
|
||||
public static void Tick(double deltaSeconds) {
|
||||
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = TickingCoroutines[i];
|
||||
if (coroutine.Tick(deltaSeconds)) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.IsWaitingForEvent()) {
|
||||
TickingCoroutines.RemoveAt(i);
|
||||
EventCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
Instance.Tick(deltaSeconds);
|
||||
}
|
||||
|
||||
public static void RaiseEvent(Event evt) {
|
||||
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = EventCoroutines[i];
|
||||
if (coroutine.OnEvent(evt)) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
} else if (!coroutine.IsWaitingForEvent()) {
|
||||
EventCoroutines.RemoveAt(i);
|
||||
TickingCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
Instance.RaiseEvent(evt);
|
||||
}
|
||||
|
||||
public static IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
|
||||
return TickingCoroutines.Concat(EventCoroutines);
|
||||
}
|
||||
|
||||
private static IEnumerator<Wait> InvokeLaterImpl(Wait wait, Action action) {
|
||||
yield return wait;
|
||||
action();
|
||||
return Instance.GetActiveCoroutines();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
65
Coroutine/CoroutineHandlerInstance.cs
Normal file
65
Coroutine/CoroutineHandlerInstance.cs
Normal file
|
@ -0,0 +1,65 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Coroutine {
|
||||
public class CoroutineHandlerInstance {
|
||||
|
||||
private readonly List<ActiveCoroutine> tickingCoroutines = new List<ActiveCoroutine>();
|
||||
private readonly List<ActiveCoroutine> eventCoroutines = new List<ActiveCoroutine>();
|
||||
|
||||
public ActiveCoroutine Start(IEnumerable<Wait> coroutine) {
|
||||
return this.Start(coroutine.GetEnumerator());
|
||||
}
|
||||
|
||||
public ActiveCoroutine Start(IEnumerator<Wait> coroutine) {
|
||||
var inst = new ActiveCoroutine(coroutine);
|
||||
if (inst.MoveNext()) {
|
||||
if (inst.IsWaitingForEvent()) {
|
||||
this.eventCoroutines.Add(inst);
|
||||
} else {
|
||||
this.tickingCoroutines.Add(inst);
|
||||
}
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
public void InvokeLater(Wait wait, Action action) {
|
||||
this.Start(InvokeLaterImpl(wait, action));
|
||||
}
|
||||
|
||||
public void Tick(double deltaSeconds) {
|
||||
for (var i = this.tickingCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = this.tickingCoroutines[i];
|
||||
if (coroutine.Tick(deltaSeconds)) {
|
||||
this.tickingCoroutines.RemoveAt(i);
|
||||
} else if (coroutine.IsWaitingForEvent()) {
|
||||
this.tickingCoroutines.RemoveAt(i);
|
||||
this.eventCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RaiseEvent(Event evt) {
|
||||
for (var i = this.eventCoroutines.Count - 1; i >= 0; i--) {
|
||||
var coroutine = this.eventCoroutines[i];
|
||||
if (coroutine.OnEvent(evt)) {
|
||||
this.eventCoroutines.RemoveAt(i);
|
||||
} else if (!coroutine.IsWaitingForEvent()) {
|
||||
this.eventCoroutines.RemoveAt(i);
|
||||
this.tickingCoroutines.Add(coroutine);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<ActiveCoroutine> GetActiveCoroutines() {
|
||||
return this.tickingCoroutines.Concat(this.eventCoroutines);
|
||||
}
|
||||
|
||||
private static IEnumerator<Wait> InvokeLaterImpl(Wait wait, Action action) {
|
||||
yield return wait;
|
||||
action();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue