Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Mst;
public class PlayerStateTransition : AnimatorBehaviourBase
{
[SerializeField] float animTransitionDuration = 0.2f;
[SerializeField] float enabledInputSinceTime = 0.0f;
[SerializeField] PlayerState currentState;
[SerializeField] bool isTransitionDefault;
[SerializeField] float defaultCancelTime = 1.0f;
[SerializeField] List<TransitionSettings> changedDefaultStateSettings;
[SerializeField] List<TransitionSettings> changedStateSettings;
TransitionSettings nextState;
bool isTransition;
bool isFading;
#region InnerClass
[System.Serializable]
public class TransitionSettings
{
public PlayerState changedState;
public float priority = 0.0f;
public float cancelTime = 0.0f;
public void Init()
{
priority = -99.0f;
}
}
#endregion
public override void StateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
GameManager.I.Player.PlayerStateModel.ChangeState(currentState);
isTransition = false;
isFading = false;
nextState = new TransitionSettings();
nextState.Init();
}
public override void StateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if(isFading)
return;
if(isTransitionDefault && NormalizedTime.Value >= defaultCancelTime)
CheckDefaultTransition(stateInfo);
CheckStateTransition(stateInfo);
if(isTransition)
TransitionState(animator, stateInfo);
}
void CheckDefaultTransition(AnimatorStateInfo stateInfo)
{
foreach(var state in changedDefaultStateSettings)
{
if(GameManager.I.Player.PlayerStateModel.IsChangedNextState(state.changedState))
{
if(nextState.priority <= state.priority)
nextState = state;
isTransition = true;
}
}
}
void CheckStateTransition(AnimatorStateInfo stateInfo)
{
if(NormalizedTime.Value < enabledInputSinceTime)
return;
foreach(var state in changedStateSettings)
{
if(GameManager.I.Player.PlayerStateModel.IsChangedNextState(state.changedState))
{
if(nextState.priority <= state.priority)
nextState = state;
isTransition = true;
}
}
}
void TransitionState(Animator animator, AnimatorStateInfo stateInfo)
{
if(!(NormalizedTime.Value >= nextState.cancelTime))
return;
string stateName = nextState.changedState.ToString();
if(stateInfo.IsName(stateName))
{
isTransition = false;
nextState = new TransitionSettings();
nextState.Init();
return;
}
animator.CrossFadeInFixedTime(stateName, animTransitionDuration);
isFading = true;
}
//強制トランジション・ダメージ動作でController側から呼ばれる
public void ForcedTransition(Animator animator, PlayerState nextState)
{
string stateName = nextState.ToString();
animator.CrossFadeInFixedTime(stateName, 0.0f);
isFading = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.