Stratasys-450mc-VR/Assets/Scripts/Managers/AnimationManager.cs

73 lines
2.3 KiB
C#
Executable File

using SO;
using SO.Channels;
using UnityEngine;
namespace Managers
{
public class AnimationManager : MonoBehaviour
{
[SerializeField] protected Animation animationPlayer;
[SerializeField] private StepChangeChannelSO stepChangeChannel;
[SerializeField] private SimpleChannelSO animationChannel;
[SerializeField] private SimpleChannelSO nextStepChannel;
[SerializeField] private SimpleChannelSO previousStepChannel;
[SerializeField] private bool IsGuideModel;
[SerializeField] private AnimationClip clearClip;
private bool hasPlayed = false;
private string currentAnimationName = "";
private void OnEnable()
{
stepChangeChannel.OnStepChanged += OnStepChanged;
animationChannel.OnEventRaised += PlayAnimation;
nextStepChannel.OnEventRaised += NextStepAnimation;
previousStepChannel.OnEventRaised += ResetAnimation;
}
private void OnDisable()
{
stepChangeChannel.OnStepChanged -= OnStepChanged;
animationChannel.OnEventRaised -= PlayAnimation;
nextStepChannel.OnEventRaised -= NextStepAnimation;
previousStepChannel.OnEventRaised -= ResetAnimation;
}
private void ResetAnimation()
{
currentAnimationName = animationPlayer.clip.name;
animationPlayer.Rewind(currentAnimationName);
animationPlayer.Play(currentAnimationName);
animationPlayer.Sample();
animationPlayer.Stop();
}
private void OnStepChanged(StepSO step)
{
animationPlayer.clip = IsGuideModel?step.GuideClip:step.Animation;
if (!IsGuideModel)
{
animationPlayer.Play();
}
}
protected virtual void PlayAnimation()
{
ResetAnimation();
animationPlayer.Play();
hasPlayed = true;
}
private void NextStepAnimation()
{
if (!hasPlayed)
{
currentAnimationName = animationPlayer.clip.name;
animationPlayer.Play();
}
hasPlayed = false;
}
}
}