Files
BITKit/Src/Unity/Scripts/Entity/Components/Animator/EntityAnimator.cs
2023-09-02 00:51:39 +08:00

65 lines
2.2 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using System.Linq;
namespace BITKit.Entities
{
public sealed class EntityAnimator : EntityComponent
{
public Animator[] animators;
[SerializeReference, SubclassSelector] public References[] animationKeyWords;
[SerializeReference, SubclassSelector] public References _rootVelocity;
[SerializeReference, SubclassSelector] public References[] boolParameters;
[SerializeReference, SubclassSelector] public References[] floatParameters;
List<string> keyWords;
public override void OnAwake()
{
keyWords = animationKeyWords.Select(x => x.Get()).ToList();
}
public override void OnStart()
{
entity.AddListener<string>(Constant.Animation.Play, Play);
}
private void Play(string animationName)
{
if (animationKeyWords.Length is 0 || keyWords.Contains(animationName))
{
animators.ForEach(x =>
{
if (!x.isActiveAndEnabled) return;
animationName = animationName.Replace(".", "_");
x.SetTrigger(animationName);
});
}
}
public override void OnFixedUpdate(float deltaTime)
{
foreach (var boolPar in boolParameters)
{
animators.ForEach(x =>
{
if (x.isActiveAndEnabled)
x.SetBool(boolPar, entity.Get<bool>(boolPar));
});
}
foreach (var floatPar in floatParameters)
{
animators.ForEach(x =>
{
if (x.isActiveAndEnabled)
x.SetFloat(floatPar, entity.Get<float>(floatPar));
});
}
}
private void OnAnimatorMove()
{
entity.Set(_rootVelocity, animators[0].velocity);
}
// ReSharper disable once UnusedMember.Local
private void AnimationEvent(string eventName)
{
entity.Invoke(Constant.Animation.OnEvent, eventName);
}
}
}