2023-06-05 19:57:17 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using System.Linq;
|
|
|
|
namespace BITKit.Entities
|
|
|
|
{
|
|
|
|
|
2023-08-23 01:59:26 +08:00
|
|
|
public sealed class EntityAnimator : EntityComponent
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-10-24 23:38:22 +08:00
|
|
|
[SerializeField] private Animator[] animators;
|
|
|
|
[SerializeReference, SubclassSelector] private References[] animationKeyWords;
|
|
|
|
[SerializeReference, SubclassSelector] private References _rootVelocity;
|
|
|
|
[SerializeReference, SubclassSelector] private References[] boolParameters;
|
|
|
|
[SerializeReference, SubclassSelector] private References[] floatParameters;
|
|
|
|
private List<string> keyWords;
|
2023-06-05 19:57:17 +08:00
|
|
|
public override void OnAwake()
|
|
|
|
{
|
|
|
|
keyWords = animationKeyWords.Select(x => x.Get()).ToList();
|
|
|
|
}
|
|
|
|
public override void OnStart()
|
|
|
|
{
|
|
|
|
entity.AddListener<string>(Constant.Animation.Play, Play);
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
|
|
|
|
private void Play(string animationName)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-08-23 01:59:26 +08:00
|
|
|
if (animationKeyWords.Length is 0 || keyWords.Contains(animationName))
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
animators.ForEach(x =>
|
|
|
|
{
|
2023-08-23 01:59:26 +08:00
|
|
|
if (!x.isActiveAndEnabled) return;
|
|
|
|
animationName = animationName.Replace(".", "_");
|
|
|
|
x.SetTrigger(animationName);
|
2023-06-05 19:57:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
private void OnAnimatorMove()
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
|
|
|
entity.Set(_rootVelocity, animators[0].velocity);
|
|
|
|
}
|
2023-08-23 01:59:26 +08:00
|
|
|
// ReSharper disable once UnusedMember.Local
|
|
|
|
private void AnimationEvent(string eventName)
|
2023-06-05 19:57:17 +08:00
|
|
|
{
|
2023-08-23 01:59:26 +08:00
|
|
|
entity.Invoke(Constant.Animation.OnEvent, eventName);
|
2023-06-05 19:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|