using System; using System.Collections; using System.Collections.Generic; using Animancer; using BITFALL.Player.Movement; using BITKit; using BITKit.Animations; using BITKit.Entities; using BITKit.StateMachine; using UnityEngine; using UnityEngine.Animations.Rigging; namespace BITFALL.Player.Animation { public interface IPlayerAnimationState : IState { void OnMovementStateChanged(IEntityMovementState oldState, IEntityMovementState newState); } public class PlayerAnimationController : StateBasedBehavior { [SerializeField] internal UnityAnimator animator; [SerializeField] internal AnimancerComponent animancerComponent; [Inject] internal IEntityMovement _movement; private static readonly int Vertical = Animator.StringToHash(BITConstant.Player.Vertical); private static readonly int Horizontal = Animator.StringToHash(BITConstant.Player.Horizontal); private static readonly int SqrMagnitude = Animator.StringToHash(BITConstant.Player.SqrMagnitude); private static readonly int Pitch = Animator.StringToHash(BITConstant.Player.Pitch); public override void OnAwake() { base.OnAwake(); _movement.OnStateChanged += OnMovementStateChanged; _movement.OnCommand += OnCommand; } private void OnCommand(object obj) { switch (obj) { case OnPlayerJumpCommand: UnityEntity.Invoke(Constant.Animation.Play,BITConstant.Player.Jump); break; } } public override void OnFixedUpdate(float deltaTime) { animator.animator.SetFloat(Vertical, _movement.LocomotionBasedVelocity.z); animator.animator.SetFloat(Horizontal, _movement.LocomotionBasedVelocity.x); animator.animator.SetFloat(SqrMagnitude, _movement.LocomotionBasedVelocity.sqrMagnitude); animator.animator.SetFloat(Pitch,-MathV.TransientRotationAxis(_movement.ViewRotation.eulerAngles.x)); CurrentState?.OnStateUpdate(deltaTime); UnityEntity.SetDirect(BITHash.Player.IsGrounded,_movement.IsGrounded); } private void OnMovementStateChanged(IEntityMovementState arg1, IEntityMovementState arg2) { foreach (var x in StateDictionary.Values) { x.OnMovementStateChanged(arg1,arg2); } } } }