Coroutine/Coroutine/ActiveCoroutine.cs

57 lines
1.6 KiB
C#
Raw Normal View History

2020-03-22 22:28:19 +01:00
using System;
2019-06-22 17:24:50 +02:00
using System.Collections.Generic;
namespace Coroutine {
public class ActiveCoroutine {
2019-06-22 17:24:50 +02:00
private readonly IEnumerator<IWait> enumerator;
public bool IsFinished { get; private set; }
2020-03-22 22:28:19 +01:00
public bool WasCanceled { get; private set; }
public FinishCallback OnFinished;
2019-06-22 17:24:50 +02:00
internal ActiveCoroutine(IEnumerator<IWait> enumerator) {
2019-06-22 17:24:50 +02:00
this.enumerator = enumerator;
}
public bool Cancel() {
if (this.IsFinished || this.WasCanceled)
return false;
this.WasCanceled = true;
this.IsFinished = true;
this.OnFinished?.Invoke(this);
return true;
}
internal bool Tick(double deltaSeconds) {
if (!this.WasCanceled) {
var curr = this.enumerator.Current;
if (curr != null && curr.Tick(deltaSeconds))
this.MoveNext();
}
return this.IsFinished;
2019-06-22 17:24:50 +02:00
}
internal bool OnEvent(Event evt) {
if (!this.WasCanceled) {
var curr = this.enumerator.Current;
if (curr != null && curr.OnEvent(evt))
this.MoveNext();
}
return this.IsFinished;
2019-06-22 17:24:50 +02:00
}
2020-03-22 22:28:19 +01:00
private void MoveNext() {
if (!this.enumerator.MoveNext()) {
this.IsFinished = true;
this.OnFinished?.Invoke(this);
}
2020-03-22 22:28:19 +01:00
}
internal WaitType GetCurrentType() {
2019-06-22 17:24:50 +02:00
return this.enumerator.Current.GetWaitType();
}
public delegate void FinishCallback(ActiveCoroutine coroutine);
2020-03-22 22:28:19 +01:00
2019-06-22 17:24:50 +02:00
}
}