OldBlueWater/BlueWater/Assets/02.Scripts/Interface/IAnimationStateController.cs

68 lines
2.5 KiB
C#

using System;
using System.Collections;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public interface IAnimationStateController
{
Animator Anim { get; set; }
void SetAnimationParameter<T>(T parameter, bool value) where T : Enum;
void SetAnimationParameter<T>(T parameter, int value) where T : Enum;
void SetAnimationParameter<T>(T parameter, float value) where T : Enum;
void SetAnimationTrigger<T>(T parameter) where T : Enum;
bool IsComparingCurrentAnimation(string animationName, int animatorLayer = 0)
{
return Anim.GetCurrentAnimatorStateInfo(animatorLayer).IsName(animationName);
}
bool IsComparingCurrentAnimation<T>(T animationName, int animatorLayer = 0) where T : Enum;
IEnumerator WaitForAnimationToRun(string animationName, Action<bool> 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>(T animationName, Action<bool> 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;
}
}
}