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

87 lines
2.5 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;
2024-03-31 23:34:22 +08:00
using BITFALL.Player.Animation.States;
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);
}
2024-04-19 00:40:34 +08:00
[CustomType(typeof(PlayerAnimationController))]
2023-10-30 01:25:53 +08:00
public class PlayerAnimationController : StateBasedBehavior<IPlayerAnimationState>
2023-08-27 02:58:19 +08:00
{
2024-04-19 00:40:34 +08:00
[SerializeField] private AnimancerComponent animancerComponent;
2023-11-30 00:23:23 +08:00
[Inject]
2024-04-22 01:21:28 +08:00
public IEntityMovement _movement;
2024-04-19 00:40:34 +08:00
2024-04-22 01:21:28 +08:00
[Inject] public IHealth _health;
2023-11-30 00:23:23 +08:00
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
2024-04-19 00:40:34 +08:00
public AnimancerComponent AnimancerComponent => animancerComponent;
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;
2024-04-19 00:40:34 +08:00
_health.OnSetAlive += OnSetAlive;
}
private void OnSetAlive(bool obj)
{
AnimancerComponent.enabled = obj;
2023-08-27 02:58:19 +08:00
}
2023-11-30 00:23:23 +08:00
2024-03-31 23:34:22 +08:00
public override void OnStart()
{
base.OnStart();
TransitionState<Walk>();
}
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)
{
2024-04-19 00:40:34 +08:00
//animator.animator.SetFloat(Vertical, _movement.LocomotionBasedVelocity.z);
//animator.animator.SetFloat(Horizontal, _movement.LocomotionBasedVelocity.x);
//animator.animator.SetFloat(SqrMagnitude, _movement.Velocity.GetLength() / _movement.ReferenceSpeed);
2023-11-30 00:23:23 +08:00
2024-04-19 00:40:34 +08:00
//animator.animator.SetFloat(Pitch,-MathV.TransientRotationAxis(_movement.ViewRotation.eulerAngles.x));
2023-11-30 00:23:23 +08:00
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);
}
}
}
}