proto_fighter/Assets/Scripts/AttackAnimationState.cs

87 lines
2.9 KiB
C#
Raw Permalink Normal View History

using System;
using UnityEngine;
public class AttackAnimationState : StateMachineBehaviour
{
private enum HitBoxState : byte
{
Startup,
Active,
Recovery
}
private float _startTime;
private HitBox _hitBox;
private HitBoxState _currentHitBoxState;
public float activeStartTime;
public float activeEndTime;
public AttackState attack;
// OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
_currentHitBoxState = HitBoxState.Startup;
_startTime = Time.time;
var player = animator.GetComponent<Player>();
switch (attack)
{
case AttackState.Idle:
Debug.LogError("AttackAnimationState should not be associated to idle!");
break;
case AttackState.Light:
_hitBox = player.lightHitBox;
break;
case AttackState.Medium:
_hitBox = player.mediumHitBox;
break;
case AttackState.Heavy:
_hitBox = player.heavyHitBox;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (Time.time >= activeStartTime + _startTime && _currentHitBoxState == HitBoxState.Startup)
{
_currentHitBoxState = HitBoxState.Active;
_hitBox.DoesDealDamage = true;
}
if (Time.time >= activeEndTime + _startTime && _currentHitBoxState == HitBoxState.Active)
{
_currentHitBoxState = HitBoxState.Recovery;
_hitBox.DoesDealDamage = false;
}
if (Time.time >= stateInfo.length + _startTime)
{
// we done!
var player = animator.GetComponent<Player>();
player.StopAttack();
}
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
// public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
// {
//
// }
// OnStateMove is called right after Animator.OnAnimatorMove()
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that processes and affects root motion
//}
// OnStateIK is called right after Animator.OnAnimatorIK()
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// // Implement code that sets up animation IK (inverse kinematics)
//}
}