start dealing with corutines

This commit is contained in:
2023-03-14 06:09:21 +02:00
parent 6d26efe67c
commit 63daaaf3e5
3 changed files with 21 additions and 13 deletions
+11 -3
View File
@@ -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()
+8 -9
View File
@@ -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)
+2 -1
View File
@@ -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;
}