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