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
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
private readonly IEnumerator<Wait> enumerator;
|
|
|
|
private Wait current;
|
|
|
|
|
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-06-13 02:58:54 +02:00
|
|
|
internal ActiveCoroutine(IEnumerator<Wait> enumerator) {
|
2019-06-22 17:24:50 +02:00
|
|
|
this.enumerator = enumerator;
|
|
|
|
}
|
|
|
|
|
2020-03-26 02:30:29 +01:00
|
|
|
public bool Cancel() {
|
|
|
|
if (this.IsFinished || this.WasCanceled)
|
|
|
|
return false;
|
|
|
|
this.WasCanceled = true;
|
|
|
|
this.IsFinished = true;
|
|
|
|
this.OnFinished?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-28 22:27:38 +01:00
|
|
|
internal bool Tick(double deltaSeconds) {
|
2020-03-26 02:30:29 +01:00
|
|
|
if (!this.WasCanceled) {
|
2020-06-13 02:58:54 +02:00
|
|
|
if (this.current.Tick(deltaSeconds))
|
2020-03-26 02:30:29 +01:00
|
|
|
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) {
|
2020-03-26 02:30:29 +01:00
|
|
|
if (!this.WasCanceled) {
|
2020-06-13 02:58:54 +02:00
|
|
|
if (this.current.OnEvent(evt))
|
2020-03-26 02:30:29 +01:00
|
|
|
this.MoveNext();
|
|
|
|
}
|
2020-02-28 22:27:38 +01:00
|
|
|
return this.IsFinished;
|
2019-06-22 17:24:50 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
internal bool MoveNext() {
|
2020-03-26 02:30:29 +01:00
|
|
|
if (!this.enumerator.MoveNext()) {
|
|
|
|
this.IsFinished = true;
|
|
|
|
this.OnFinished?.Invoke(this);
|
2020-06-13 02:58:54 +02:00
|
|
|
return false;
|
2020-03-26 02:30:29 +01:00
|
|
|
}
|
2020-06-13 02:58:54 +02:00
|
|
|
this.current = this.enumerator.Current;
|
|
|
|
return true;
|
2020-03-22 22:28:19 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
internal bool IsWaitingForEvent() {
|
|
|
|
return this.current.IsWaitingForEvent();
|
2019-06-22 17:24:50 +02:00
|
|
|
}
|
|
|
|
|
2020-03-26 02:30:29 +01:00
|
|
|
public delegate void FinishCallback(ActiveCoroutine coroutine);
|
2020-03-22 22:28:19 +01:00
|
|
|
|
2019-06-22 17:24:50 +02:00
|
|
|
}
|
|
|
|
}
|