BITFALL/Assets/Artists/Scripts/Player/PlayerAnimationController/PlayerAnimationController.cs

72 lines
2.1 KiB
C#
Raw Normal View History

2023-08-27 02:58:19 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2024-02-21 01:40:53 +08:00
using Animancer;
2023-11-30 00:23:23 +08:00
using BITFALL.Player.Movement;
using BITKit;
2023-08-27 02:58:19 +08:00
using BITKit.Animations;
using BITKit.Entities;
using BITKit.StateMachine;
using UnityEngine;
2023-11-30 00:23:23 +08:00
using UnityEngine.Animations.Rigging;
2023-08-27 02:58:19 +08:00
namespace BITFALL.Player.Animation
{
public interface IPlayerAnimationState : IState
{
void OnMovementStateChanged(IEntityMovementState oldState, IEntityMovementState newState);
}
2023-10-30 01:25:53 +08:00
public class PlayerAnimationController : StateBasedBehavior<IPlayerAnimationState>
2023-08-27 02:58:19 +08:00
{
2023-10-20 19:31:12 +08:00
2023-08-27 02:58:19 +08:00
[SerializeField] internal UnityAnimator animator;
2024-02-21 01:40:53 +08:00
[SerializeField] internal AnimancerComponent animancerComponent;
2023-11-30 00:23:23 +08:00
[Inject]
internal IEntityMovement _movement;
2023-11-15 23:54:54 +08:00
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);
2023-11-30 00:23:23 +08:00
private static readonly int Pitch = Animator.StringToHash(BITConstant.Player.Pitch);
2023-08-27 02:58:19 +08:00
public override void OnAwake()
{
base.OnAwake();
_movement.OnStateChanged += OnMovementStateChanged;
2023-11-30 00:23:23 +08:00
_movement.OnCommand += OnCommand;
2023-08-27 02:58:19 +08:00
}
2023-11-30 00:23:23 +08:00
private void OnCommand(object obj)
{
switch (obj)
{
case OnPlayerJumpCommand:
UnityEntity.Invoke<string>(Constant.Animation.Play,BITConstant.Player.Jump);
break;
}
}
2023-08-27 02:58:19 +08:00
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);
2023-11-30 00:23:23 +08:00
animator.animator.SetFloat(Pitch,-MathV.TransientRotationAxis(_movement.ViewRotation.eulerAngles.x));
2024-02-21 01:40:53 +08:00
CurrentState?.OnStateUpdate(deltaTime);
2023-11-30 00:23:23 +08:00
UnityEntity.SetDirect(BITHash.Player.IsGrounded,_movement.IsGrounded);
2023-08-27 02:58:19 +08:00
}
private void OnMovementStateChanged(IEntityMovementState arg1, IEntityMovementState arg2)
{
foreach (var x in StateDictionary.Values)
{
x.OnMovementStateChanged(arg1,arg2);
}
}
}
}