87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Animancer;
|
|
using BITFALL.Player.Animation.States;
|
|
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);
|
|
}
|
|
[CustomType(typeof(PlayerAnimationController))]
|
|
public class PlayerAnimationController : StateBasedBehavior<IPlayerAnimationState>
|
|
{
|
|
[SerializeField] private AnimancerComponent animancerComponent;
|
|
|
|
[Inject]
|
|
public IEntityMovement _movement;
|
|
|
|
[Inject] public IHealth _health;
|
|
|
|
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 AnimancerComponent AnimancerComponent => animancerComponent;
|
|
public override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
_movement.OnStateChanged += OnMovementStateChanged;
|
|
|
|
_movement.OnCommand += OnCommand;
|
|
_health.OnSetAlive += OnSetAlive;
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
{
|
|
AnimancerComponent.enabled = obj;
|
|
}
|
|
|
|
public override void OnStart()
|
|
{
|
|
base.OnStart();
|
|
TransitionState<Walk>();
|
|
}
|
|
|
|
private void OnCommand(object obj)
|
|
{
|
|
switch (obj)
|
|
{
|
|
case OnPlayerJumpCommand:
|
|
UnityEntity.Invoke<string>(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.Velocity.GetLength() / _movement.ReferenceSpeed);
|
|
|
|
//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);
|
|
}
|
|
}
|
|
}
|
|
}
|