2019-06-22 17:24:50 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Coroutine;
|
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
|
namespace Example {
|
2019-06-22 17:24:50 +02:00
|
|
|
|
internal static class Example {
|
|
|
|
|
|
|
|
|
|
private static readonly Event TestEvent = new Event();
|
|
|
|
|
|
|
|
|
|
public static void Main() {
|
2020-12-20 17:12:43 +01:00
|
|
|
|
var seconds = CoroutineHandler.Start(WaitSeconds(), "Awesome Waiting Coroutine");
|
2020-02-28 22:27:38 +01:00
|
|
|
|
CoroutineHandler.Start(PrintEvery10Seconds(seconds));
|
2019-06-22 17:24:50 +02:00
|
|
|
|
|
2020-05-19 16:06:38 +02:00
|
|
|
|
CoroutineHandler.Start(EmptyCoroutine());
|
|
|
|
|
|
2021-03-20 15:13:39 +01:00
|
|
|
|
CoroutineHandler.InvokeLater(new Wait(5), () => {
|
2019-11-20 11:30:28 +01:00
|
|
|
|
Console.WriteLine("Raising test event");
|
|
|
|
|
CoroutineHandler.RaiseEvent(TestEvent);
|
|
|
|
|
});
|
2021-03-18 16:38:34 +01:00
|
|
|
|
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("Example event received"));
|
2019-11-20 11:30:28 +01:00
|
|
|
|
|
2021-03-18 16:38:34 +01:00
|
|
|
|
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked after 'Example event received'"), priority: -5);
|
|
|
|
|
CoroutineHandler.InvokeLater(new Wait(TestEvent), () => Console.WriteLine("I am invoked before 'Example event received'"), priority: 2);
|
2021-03-16 19:07:28 +01:00
|
|
|
|
|
2019-06-22 17:24:50 +02:00
|
|
|
|
var lastTime = DateTime.Now;
|
|
|
|
|
while (true) {
|
|
|
|
|
var currTime = DateTime.Now;
|
2021-03-20 02:00:46 +01:00
|
|
|
|
CoroutineHandler.Tick(currTime - lastTime);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
lastTime = currTime;
|
|
|
|
|
Thread.Sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
|
private static IEnumerator<Wait> WaitSeconds() {
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("First thing " + DateTime.Now);
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(1);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("After 1 second " + DateTime.Now);
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(9);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("After 10 seconds " + DateTime.Now);
|
2021-03-17 00:38:44 +01:00
|
|
|
|
CoroutineHandler.Start(NestedCoroutine());
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(5);
|
2019-11-20 11:30:28 +01:00
|
|
|
|
Console.WriteLine("After 5 more seconds " + DateTime.Now);
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(10);
|
2019-11-20 11:30:28 +01:00
|
|
|
|
Console.WriteLine("After 10 more seconds " + DateTime.Now);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(20);
|
2020-02-28 22:27:38 +01:00
|
|
|
|
Console.WriteLine("First coroutine done");
|
2019-06-22 17:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
|
private static IEnumerator<Wait> PrintEvery10Seconds(ActiveCoroutine first) {
|
2019-06-22 17:24:50 +02:00
|
|
|
|
while (true) {
|
2020-06-13 02:58:54 +02:00
|
|
|
|
yield return new Wait(10);
|
2019-06-22 17:24:50 +02:00
|
|
|
|
Console.WriteLine("The time is " + DateTime.Now);
|
2020-02-28 22:27:38 +01:00
|
|
|
|
if (first.IsFinished) {
|
|
|
|
|
Console.WriteLine("By the way, the first coroutine has finished!");
|
2021-01-01 06:21:17 +01:00
|
|
|
|
Console.WriteLine($"{first.Name} data: {first.MoveNextCount} moves, " +
|
2021-03-16 19:07:28 +01:00
|
|
|
|
$"{first.TotalMoveNextTime.TotalMilliseconds} total time, " +
|
2021-03-21 17:54:21 +01:00
|
|
|
|
$"{first.LastMoveNextTime.TotalMilliseconds} last time");
|
2020-02-28 22:27:38 +01:00
|
|
|
|
Environment.Exit(0);
|
|
|
|
|
}
|
2019-06-22 17:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 02:58:54 +02:00
|
|
|
|
private static IEnumerator<Wait> EmptyCoroutine() {
|
2020-05-19 16:06:38 +02:00
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 00:38:44 +01:00
|
|
|
|
private static IEnumerable<Wait> NestedCoroutine() {
|
|
|
|
|
Console.WriteLine("I'm a coroutine that was started from another coroutine!");
|
|
|
|
|
yield return new Wait(5);
|
|
|
|
|
Console.WriteLine("It's been 5 seconds since a nested coroutine was started, yay!");
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-22 17:24:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|