using System; using System.Collections; using UnityEngine; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public interface IAnimationStateController { Animator Anim { get; set; } void SetAnimationParameter(T parameter, bool value) where T : Enum; void SetAnimationParameter(T parameter, int value) where T : Enum; void SetAnimationParameter(T parameter, float value) where T : Enum; void SetAnimationTrigger(T parameter) where T : Enum; bool IsComparingCurrentAnimation(string animationName, int animatorLayer = 0) { return Anim.GetCurrentAnimatorStateInfo(animatorLayer).IsName(animationName); } bool IsComparingCurrentAnimation(T animationName, int animatorLayer = 0) where T : Enum; IEnumerator WaitForAnimationToRun(string animationName, Action onSuccess, float timeout = 0.2f) { var elapsedTime = 0f; while (!IsComparingCurrentAnimation(animationName)) { elapsedTime += Time.deltaTime; yield return null; if (elapsedTime <= timeout) continue; Debug.Log("Timeout waiting for animation state: " + animationName); onSuccess?.Invoke(false); } onSuccess?.Invoke(true); } IEnumerator WaitForAnimationToRun(T animationName, Action onSuccess, float timeout = 0.2f) where T : Enum { var elapsedTime = 0f; while (!IsComparingCurrentAnimation(animationName)) { elapsedTime += Time.deltaTime; yield return null; if (elapsedTime <= timeout) continue; Debug.Log("Timeout waiting for animation state: " + animationName); onSuccess?.Invoke(false); } onSuccess?.Invoke(true); } void SetCurrentAnimationSpeed(float targetDuration, int animatorLayer = 0) { var animationLength = Anim.GetCurrentAnimatorStateInfo(animatorLayer).length; Anim.speed = animationLength / targetDuration; } float GetCurrentAnimationNormalizedTime(int animatorLayer = 0) { return Anim.GetCurrentAnimatorStateInfo(animatorLayer).normalizedTime; } void ResetAnimationSpeed() { Anim.speed = 1f; } } }