BITFALL/Assets/BITKit/Scripts/Entity/Components/Animator/EntityAnimator.cs

69 lines
2.2 KiB
C#
Raw Normal View History

2023-06-08 14:09:50 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.Serialization;
using UnityEngine.Events;
using System.Linq;
namespace BITKit.Entities
{
public 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);
}
protected virtual void Play(string name)
{
if (animationKeyWords.Length is 0 || keyWords.Contains(name))
{
animators.ForEach(x =>
{
if (x.isActiveAndEnabled)
{
name = name.Replace(".", "_");
x.SetTrigger(name);
}
});
}
}
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));
});
}
}
void OnAnimatorMove()
{
entity.Set(_rootVelocity, animators[0].velocity);
}
void AnimationEvent(string name)
{
entity.Invoke(Constant.Animation.OnEvent, name);
}
}
}