using System; using KINEMATION.MagicBlend.Runtime; using NBC; using UnityEngine; namespace NBF { public class PlayerAnimator : MonoBehaviour { public Animator _Animator; public FPlayer Player { get; private set; } private bool _isRodLayerEnabled; private bool _isInit; private PlayerIK _IK; private MagicBlending _magicBlending; private bool _IsInVehicle; #region 参数定义 public static readonly int IsSwiming = Animator.StringToHash("Swim"); public static readonly int ThrowFar = Animator.StringToHash("ThrowFar"); public static readonly int BoatDriving = Animator.StringToHash("BoatDriving"); public static readonly int BaitInWater = Animator.StringToHash("BaitInWater"); public static readonly int HeldRod = Animator.StringToHash("HeldRod"); public static readonly int RodArming = Animator.StringToHash("RodArming"); public static readonly int Forward = Animator.StringToHash("Forward"); public static readonly int Turn = Animator.StringToHash("Turn"); public static readonly int OnGround = Animator.StringToHash("OnGround"); public static readonly int RodRight = Animator.StringToHash("rod right"); public static readonly int RodForward = Animator.StringToHash("rod forward"); public static readonly int PreciseCast = Animator.StringToHash("Precise Cast"); public static readonly int PreciseIdle = Animator.StringToHash("Precise Idle"); public static readonly string LureRodLayer = "LureRod"; public static readonly string HandRodLayer = "HandRod"; #endregion private void Awake() { Player = GetComponentInParent(); _magicBlending = GetComponent(); _Animator = GetComponent(); _Animator.keepAnimatorStateOnDisable = true; _IK = GetComponent(); _isInit = true; Player.OnFishingSetEquiped += OnFishingSetEquiped_OnRaised; Player.OnFishingSetUnequip += OnFishingSetUnequip; Player.Data.OnStateChange += PlayerFSMState_OnValueChanged; } private void OnDestroy() { Player.OnFishingSetEquiped -= OnFishingSetEquiped_OnRaised; Player.OnFishingSetUnequip -= OnFishingSetUnequip; Player.Data.OnStateChange += PlayerFSMState_OnValueChanged; } private void OnFishingSetUnequip() { _isRodLayerEnabled = false; _IK.SetBipedLeftHandIK(enabled: false, null); } private void OnCastLure() { Player.Data.State = PlayerState.baitFlies; // SFXGameManagement.PlaySound("Cast Fishing Rod", base.transform); } private void OnBailUnnarm() { // if (IsThrowButtonPressed.Value) // { // _Animator.SetBool(ThrowFar, value: true); // Player.Data.State = PlayerState.casting; // OnBailOpen?.Invoke(); // } } private void OnFishingSetEquiped_OnRaised(FHandItem item) { if (item is FRod rod) { _isRodLayerEnabled = true; // var reel = Player.Rod.Reel; // _IK.SetBipedLeftHandIK(enabled: false, reel.FingersIKAnchor); } else { } } private void PlayAnimation(string state, string layer) { _Animator.CrossFade(state, 0.3f, _Animator.GetLayerIndex(layer)); } public void PlayPreciseCastAnimation() { _Animator.SetBool(PreciseIdle, value: false); _Animator.SetTrigger(PreciseCast); } public void SetLayerWeight(string layer, float weight) { _Animator.SetLayerWeight(_Animator.GetLayerIndex(layer), weight); } private void PlayerFSMState_OnValueChanged(PlayerState state) { switch (Player.Data.PreviousState) { case PlayerState.vehicle: _IsInVehicle = false; _Animator.SetBool(BoatDriving, value: false); break; case PlayerState.swiming: _Animator.SetBool(IsSwiming, value: false); break; case PlayerState.preciseCastIdle: _Animator.SetBool(PreciseIdle, value: false); break; case PlayerState.prepare: _Animator.SetBool(RodArming, value: false); break; case PlayerState.casting: _Animator.SetBool(ThrowFar, value: false); break; case PlayerState.collectFish: _magicBlending.BlendAsset.globalWeight = 0f; break; } switch (state) { case PlayerState.idle: case PlayerState.move: _Animator.SetBool(BaitInWater, value: false); _Animator.SetBool(HeldRod, value: false); _Animator.SetBool(ThrowFar, value: false); _Animator.SetBool(RodArming, value: false); break; case PlayerState.prepare: _Animator.SetBool(RodArming, value: true); _Animator.SetBool(HeldRod, value: true); break; case PlayerState.fishing: _Animator.SetBool(HeldRod, value: true); _Animator.SetBool(BaitInWater, value: true); break; case PlayerState.vehicle: _Animator.SetBool(BaitInWater, value: false); _Animator.SetBool(HeldRod, value: false); _Animator.SetBool(ThrowFar, value: false); _Animator.SetBool(RodArming, value: false); _Animator.SetBool(BoatDriving, value: true); _IK.SetBipedLeftHandIK(enabled: true); _IsInVehicle = true; break; case PlayerState.vehicleFishing: _Animator.SetBool(BaitInWater, value: false); _Animator.SetBool(HeldRod, value: false); _Animator.SetBool(ThrowFar, value: false); _Animator.SetBool(RodArming, value: false); _IsInVehicle = true; break; case PlayerState.swiming: _Animator.SetBool(IsSwiming, value: true); break; case PlayerState.collectFish: _Animator.SetBool(BaitInWater, value: false); _IK.SetAimIK(enabled: false); _magicBlending.BlendAsset.globalWeight = 1f; break; case PlayerState.preciseCastIdle: _Animator.SetBool(PreciseIdle, value: true); break; case PlayerState.casting: case PlayerState.baitFlies: case PlayerState.fight: case PlayerState.fishView: case PlayerState.throwFish: case PlayerState.flyModeDebug: break; } } private void LateUpdate() { if (Player.Data.State == PlayerState.swiming) { float value = Mathf.Lerp(_Animator.GetFloat(Forward), Player.Data.Speed / 2.5f, Time.deltaTime * 5f); float value2 = Mathf.Lerp(_Animator.GetFloat(Turn), Player.Data.RotationSpeed, Time.deltaTime * 5f); _Animator.SetFloat(Forward, Mathf.Clamp01(value)); _Animator.SetFloat(Turn, Mathf.Clamp(value2, -1f, 1f)); } else { float value3 = Mathf.Lerp(_Animator.GetFloat(Forward), Player.Data.Speed / 5f, Time.deltaTime * 20f); float value4 = Mathf.Lerp(_Animator.GetFloat(Turn), Player.Data.RotationSpeed, Time.deltaTime * 15f); _Animator.SetFloat(Forward, Mathf.Clamp01(value3)); _Animator.SetFloat(Turn, Mathf.Clamp(value4, -1f, 1f)); } var rod = Vector3.zero; if (Player.Rod) { rod = Player.Rod.transform.position; } _Animator.SetBool(OnGround, _IsInVehicle || Player.Data.IsGrounded); _Animator.SetFloat(RodRight, rod.x); _Animator.SetFloat(RodForward, rod.y); var isHandRodLayerEnabled = _isRodLayerEnabled && !Player.Data.IsLureRod ? 1 : 0; float handRodLayerWeight = _Animator.GetLayerWeight(_Animator.GetLayerIndex(HandRodLayer)); SetLayerWeight(HandRodLayer, Mathf.MoveTowards(handRodLayerWeight, isHandRodLayerEnabled, Time.deltaTime * 3f)); var isLureRodLayerEnabled = _isRodLayerEnabled && Player.Data.IsLureRod ? 1 : 0; float lureRodLayerWeight = _Animator.GetLayerWeight(_Animator.GetLayerIndex(LureRodLayer)); SetLayerWeight(LureRodLayer, Mathf.MoveTowards(lureRodLayerWeight, isLureRodLayerEnabled, Time.deltaTime * 3f)); } } }