private void UpdateCoroutineServiceTime() { TimeSpan timeSinceStartup = DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime(); _coroutineService?.SetTime((float)timeSinceStartup.TotalSeconds); _coroutineService?.SetTime((float)timeSinceStartup.TotalSeconds); }
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"); }
public IEnumerator CoroutineService_WaitForSecondsAsync_WaitForSeconds() { var coroutineService = new CoroutineService(); float delaySeconds = 0.1f; float startTimeSeconds = Time.time; coroutineService.SetTime(startTimeSeconds); Task task = coroutineService.WaitForSecondsAsync(delaySeconds); while (!task.IsCompleted) { coroutineService.SetTime(Time.time); coroutineService.TickCoroutines(); yield return(null); } Assert.IsTrue(Time.time >= startTimeSeconds + delaySeconds, "Not enough time has passed"); }
public void CoroutineService_WaitForSecondsAsync_ReturnTaskThatCompletesAfterNumSeconds() { float timeoutSeconds = 0.2f; Stopwatch sw = new Stopwatch(); var coroutineService = new CoroutineService(); Task task = coroutineService.WaitForSecondsAsync(0.1f); sw.Start(); while (!task.IsCompleted && sw.ElapsedMilliseconds < timeoutSeconds) { coroutineService.SetTime(sw.ElapsedMilliseconds * 1000); Thread.Sleep(1); } Assert.Less(timeoutSeconds, sw.ElapsedMilliseconds, "Task timed out"); }
public void CoroutineService_WaitForSeconds_CompletesCoroutineAfterNumSeconds() { bool didComplete = false; var coroutineService = new CoroutineService(); IEnumerator RunCoroutine() { yield return(coroutineService.WaitForSeconds(0.1f)); didComplete = true; }; coroutineService.StartCoroutine(RunCoroutine()); coroutineService.SetTime(1); coroutineService.TickCoroutines(); Assert.IsTrue(didComplete, "Did not complete"); }
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"); } }
public void CoroutineService_SetTime_UpdatesCoroutineTime() { bool isCompleted = false; var coroutineService = new CoroutineService(); IEnumerator RunCoroutine() { yield return(coroutineService.WaitForSeconds(1f)); isCompleted = true; } coroutineService.StartCoroutine(RunCoroutine()); coroutineService.TickCoroutines(); // Still waits Assert.IsFalse(isCompleted, "Coroutine completed too early"); coroutineService.SetTime(1f); coroutineService.TickCoroutines(); // Completes Assert.IsTrue(isCompleted, "Coroutine did not complete"); }