From 63daaaf3e5d57cbec85b4b8a6f9db1a753f0d29b Mon Sep 17 00:00:00 2001 From: Valdimir Date: Tue, 14 Mar 2023 06:09:21 +0200 Subject: [PATCH] start dealing with corutines --- Assets/Scripts/InteractableObjects/Bed.cs | 14 +++++++++++--- Assets/Scripts/Player/Player.cs | 17 ++++++++--------- Assets/Scripts/TimeManager.cs | 3 ++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Assets/Scripts/InteractableObjects/Bed.cs b/Assets/Scripts/InteractableObjects/Bed.cs index 1d17a5c9..75907df4 100644 --- a/Assets/Scripts/InteractableObjects/Bed.cs +++ b/Assets/Scripts/InteractableObjects/Bed.cs @@ -1,5 +1,5 @@ using System; -using UnityEngine; +using System.Collections; public class Bed : BaseInteractableObject { @@ -8,9 +8,17 @@ public class Bed : BaseInteractableObject public override void Interact(Player player) { _player=player; - Debug.Log("Sitting"); - player.SetPlayerAnimation(AnimationStates.Sitting); + StartCoroutine(Sleep()); + } + + private IEnumerator Sleep() + { + _player.SetPlayerAnimation(AnimationStates.Sitting); + yield return StartCoroutine(_player.IsAnimationStatePlaying(0)); + print("sitting animation finished"); TimeManager.Instance.FastForward(TimeSpan.FromHours(8), OnFastForwardEnd); + + yield break; } private void OnFastForwardEnd() diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs index 26595d58..0baa668f 100644 --- a/Assets/Scripts/Player/Player.cs +++ b/Assets/Scripts/Player/Player.cs @@ -1,5 +1,6 @@ using Assets.Scripts.Actions.Interfaces; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -63,7 +64,7 @@ public class Player : MonoBehaviour { if (IsBlockingAnimation(_currentAnimation)) { - if (IsAnimationStatePlaying(0, GetEnumMemberValue(_currentAnimation))) + StartCoroutine(IsAnimationStatePlaying(0)); return; } @@ -116,8 +117,6 @@ public class Player : MonoBehaviour _navAgent.isStopped = false; SetPlayerAnimation(AnimationStates.Walking); - //transform.position = Vector3.MoveTowards(_navAgent.transform.position, _navAgent.pathEndPosition, _navAgent.speed * Time.deltaTime); - // transform.position = _animator.(_navAgent.pathEndPosition); _animator.SetFloat(WALK_VELOCITY, _navAgent.velocity.magnitude); return IsPathComplete(_navAgent.destination) ? TaskStatus.Complete : TaskStatus.InProgress; } @@ -175,13 +174,13 @@ public class Player : MonoBehaviour _currentAnimation = newState; } - private bool IsAnimationStatePlaying(int animLayer, string stateName) + + public IEnumerator IsAnimationStatePlaying(int animLayer) { - if (_animator.GetCurrentAnimatorStateInfo(animLayer).IsName(stateName) && - _animator.GetCurrentAnimatorStateInfo(animLayer).normalizedTime < 1.0f) - return true; - else - return false; + string stateName = GetEnumMemberValue(_currentAnimation); + var stateInfo = _animator.GetCurrentAnimatorStateInfo(animLayer); + yield return new WaitWhile(() => stateInfo.IsName(stateName) && stateInfo.normalizedTime < 1.0f); + yield break; } public void AddTask(PlayerTasks task) diff --git a/Assets/Scripts/TimeManager.cs b/Assets/Scripts/TimeManager.cs index 710d8e24..278815d1 100644 --- a/Assets/Scripts/TimeManager.cs +++ b/Assets/Scripts/TimeManager.cs @@ -54,6 +54,7 @@ public class TimeManager : MonoBehaviour // _sunInitialIntensity = _sunLight.intensity; _timer = _minuteToRealTime; _currentTime = TimeSpan.Zero + TimeSpan.FromHours(_startHour); + _timeToStop = _currentTime; _sunriseTime = TimeSpan.FromHours(_sunriseHour); _sunsetTime = TimeSpan.FromHours(_sunsetHour); } @@ -77,7 +78,7 @@ public class TimeManager : MonoBehaviour { _minuteToRealTime = MINUTE_TIME; _timeToStop = TimeSpan.MaxValue; - _callBackOnFastForward.Invoke(); + _callBackOnFastForward?.Invoke(); } _timer = _minuteToRealTime; }