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

51 lines
1.6 KiB
C#
Raw Normal View History

2023-08-27 02:58:19 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit.Animations;
using BITKit.Entities;
using BITKit.StateMachine;
using UnityEngine;
namespace BITFALL.Player.Animation
{
public interface IPlayerAnimationState : IState
{
void OnMovementStateChanged(IEntityMovementState oldState, IEntityMovementState newState);
}
public class PlayerAnimationController : StateBasedComponent<IPlayerAnimationState>
{
public const string _Walk = "Walk";
public const string _Run = "Run";
public const string _Sprint = "Sprint";
public const string _Crouch = "Crouch";
[SerializeField] internal UnityAnimator animator;
private IEntityMovement _movement;
private static readonly int Vertical = Animator.StringToHash("Vertical");
private static readonly int Horizontal = Animator.StringToHash("Horizontal");
private static readonly int SqrMagnitude = Animator.StringToHash("SqrMagnitude");
public override void OnAwake()
{
base.OnAwake();
_movement = entity.Get<IEntityMovement>();
_movement.OnStateChanged += OnMovementStateChanged;
}
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);
}
private void OnMovementStateChanged(IEntityMovementState arg1, IEntityMovementState arg2)
{
foreach (var x in StateDictionary.Values)
{
x.OnMovementStateChanged(arg1,arg2);
}
}
}
}