public void AwaitYieldTest()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();

            var co = runner.Create(async() =>
            {
                counter.Inc();
                await AwaitableCoroutine.Yield();
                counter.Inc();
            });

            Assert.False(co.IsCompleted);
            Assert.Equal(0, counter.Count);
            Assert.False(co.IsCompleted);

            runner.Update();
            Assert.Equal(1, counter.Count);
            Assert.False(co.IsCompleted);

            runner.Update();
            Assert.Equal(2, counter.Count);
            Assert.True(co.IsCompleted);
        }
Example #2
0
        public static void Main(string[] _)
        {
            var runner = new CoroutineRunner();

            var coroutine = runner.Create(CreateCoroutine);

            Console.WriteLine("Started!");

            while (!coroutine.IsCompleted)
            {
                Console.WriteLine($"{s_count}");
                runner.Update();
            }

            Console.WriteLine("Finished!");
        }
Example #3
0
        public void RunAndThenWithLoop()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();

            var target = 5;

            var c = runner.Create(() => CreateCoroutine(target, counter));

            while (!c.IsCompleted)
            {
                runner.Update();
            }

            Assert.Equal(target, counter.Count);
        }
Example #4
0
        public void RunCoroutine()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();

            _ = runner.Create(() => GetCoroutine(counter));

            var i = 0;

            while (i < 3)
            {
                runner.Update();
                i++;
                Log($"i: {i}");
                Assert.Equal(i, counter.Count);
            }
        }
        public void CreateCoroutineTest()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();

            var co = runner.Create(async() =>
            {
                counter.Inc();
            });

            Assert.False(co.IsCompleted);
            Assert.Equal(0, counter.Count);

            runner.Update();
            Assert.Equal(1, counter.Count);
            Assert.True(co.IsCompleted);
        }
        public void WaitAll2Test()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
                                        AwaitableCoroutine.WaitAll(
                                            AwaitableCoroutine.DelayCount(0),
                                            AwaitableCoroutine.DelayCount(0)
                                            )
                                        );

            Assert.False(waitAll.IsCompleted);

            // completes children
            runner.Update();

            Assert.True(waitAll.IsCompleted);
        }
        public void WaitAllOfDelay0Test()
        {
            var runner = new CoroutineRunner();

            var waitAll = runner.Create(() =>
                                        AwaitableCoroutine.WaitAll(new AwaitableCoroutineBase[] {
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
                AwaitableCoroutine.DelayCount(0),
            })
                                        );

            Assert.False(waitAll.IsCompleted);

            runner.Update();
            Assert.True(waitAll.IsCompleted);
        }
Example #8
0
        public void RunAndThen()
        {
            var runner  = new CoroutineRunner();
            var counter = new Counter();
            var c       = runner.Create(() =>
                                        AwaitableCoroutine.DelayCount(2)
                                        .AndThen(() => AwaitableCoroutine.DelayCount(2).OnCompleted(() =>
            {
                counter.Inc();
                Log($"Count: {counter.Count}");
            }))
                                        );

            while (!c.IsCompleted)
            {
                runner.Update();
            }

            Assert.True(c.IsCompleted);

            Assert.Equal(1, counter.Count);
        }
        public void RunSelect()
        {
            var runner = new CoroutineRunner();

            var(c1, c2, c3) = runner.Context(() =>
            {
                var c1 = AwaitableCoroutine.DelayCount(0);
                var c2 = c1.SelectTo(2);
                var c3 = c2.Select(x => x * x);
                return(c1, c2, c3);
            });

            Assert.False(c1.IsCompleted);
            Assert.False(c2.IsCompleted);
            Assert.False(c3.IsCompleted);

            runner.Update();
            Assert.True(c1.IsCompleted);
            Assert.True(c2.IsCompleted);
            Assert.True(c3.IsCompleted);

            Assert.Equal(2, c2.Result);
            Assert.Equal(4, c3.Result);
        }
Example #10
0
        public static void Main(string[] args)
        {
            //Timer variables to run the update loop at 10 fps
            var         watch       = Stopwatch.StartNew();
            const float updateRate  = 1f / 10f;
            var         prevTime    = watch.ElapsedMilliseconds / 1000f;
            var         accumulator = 0f;

            //The little @ character's position
            var px = 0;
            var py = 0;

            //Routine to move horizontally
            IEnumerator <object?> MoveX(int amount, float stepTime)
            {
                var dir = amount > 0 ? 1 : -1;

                while (amount != 0)
                {
                    yield return(stepTime);

                    px     += dir;
                    amount -= dir;
                }
            }

            //Routine to move vertically
            IEnumerator <object?> MoveY(int amount, float stepTime)
            {
                var dir = amount > 0 ? 1 : -1;

                while (amount != 0)
                {
                    yield return(stepTime);

                    py     += dir;
                    amount -= dir;
                }
            }

            //Walk the little @ character on a path
            IEnumerator <object?> Movement()
            {
                //Walk normally
                yield return(MoveX(5, 0.25f));

                yield return(MoveY(5, 0.25f));

                //Walk slowly
                yield return(MoveX(2, 0.5f));

                yield return(MoveY(2, 0.5f));

                yield return(MoveX(-2, 0.5f));

                yield return(MoveY(-2, 0.5f));

                //Run fast
                yield return(MoveX(5, 0.1f));

                yield return(MoveY(5, 0.1f));
            }

            //Render a little map with the @ character in the console
            void DrawMap()
            {
                Console.Clear();
                for (var y = 0; y < 16; ++y)
                {
                    for (var x = 0; x < 16; ++x)
                    {
                        if (x == px && y == py)
                        {
                            Console.Write('@');
                        }
                        else
                        {
                            Console.Write('.');
                        }
                    }
                    Console.WriteLine();
                }
            }

            //Run the coroutine
            var runner = new CoroutineRunner();
            var moving = runner.Run(Movement());

            //Run the update loop until we've finished moving
            while (moving.IsRunning)
            {
                //Track time
                var currTime = watch.ElapsedMilliseconds / 1000f;
                accumulator += currTime - prevTime;
                prevTime     = currTime;

                //Update at our requested rate (10 fps)
                if (accumulator > updateRate)
                {
                    accumulator -= updateRate;
                    runner.Update(updateRate);
                    DrawMap();
                }
            }
        }
 public override void Update(GameTime gameTime)
 {
     runner.Update(gameTime.GetElapsedSeconds());
 }
Example #12
0
 public void Update() => _runner.Update(1 / Engine.FPS);