Example #1
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");
        }
Example #2
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");
        }
        public IEnumerator CoroutineService_WaitForNumTicks_WaitsForNumTicks_WithServiceRunner()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

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

                    didComplete = true;
                }

                coroutineService.StartCoroutine(RunCoroutine());

                // This test may complete before ServiceRunner receives it's first update, depending on order of execution
                yield return(null);

                yield return(null);

                Assert.IsTrue(didComplete, "Coroutine did not complete");
            }
        }
Example #4
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");
        }
Example #5
0
        public SceneService(CoroutineService coroutineService, GlobalEventDispatcher globalEventDispatcher)
        {
            this.coroutineService      = coroutineService;
            this.globalEventDispatcher = globalEventDispatcher;

            SceneManager.sceneLoaded += OnSceneLoaded;
        }
Example #6
0
        public IEnumerator DamageOverTime_DealsPlayerDamage_Example1()
        {
            var coroutineService = new CoroutineService();

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

                        serviceRunner.SetCoroutineService(coroutineService);
                        damageOverTime.Setup(coroutineService);

                        damageOverTime.DoDamage(player, 10, 2);

                        // Wait for Unity seconds
                        yield return(new WaitForSeconds(2));

                        Assert.AreEqual(80, player.Hp, "Incorrect player HP");

                        Assert.AreEqual("80/100", playerHpText.text, "Incorrect player hp text");
                    }
        }
Example #7
0
        public void CoroutineService_TickCoroutines_WithTask_Throws_WhenTaskFails()
        {
            var coroutineService = new CoroutineService();

            Assert.Throws <InvalidOperationException>(() =>
            {
                coroutineService.StartCoroutine(Task.FromException(new InvalidOperationException()));
            });
        }
Example #8
0
        public void CoroutineService_WaitForNextTickAsync_ReturnTaskThatCompletesAfterNextTick()
        {
            var coroutineService = new CoroutineService();

            Task task = coroutineService.WaitForNextTickAsync();

            coroutineService.TickCoroutines();

            Assert.IsTrue(task.IsCompleted, "Task did not complete");
        }
Example #9
0
        public void CoroutineService_StartCoroutine_ThrowsException_WhenCoroutineThrowsImmediately()
        {
            IEnumerator RunCoroutine()
            {
                throw new InvalidOperationException();
            }

            var coroutineService = new CoroutineService();

            Assert.Throws <InvalidOperationException>(() => coroutineService.StartCoroutine(RunCoroutine()));
        }
Example #10
0
    public void Awake()
    {
        // Setup coroutine service
        CoroutineService coroutineService = new CoroutineService();

        _coroutineServiceRunner.SetCoroutineService(coroutineService);

        // Setup DOT component
        _damageOverTime.Setup(coroutineService);

        // Start dealing damage
        _damageOverTime.DoDamage(_player, 20, 5);
    }
Example #11
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");
        }
Example #12
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());
        }
Example #13
0
        public GameServices(IMessageBrokerService messageBrokerService, ITimeService timeService, IDataSaver dataSaver,
                            IGameLogic gameLogic, IWorldObjectReferenceService worldObjectReference)
        {
            var networkService = new GameNetworkService();

            NetworkService              = networkService;
            MessageBrokerService        = messageBrokerService;
            TimeService                 = timeService;
            WorldObjectReferenceService = worldObjectReference;
            DataSaver = dataSaver;

            CommandService       = new CommandService <IGameLogic>(gameLogic, networkService);
            PoolService          = new PoolService();
            AssetResolverService = new AssetResolverService();
            TickService          = new TickService();
            CoroutineService     = new CoroutineService();
        }
Example #14
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");
        }
        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");
        }
Example #16
0
        public void CoroutineService_WaitForSecondsRealtimeAsync_ReturnTaskThatCompletesAfterNumSeconds()
        {
            float     timeoutSeconds = 0.2f;
            Stopwatch sw             = new Stopwatch();

            var coroutineService = new CoroutineService();

            Task task = coroutineService.WaitForSecondsRealtimeAsync(0.1f);

            sw.Start();

            while (!task.IsCompleted && sw.ElapsedMilliseconds < timeoutSeconds)
            {
                coroutineService.SetRealTime(sw.ElapsedMilliseconds * 1000);
                Thread.Sleep(1);
            }

            Assert.Less(timeoutSeconds, sw.ElapsedMilliseconds, "Task timed out");
        }
Example #17
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");
        }
        static async Task Main(string[] args)
        {
            CoroutineService             coroutineService = new CoroutineService();
            ServerCoroutineServiceRunner coroutineRunner  = new ServerCoroutineServiceRunner(coroutineService);

            Player player1 = new Player(coroutineService);

            player1.MovePlayer();

            // Run server logic
            while (true)
            {
                // Tick Coroutines
                coroutineRunner.Tick();

                // Assuming server ticks 66 times a second
                await Task.Delay(15);
            }
        }
Example #19
0
        public void CoroutineService_TickCoroutines_TicksFixedScheduledCoroutine()
        {
            bool isCompleted = false;

            var coroutineService = new CoroutineService();

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

                isCompleted = true;
            }

            coroutineService.StartCoroutine(RunCoroutine());

            coroutineService.TickFixedCoroutines();

            Assert.IsTrue(isCompleted, "Coroutine did not complete");
        }
Example #20
0
        public void CoroutineService_StartCoroutine_RunsCoroutine()
        {
            bool didComplete = false;

            IEnumerator RunCoroutine()
            {
                yield return(null);

                didComplete = true;
            }

            var coroutineService = new CoroutineService();

            coroutineService.StartCoroutine(RunCoroutine());

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

            coroutineService.TickCoroutines();

            Assert.IsTrue(didComplete, "Coroutine did not complete");
        }
Example #21
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");
        }
        public IEnumerator CoroutineService_WaitForLateTick_RunsAsUnityCoroutine()
        {
            bool didComplete = false;

            using (CoroutineServiceRunner serviceRunner = new GameObject()
                                                          .AddComponent <CoroutineServiceRunner>())
            {
                var coroutineService = new CoroutineService();
                serviceRunner.SetCoroutineService(coroutineService);

                IEnumerator RunCoroutine()
                {
                    yield return(null);

                    didComplete = true;
                }

                yield return(coroutineService.StartCoroutine(RunCoroutine()));

                Assert.IsTrue(didComplete, "Coroutine did complete when it shouldn't have");
            }
        }
Example #23
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");
        }
Example #24
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");
        }
Example #25
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");
        }
Example #26
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");
        }
Example #27
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");
                }
        }
Example #28
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");
        }
Example #29
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");
        }
Example #30
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");
        }