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 keyWords; public override void OnAwake() { keyWords = animationKeyWords.Select(x => x.Get()).ToList(); } public override void OnStart() { entity.AddListener(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(boolPar)); }); } foreach (var floatPar in floatParameters) { animators.ForEach(x => { if (x.isActiveAndEnabled) x.SetFloat(floatPar, entity.Get(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); } } }