using System.Collections.Generic; using UnityEngine; using System.Linq; using System.Net.Configuration; using BITKit.Animations; namespace BITKit.Entities { [ExecuteInEditMode] public sealed class EntityAnimator : EntityBehavior { [SerializeField] private bool debug; [SerializeField] private UnityAnimator[] animators; [SerializeReference, SubclassSelector] private References[] animationKeyWords; [SerializeReference, SubclassSelector] private References _rootVelocity; [SerializeReference, SubclassSelector] private References[] boolParameters; [SerializeReference, SubclassSelector] private References[] floatParameters; private List keyWords; private ConstantHash[] _boolParameterHashes; private ConstantHash[] _floatParameterHashes; public override void OnAwake() { keyWords = animationKeyWords.Select(x => x.Get()).ToList(); _boolParameterHashes = boolParameters.Select(x =>new ConstantHash(x.Get())).ToArray(); _floatParameterHashes = floatParameters.Select(x =>new ConstantHash(x.Get())).ToArray(); } public override void OnStart() { UnityEntity.AddListener(Constant.Animation.Play, Play); } private void Play(string animationName) { if (enabled is false) return; if (animationKeyWords.Length is 0 || keyWords.Contains(animationName)) { if (debug) BIT4Log.Log($"{gameObject.name} playing {animationName}"); animators.ForEach(x => { if (!x.isActiveAndEnabled) return; x.Play(animationName); }); return; } if (debug) BIT4Log.Log($"{gameObject.name} cancel play {animationName}"); } public override void OnFixedUpdate(float deltaTime) { bool cacheBool; float cacheFloat; foreach (var boolPar in _boolParameterHashes) { foreach (var x in animators) { if (!x.isActiveAndEnabled) return; UnityEntity.GetDirect(boolPar, out cacheBool); x.animator.SetBool(boolPar.HashCode, cacheBool); } } foreach (var floatPar in _floatParameterHashes) { foreach (var x in animators) { if (!x.isActiveAndEnabled) return; UnityEntity.GetDirect(floatPar, out cacheFloat); x.animator.SetFloat(floatPar.HashCode, cacheFloat); } } } private void OnAnimatorMove() { if (enabled is false) return; if (BITAppForUnity.IsPlaying is false) { foreach (var x in animators) { x.animator.ApplyBuiltinRootMotion(); } return; } if (_rootVelocity is not null && UnityEntity is not null) UnityEntity.Set(_rootVelocity, animators[0].animator.velocity); } // ReSharper disable once UnusedMember.Local private void AnimationEvent(string eventName) { UnityEntity.Invoke(Constant.Animation.OnEvent, eventName); } } }