Beispiel #1
0
        public void CoroutineService_WaitForNumTicksAsync_ReturnTaskThatCompletesAfterNumTicks()
        {
            var coroutineService = new CoroutineService();

            Task task = coroutineService.WaitForNumTicksAsync(2);

            coroutineService.TickCoroutines();

            Assert.IsFalse(task.IsCompleted, "Task completed too early");

            coroutineService.TickCoroutines();

            Assert.IsTrue(task.IsCompleted, "Task did not complete");
        }
Beispiel #2
0
        public void CoroutineService_WaitUntil_CompletesCoroutineAfterPredicateReturnsFalse()
        {
            bool condition = true;

            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitUntil(() => condition));

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            Assert.IsFalse(didComplete, "Completed too early");

            condition = false;

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
Beispiel #3
0
        public void CoroutineService_StartCoroutine_WithTask_RunsTask()
        {
            Stopwatch sw = new Stopwatch();

            bool didComplete = false;

            var coroutineService = new CoroutineService();

            async Task DoSomethingAsync()
            {
                await coroutineService.WaitForSecondsAsync(0.1f);

                didComplete = true;
            }

            var task = DoSomethingAsync();

            // Start a coroutine
            coroutineService.StartCoroutine(task);

            coroutineService.SetTime(10);
            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Coroutine did not complete");
            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine was not removed from scheduled");
        }
Beispiel #4
0
        public void CoroutineService_StartCoroutine_RunsCoroutine_WithNestedCoroutine()
        {
            bool didCompleteInner  = false;
            bool didCompleteOutter = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutineInner()
            {
                yield return(null);

                didCompleteInner = true;
            }

            IEnumerator RunCoroutineOutter()
            {
                yield return(coroutineService.StartCoroutine(RunCoroutineInner()));

                didCompleteOutter = true;
            }

            coroutineService.StartCoroutine(RunCoroutineOutter());

            coroutineService.TickCoroutines();

            Assert.IsTrue(didCompleteInner, "Coroutine did not complete");
            Assert.IsTrue(didCompleteOutter, "Coroutine did not complete");
        }
Beispiel #5
0
 public void Tick()
 {
     UpdateCoroutineServiceTime();
     _coroutineService.TickCoroutines();
     _coroutineService.TickFixedCoroutines();
     _coroutineService.TickLateCoroutines();
 }
Beispiel #6
0
        public void CoroutineService_WaitUntilAsync_ReturnTaskThatCompletesAfterPredicateReturnsFalse()
        {
            var coroutineService = new CoroutineService();

            bool condition = false;

            Task task = coroutineService.WaitUntilAsync(() => condition);

            coroutineService.TickCoroutines();

            Assert.IsFalse(task.IsCompleted, "Task completed too early");

            condition = true;

            coroutineService.TickCoroutines();

            Assert.IsTrue(task.IsCompleted, "Task did not complete");
        }
Beispiel #7
0
        public void CoroutineService_WaitForNextTickAsync_ReturnTaskThatCompletesAfterNextTick()
        {
            var coroutineService = new CoroutineService();

            Task task = coroutineService.WaitForNextTickAsync();

            coroutineService.TickCoroutines();

            Assert.IsTrue(task.IsCompleted, "Task did not complete");
        }
Beispiel #8
0
        public void CoroutineService_StartCoroutine_RunsCoroutine_MultipleFrames()
        {
            bool didComplete = false;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                yield return(null);

                didComplete = true;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();
            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Coroutine did not complete");
        }
Beispiel #9
0
        public void CoroutineService_TickCoroutines_AdjustsTickCount()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNumTicks(2));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // 0->1
            coroutineService.TickCoroutines(); // 1->2

            coroutineService.TickCoroutines(); // 2->3, coroutine completes after 2 full ticks

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
Beispiel #10
0
        public void CoroutineService_StartCoroutine_RunsParallelCoroutine()
        {
            bool didComplete1 = false;
            bool didComplete2 = false;

            IEnumerator RunCoroutine1()
            {
                yield return(null);

                didComplete1 = true;
            }

            IEnumerator RunCoroutine2()
            {
                yield return(null);

                yield return(null);

                didComplete2 = true;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine1());
            coroutineService.StartCoroutine(RunCoroutine2());

            Assert.AreEqual(2, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete1, "Coroutine did not complete");
            Assert.AreEqual(1, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete2, "Coroutine did not complete");
            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine was not scheduled");
        }
Beispiel #11
0
        public void CoroutineService_StopCoroutine_StopsCoroutine()
        {
            bool didComplete = false;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                yield return(null);

                didComplete = true;
            }

            var coroutineService = new CoroutineService();
            var coroutine        = coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();
            coroutineService.StopCoroutine(coroutine);
            coroutineService.TickCoroutines();

            Assert.AreEqual(0, coroutineService.NumScheduledCoroutines, "Coroutine did not stop properly");
            Assert.IsFalse(didComplete, "Coroutine did not stop properly");
        }
Beispiel #12
0
        public void CoroutineService_SetRealTime_UpdatesCoroutineRealTime()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForSecondsRealtime(1f));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // Still waits

            Assert.IsFalse(isCompleted, "Coroutine completed too early");

            coroutineService.SetRealTime(1f);
            coroutineService.TickCoroutines(); // Completes

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
Beispiel #13
0
        public void CoroutineService_TickCoroutines_ThrowsException_WhenCoroutineThrowsInThatTick()
        {
            IEnumerator RunCoroutine()
            {
                yield return(null);

                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());

            Assert.Throws <InvalidOperationException>(() => coroutineService.TickCoroutines());
        }
Beispiel #14
0
        public void CoroutineService_SetNumTick_UpdatesCoroutineTickCount()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNumTicks(10));

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines(); // Still waits

            Assert.IsFalse(isCompleted, "Coroutine completed too early");

            coroutineService.SetNumTick(10);
            coroutineService.TickCoroutines(); // Completes

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
        public IEnumerator CoroutineService_WaitForSecondsRealtimeAsync_WaitForSecondsRealtime()
        {
            var   coroutineService = new CoroutineService();
            float delaySeconds     = 0.05f;
            float startTimeSeconds = Time.realtimeSinceStartup;

            coroutineService.SetRealTime(startTimeSeconds);
            Task task = coroutineService.WaitForSecondsRealtimeAsync(delaySeconds);

            while (!task.IsCompleted)
            {
                coroutineService.SetRealTime(Time.realtimeSinceStartup);
                coroutineService.TickCoroutines();
                yield return(null);
            }

            Assert.IsTrue(Time.realtimeSinceStartup >= startTimeSeconds + delaySeconds, "Not enough time has passed");
        }
Beispiel #16
0
        public void CoroutineService_WaitForNextTick_CompletesCoroutineAfterNextTicks()
        {
            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForNextTick());

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
Beispiel #17
0
        public void CoroutineService_WaitForSecondsRealtime_CompletesCoroutineAfterNumRealtimeSeconds()
        {
            bool didComplete = false;

            var coroutineService = new CoroutineService();

            IEnumerator RunCoroutine()
            {
                yield return(coroutineService.WaitForSecondsRealtime(0.1f));

                didComplete = true;
            };

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.SetRealTime(1);

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Did not complete");
        }
Beispiel #18
0
        public void CoroutineService_TickCoroutines_TicksScheduledCoroutines()
        {
            int numCompleted = 0;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                numCompleted++;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());
            coroutineService.StartCoroutine(RunCoroutine());

            Assert.AreEqual(2, coroutineService.NumScheduledCoroutines, "Coroutines were not scheduled");

            coroutineService.TickCoroutines();

            Assert.AreEqual(2, numCompleted, "Coroutines did not complete");
        }
Beispiel #19
0
        public void CoroutineService_TickCoroutines_AssignsCoroutineException_WhenCoroutineThrowsInThatTick()
        {
            IEnumerator RunCoroutine()
            {
                yield return(null);

                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            ICoroutine coroutine = coroutineService.StartCoroutine(RunCoroutine());

            try
            {
                coroutineService.TickCoroutines();
            }
            catch
            { }

            Assert.IsInstanceOf(typeof(InvalidOperationException), coroutine.Exception, "Did not assign exception");
        }
Beispiel #20
0
        public void DamageOverTime_DealsPlayerDamage_Example2()
        {
            var coroutineService = new CoroutineService();

            using (DamageOverTime damageOverTime = new GameObject()
                                                   .AddComponent <DamageOverTime>())
                using (Player player = Object.Instantiate(Resources.Load <GameObject>("PlayerPrefab"))
                                       .GetComponent <Player>())
                {
                    var playerHpText = player.GetComponentInChildren <TextMesh>();

                    damageOverTime.Setup(coroutineService);

                    damageOverTime.DoDamage(player, 10, 2);

                    // Set time and tick coroutines
                    coroutineService.SetTime(1);
                    coroutineService.TickCoroutines();

                    Assert.AreEqual(80, player.Hp, "Incorrect player HP");
                    Assert.AreEqual("80/100", playerHpText.text, "Incorrect player hp text");
                }
        }