2020-06-13 02:58:54 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Coroutine {
|
2020-06-13 03:12:26 +02:00
|
|
|
/// <summary>
|
2021-03-20 15:13:39 +01:00
|
|
|
/// Represents either an amount of time, or an <see cref="Coroutine.Event"/> that is being waited for by an <see cref="ActiveCoroutine"/>.
|
2020-06-13 03:12:26 +02:00
|
|
|
/// </summary>
|
2020-06-13 02:58:54 +02:00
|
|
|
public struct Wait {
|
|
|
|
|
2021-03-20 15:13:39 +01:00
|
|
|
internal readonly Event Event;
|
2020-06-13 02:58:54 +02:00
|
|
|
private double seconds;
|
|
|
|
|
2020-06-13 03:12:26 +02:00
|
|
|
/// <summary>
|
2021-03-20 15:13:39 +01:00
|
|
|
/// Creates a new wait that waits for the given <see cref="Coroutine.Event"/>.
|
2020-06-13 03:12:26 +02:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="evt">The event to wait for</param>
|
2020-06-13 02:58:54 +02:00
|
|
|
public Wait(Event evt) {
|
2021-03-20 15:13:39 +01:00
|
|
|
this.Event = evt;
|
2020-06-13 02:58:54 +02:00
|
|
|
this.seconds = 0;
|
|
|
|
}
|
|
|
|
|
2020-06-13 03:12:26 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new wait that waits for the given amount of seconds.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="seconds">The amount of seconds to wait for</param>
|
2020-06-13 02:58:54 +02:00
|
|
|
public Wait(double seconds) {
|
|
|
|
this.seconds = seconds;
|
2021-03-20 15:13:39 +01:00
|
|
|
this.Event = null;
|
2020-06-13 02:58:54 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 03:12:26 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new wait that waits for the given <see cref="TimeSpan"/>.
|
|
|
|
/// Note that the exact value may be slightly different, since waits operate in <see cref="TimeSpan.TotalSeconds"/> rather than ticks.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="time">The time span to wait for</param>
|
2020-06-13 02:58:54 +02:00
|
|
|
public Wait(TimeSpan time) : this(time.TotalSeconds) {
|
|
|
|
}
|
|
|
|
|
|
|
|
internal bool Tick(double deltaSeconds) {
|
|
|
|
this.seconds -= deltaSeconds;
|
|
|
|
return this.seconds <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|