You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/3rd/D2D_Scripts/Gameplay/PlayAnimationClipOnGameStat...

47 lines
1.4 KiB
C#

2 months ago
using System;
using Animancer;
using Cysharp.Threading.Tasks;
using D2D.Core;
using D2D.Utilities;
using UnityEngine;
namespace D2D
{
public class PlayAnimationClipOnGameState : GameStateMachineUser
{
[SerializeField] private float _delay;
[SerializeField] private float _blend = .3f;
[SerializeField] private AnimationClip[] _onStart;
[SerializeField] private AnimationClip[] _onRunning;
[SerializeField] private AnimationClip[] _onWin;
[SerializeField] private AnimationClip[] _onLose;
[SerializeField] private AnimationClip[] _onGameFinish;
private AnimancerComponent _animancer;
private void Awake()
{
_animancer = Get<AnimancerComponent>();
}
private void Start() => PlayAnimation(_onStart);
protected override void OnGameRun() => PlayAnimation(_onRunning);
protected override void OnGameWin() => PlayAnimation(_onWin);
protected override void OnGameLose() => PlayAnimation(_onLose);
protected override void OnGameFinish() => PlayAnimation(_onGameFinish);
private async UniTaskVoid PlayAnimation(AnimationClip[] clips)
{
if (clips.IsNullOrEmpty())
return;
await _delay.Seconds();
_animancer.Play(clips.GetRandomElement(), _blend);
}
}
}