OOFSM(面向对象)

clipboard.png

三个接口
IStateMachine IState ITransition
三个类
MyStateMachine MyState MyTransition

--- IStateMachine

using UnityEngine;
using System.Collections;

namespace FSM
{
/// <summary>
/// 状态机接口
/// </summary>
public interface IStateMachine
{
/// <summary>
/// 当前状态
/// </summary>
IState _CurrentState { get; }

    /// <summary>
    /// 默认状态
    /// </summary>
    IState _DefaultState { get; set; }

    /// <summary>
    /// 添加状态
    /// </summary>
    /// <param name="state">要添加的状态</param>
    void _AddState(IState state);

    /// <summary>
    /// 删除状态
    /// </summary>
    /// <param name="state">要删除的状态</param>
    void _RemoveState(IState state);

    /// <summary>
    /// 通过制定 Tag 获取状态
    /// </summary>
    /// <param name="stateTag">要获取状态的 Tag </param>
    /// <returns>获取到的状态</returns>
    IState _GetState(string stateTag);
}

}

--- IState

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace FSM
{
/// <summary>
/// 状态接口
/// </summary>
public interface IState
{
/// <summary>
/// 状态名
/// </summary>
string _Name { get; }

    /// <summary>
    /// 状态标签
    /// </summary>
    string _Tag { get; set; }

    /// <summary>
    /// 当前状态的状态机
    /// </summary>
    IStateMachine _Parent { get; set; }

    /// <summary>
    /// 从进入状态开始的计时器
    /// </summary>
    float _Timer { get; }

    /// <summary>
    /// 状态过渡
    /// </summary>
    List<ITransition> _TransitionList { get; }

    /// <summary>
    /// 添加状态过渡
    /// </summary>
    /// <param name="transition"> 要添加的过渡 </param>
    void _AddTransition(ITransition transition);





    /// <summary>
    /// 进入状态时的回调函数
    /// </summary>
    /// <param name="prevState">上一个状态</param>
    void _EnterCallBack(IState prevState);

    /// <summary>
    /// FixedUpdate的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    void _FixedUpdateCallBack();

    /// <summary>
    /// Update的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    void _UpdateCallBack(float deltaTime);

    /// <summary>
    /// LateUpdate的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    void _LateUpdateCallBack(float deltaTime);

    /// <summary>
    /// 退出状态时的回调函数
    /// </summary>
    /// <param name="nextState">下一个状态</param>
    void _ExitCallBack(IState nextState);

}

}

--- ITransition

using UnityEngine;
using System.Collections;

namespace FSM
{
/// <summary>
/// 状态过渡接口
/// </summary>
public interface ITransition
{
/// <summary>
/// 由此状态开始过渡
/// </summary>
IState _From { get; set; }

    /// <summary>
    /// 过渡到此状态
    /// </summary>
    IState _To { get; set; }

    /// <summary>
    /// 过渡名
    /// </summary>
    string _Name { get; set; }

    /// <summary>
    /// 用于判断是否过渡到目标状态
    /// </summary>
    /// <returns> true 表示过渡结束可以执行目标状态, false 表示过渡还未结束继续执行原状态 </returns>
    bool _TransitionCallBack();

    /// <summary>
    /// 是否开始过渡
    /// </summary>
    /// <returns> true 为开始过渡 </returns>
    bool _WhetherBegin();
}

}

--- MyStateMachine

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace FSM
{
public class MyStateMachine : MyState, IStateMachine
{
private List<IState> mStateList;

    private IState mCurrentState;
    /// <summary>
    /// 当前状态
    /// </summary>
    public IState _CurrentState
    {
        get
        {
            return mCurrentState;
        }
    }

    private IState mDefaultState;
    /// <summary>
    /// 默认状态
    /// </summary>
    public IState _DefaultState
    {
        get
        {
            return mDefaultState;
        }
        set
        {
            //避免给默认状态赋值一个状态机中不存在的状态
            //如果不为空就添加到状态机中然后给默认状态赋值,如果为空就不添加,但是依旧可以给默认状态赋值
            _AddState(value);
            mDefaultState = value;
        }
    }


    /// <summary>
    /// 是否正在过渡状态
    /// </summary>
    private bool isTransition = false;

    /// <summary>
    /// 当前正在执行的过渡
    /// </summary>
    private ITransition mTran;



    /// <summary>
    /// 状态机构造函数
    /// </summary>
    /// <param name="name"> 状态名 </param>
    /// <param name="state"> 默认状态 </param>
    public MyStateMachine(string name, IState state)
        : base(name)
    {
        mStateList = new List<IState>();
        mDefaultState = state;
    }

    /// <summary>
    /// 添加状态
    /// </summary>
    /// <param name="state">要添加的状态</param>
    public void _AddState(IState state)
    {
        if (state != null && !mStateList.Contains(state))
        {
            mStateList.Add(state);
            state._Parent = this;
            if (mDefaultState == null)
            {
                mDefaultState = state;
            }
        }
    }

    /// <summary>
    /// 删除状态
    /// </summary>
    /// <param name="state">要删除的状态</param>
    public void _RemoveState(IState state)
    {
        //不可以删除当前状态
        if (state == _CurrentState)
            return;

        if (state != null && mStateList.Contains(state))
        {
            mStateList.Remove(state);
            state._Parent = null;
            if (mDefaultState == state)
            {
                mDefaultState = mStateList.Count >= 1 ? mStateList[0] : null;
            }
        }
    }

    /// <summary>
    /// 获取状态
    /// </summary>
    /// <param name="stateTag"> 要获取的状态 </param>
    /// <returns> 获取到的状态 </returns>
    public IState _GetState(string stateTag)
    {
        return null;
    }




    public override void _UpdateCallBack(float deltaTime)
    {
        if (isTransition)
        {
            if (mTran != null && mTran._TransitionCallBack())
            {
                DoTransition(mTran);
                isTransition = false;
            }
            return;
        }

        base._UpdateCallBack(deltaTime);

        if (mCurrentState == null)
        {
            mCurrentState = mDefaultState;
        }

        List<ITransition> tranList = mCurrentState._TransitionList;
        ITransition t;
        int count = tranList.Count;

        for (int i = 0; i < count; i++)
        {
            t = tranList[i];
            if (t._WhetherBegin())
            {
                isTransition = true;
                mTran = t;
                return;
            }
        }
        mCurrentState._UpdateCallBack(deltaTime);
    }

    public override void _FixedUpdateCallBack()
    {
        base._FixedUpdateCallBack();
        mCurrentState._FixedUpdateCallBack();
    }

    public override void _LateUpdateCallBack(float deltaTime)
    {
        base._LateUpdateCallBack(deltaTime);
        mCurrentState._LateUpdateCallBack(deltaTime);
    }

    /// <summary>
    /// 执行过渡,过渡到目标状态
    /// </summary>
    /// <param name="t"> 要执行的过渡 </param>
    private void DoTransition(ITransition t)
    {
        mCurrentState._ExitCallBack(t._To);
        mCurrentState = t._To;
        mCurrentState._EnterCallBack(t._From);
    }


}

}

--- MyState

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace FSM
{
public delegate void MyStateDelegate_Not_Not();
public delegate void MyStateDelegate_Not_IState(IState state);
public delegate void MyStateDelegate_Not_Float(float f);

public class MyState : IState
{
    private List<ITransition> mTransitionList;
    /// <summary>
    /// 状态过渡
    /// </summary>
    public List<ITransition> _TransitionList
    {
        get
        {
            return mTransitionList;
        }
    }

    /// <summary>
    /// 进入状态时的事件
    /// </summary>
    public event MyStateDelegate_Not_IState OnEnter;

    /// <summary>
    /// 退出状态时的事件
    /// </summary>
    public event MyStateDelegate_Not_IState OnExit;

    /// <summary>
    /// FixedUpdate 时调用的事件
    /// </summary>
    public event MyStateDelegate_Not_Not OnFixedUpdate;

    /// <summary>
    /// Update 时调用的事件
    /// </summary>
    public event MyStateDelegate_Not_Float OnUpdate;

    /// <summary>
    /// LateUpdate 时调用的事件
    /// </summary>
    public event MyStateDelegate_Not_Float OnLateUpdate;

    private string mName;
    /// <summary>
    /// 状态名
    /// </summary>
    public string _Name
    {
        get { return mName; }
    }

    private string mTag;
    /// <summary>
    /// 状态标签
    /// </summary>
    public string _Tag
    {
        get
        {
            return mTag;
        }
        set
        {
            mTag = value;
        }
    }

    private IStateMachine mParent;
    /// <summary>
    /// 当前状态的状态机
    /// </summary>
    public IStateMachine _Parent
    {
        get
        {
            return mParent;
        }
        set
        {
            mParent = value;
        }
    }

    private float mTimer;
    /// <summary>
    /// 从进入状态开始的计时器
    /// </summary>
    public float _Timer
    {
        get
        {
            return mTimer;
        }
    }





    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="name"> 状态名 </param>
    public MyState(string name)
    {
        mName = name;
        mTransitionList = new List<ITransition>();
    }



    /// <summary>
    /// 添加状态过渡
    /// </summary>
    /// <param name="transition"> 要添加的过渡 </param>
    public void _AddTransition(ITransition transition)
    {
        if (transition != null && !mTransitionList.Contains(transition))
        {
            mTransitionList.Add(transition);
        }
    }

    /// <summary>
    /// 进入状态时的回调函数
    /// </summary>
    /// <param name="prevState">上一个状态</param>
    public virtual void _EnterCallBack(IState prevState)
    {
        //重置计时器
        mTimer = 0;

        if (OnEnter != null)
        {
            OnEnter(prevState);
        }
    }

    /// <summary>
    /// 退出状态时的回调函数
    /// </summary>
    /// <param name="nextState">下一个状态</param>
    public virtual void _ExitCallBack(IState nextState)
    {
        //重置计时器
        mTimer = 0;

        if (OnExit != null)
        {
            OnExit(nextState);
        }
    }

    /// <summary>
    /// FixedUpdate的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    public virtual void _FixedUpdateCallBack()
    {
        if (OnFixedUpdate != null)
        {
            OnFixedUpdate();
        }
    }

    /// <summary>
    /// Update的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    public virtual void _UpdateCallBack(float deltaTime)
    {
        //累加计时器
        mTimer += deltaTime;

        if (OnUpdate != null)
        {
            OnUpdate(deltaTime);
        }
    }

    /// <summary>
    /// LateUpdate的回调函数
    /// </summary>
    /// <param name="deltaTime">Time.deltatime</param>
    public virtual void _LateUpdateCallBack(float deltaTime)
    {
        if (OnLateUpdate != null)
        {
            OnLateUpdate(deltaTime);
        }
    }

}

}

--- MyTransition

using UnityEngine;
using System.Collections;

namespace FSM
{

public delegate bool MyTransition_Bool_Not();

public class MyTransition : ITransition
{
    /// <summary>
    /// 过渡状态
    /// </summary>
    public event MyTransition_Bool_Not OnTransition;
    /// <summary>
    /// 检测是否过渡
    /// </summary>
    public event MyTransition_Bool_Not OnDetection;

    private IState mFrom;
    /// <summary>
    /// 当前状态
    /// </summary>
    public IState _From
    {
        get
        {
            return mFrom;
        }
        set
        {
            mFrom = value;
        }
    }

    private IState mTo;
    /// <summary>
    /// 目标状态
    /// </summary>
    public IState _To
    {
        get
        {
            return mTo;
        }
        set
        {
            mTo = value;
        }
    }

    private string mName;
    /// <summary>
    /// 过渡名
    /// </summary>
    public string _Name
    {
        get
        {
            return mName;
        }
        set
        {
            mName = value;
        }
    }



    /// <summary>
    /// 过渡构造
    /// </summary>
    /// <param name="name">过渡名</param>
    /// <param name="FState">当前状态</param>
    /// <param name="TState">目标状态</param>
    public MyTransition(string name, IState FState, IState TState)
    {
        mName = name;
        mFrom = FState;
        mTo = TState;
    }


    /// <summary>
    /// 用于判断执行过渡状态时是否过渡到目标状态
    /// </summary>
    /// <returns> true 表示执行过渡状态结束可以过渡到目标状态, false 表示过渡状态还未结束继续执行过渡状态直到过渡状态结束为止 </returns>
    public bool _TransitionCallBack()
    {
        if (OnTransition != null)
        {
            return OnTransition();
        }
        return true;
    }

    /// <summary>
    /// 是否开始执行过渡状态
    /// </summary>
    /// <returns> true 为开始执行 </returns>
    public bool _WhetherBegin()
    {
        if (OnDetection != null)
        {
            return OnDetection();
        }
        return true;
    }

}

}

--------------------------------------------------------------------------------------------分割线

///////////////////////////////////使用例子

1 灯光的状态---打开、关闭、忽明忽暗、变颜色等

using UnityEngine;
using System.Collections;
using FSM;

public class LightState : MonoBehaviour
{
public int mMaxIntensity = 2;
public float mLightFadeSpeed = 1;

private MyStateMachine mLightFSM;
private MyStateMachine thisOpen;
private MyState thisClose;
private MyTransition mOpenToClose;
private MyTransition mCloseToOpen;

private MyState mChangeIntensity;
private MyState mChangeColor;
private MyTransition mIntensityToColor;
private MyTransition mColorToIntensity;

private Light thisLight;
private Color mTargetColor;
private Color mStartColor;
private float mColorTimer = 0;
private bool mToChangeColor = false;

private bool mIsOpen = true;
private bool mIsAnimation = false;
private bool mIsReset = false;
private float mFlickerNum;
void Start()
{
    thisLight = this.GetComponent<Light>();

    InitFSM();



    mIsOpen = true;
}

/// <summary>
/// 初始化状态机
/// </summary>
private void InitFSM()
{
    mChangeIntensity = new MyState("ChangeIntensity");
    mChangeIntensity.OnEnter += (IState state) => { mIsAnimation = false; };
    mChangeIntensity.OnUpdate += _LightFlicker;

    mChangeColor = new MyState("ChangeColor");
    mChangeColor.OnEnter += (IState state) => { mIsAnimation = false; };
    mChangeColor.OnUpdate += (f) =>
    {
        if (mIsAnimation)
        {
            if (mColorTimer >= 1)
            {
                mIsAnimation = false;
                return;
            }
            else
            {
                mColorTimer += f * 2.5f;
                thisLight.color = Color.Lerp(mStartColor, mTargetColor, mColorTimer);
            }
        }
        else
        {
            float r = Random.Range(0, 1f);
            float g = Random.Range(0, 1f);
            float b = Random.Range(0, 1f);
            mTargetColor = new Color(r, g, b, 1);
            mStartColor = thisLight.color;
            mColorTimer = 0;
            mIsAnimation = true;
        }
    };

    mIntensityToColor = new MyTransition("IntensityToColor", mChangeIntensity, mChangeColor);
    mIntensityToColor.OnDetection += () => { return mToChangeColor; };
    mChangeIntensity._AddTransition(mIntensityToColor);

    mColorToIntensity = new MyTransition("ColorToIntensity", mChangeColor, mChangeIntensity);
    mColorToIntensity.OnDetection += () => { return !mToChangeColor; };
    mChangeColor._AddTransition(mColorToIntensity);

    thisOpen = new MyStateMachine("OpenLightState", mChangeIntensity);
    thisOpen._AddState(mChangeColor);

    thisClose = new MyState("CloseLightState");
    thisClose.OnEnter += (IState state) => { thisLight.intensity = 0; };


    mOpenToClose = new MyTransition("OpenToClose", thisOpen, thisClose);
    mOpenToClose.OnDetection += () => { return !mIsOpen; };
    mOpenToClose.OnTransition += () =>
    {
        return _LightFade(0);
    };
    thisOpen._AddTransition(mOpenToClose);

    mCloseToOpen = new MyTransition("CloseToOpen", thisClose, thisOpen);
    mCloseToOpen.OnDetection += () => { return mIsOpen; };
    mCloseToOpen.OnTransition += () =>
    {
        return _LightFade(mMaxIntensity);
    };
    thisClose._AddTransition(mCloseToOpen);

    mLightFSM = new MyStateMachine("LightMachine", thisClose);
    mLightFSM._AddState(thisOpen);

}

/// <summary>
/// 光强渐变
/// </summary>
/// <param name="intensity"> 目标光强 </param>
/// <returns> true 渐变结束 , false 继续渐变 </returns>
private bool _LightFade(float intensity)
{
    if (Mathf.Abs(intensity - thisLight.intensity) >= 0.1f)
    {
        float direction = intensity > thisLight.intensity ? 1 : -1;
        thisLight.intensity += Time.deltaTime * mLightFadeSpeed * direction;
    }
    else
    {
        thisLight.intensity = intensity;
        return true;
    }
    return false;
}


private void _LightFlicker(float f)
{
    if (mIsAnimation)
    {
        if (!mIsReset)
        {
            if (_LightFade(mMaxIntensity))
            {
                mIsReset = true;
                mIsAnimation = false;
                return;
            }
        }
        else
        {
            if (_LightFade(mFlickerNum))
            {
                mIsReset = false;
                return;
            }
        }
    }
    else
    {
        mFlickerNum = Random.Range(0.5f, 1.6f);
        mIsAnimation = true;
    }

}


void Update()
{
    mLightFSM._UpdateCallBack(Time.deltaTime);

}

void OnGUI()
{
    if (GUI.Button(new Rect(200, 10, 100, 30), "turn on"))
    {
        mIsOpen = true;
    }
    if (GUI.Button(new Rect(200, 50, 100, 30), "turn off"))
    {
        mIsOpen = false;
    }
    if (GUI.Button(new Rect(100, 10, 100, 30), "Color"))
    {
        mToChangeColor = true;
    }
    if (GUI.Button(new Rect(100, 50, 100, 30), "Intensity"))
    {
        mToChangeColor = false;
    }
}

}

2 人物状态控制---移动、静止、攻击、连击、跳跃等

using UnityEngine;
using System.Collections;
using FSM;

public partial class YuKaState : MonoBehaviour {

void Start () {
    mYuKaAnim = this.GetComponent<Animator> ();
    mRig = this.GetComponent<Rigidbody> ();
    _InitFSM ();
    _InitData ();

    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}

void Update () {
    mFSM_YuKa._UpdateCallBack (Time.deltaTime);
    if (Input.GetKeyDown (KeyCode.Escape))
        Cursor.visible = Cursor.visible == false ? true : false;
    float f = Input.GetAxis ("Mouse X");
    this.transform.Rotate (new Vector3 (0, f, 0));
}


private int IdleCTimes = 0;
/// <summary>
/// 切换待机状态
/// 挂载在动画里面调用
/// </summary>
public void IdleC_TO_IdleB(){
    IdleCTimes++;
    if (IdleCTimes == 3) {
        mYuKaAnim.SetBool ("idle_ctob", true);
        IdleCTimes = 0;
    }
}

}

/// <summary>
/// 成员类
/// </summary>
public partial class YuKaState : MonoBehaviour {
private Animator mYuKaAnim;
private Rigidbody mRig;

/*-------------\
|  FSM_Member  |
\-------------*/

MyStateMachine mFSM_YuKa;

MyState mIdle;
MyStateMachine mAction;

MyTransition mIdle_To_mAction;
MyTransition mAction_To_mIdle;
private bool mIsAction;


//Action Of State
private bool mIsATKOrIsJump;

MyState mRun;
MyState mWalkBack;
private float mMoveSpeed;
private float mAddSpeed;

MyTransition mRun_To_mWalkBack;
MyTransition mWalkBack_To_mRun;
private bool mIsRun;

MyState mATK1;
MyState mATK2;

MyTransition mRun_To_mATK1;
MyTransition mATK1_To_mRun;
private bool mIsATK1;
MyTransition mATK1_To_mATK2;
MyTransition mATK2_To_mIdle;
private bool mIsATK2;

}

/// <summary>
/// FSM Function类
/// </summary>
public partial class YuKaState : MonoBehaviour {

private void _InitFSM(){
    //mRun------------------
    mRun = new MyState ("Run");
    mRun.OnEnter += (state) => {
        print ("Enter---" + mRun._Name);
        float v = Input.GetAxis ("Vertical");
        if (v > 0)
            mYuKaAnim.SetInteger ("move", 1);
        if (state != null && state._Name == "Walkback")
            mYuKaAnim.SetInteger ("move", 1);
        mMoveSpeed = 5;
    };
    mRun.OnUpdate += _IdleState_To_ActionState;
    mRun.OnUpdate += _Run_To_OtherActions;
    mRun.OnUpdate += (f) => {
        mAddSpeed = Input.GetKey (KeyCode.F) ? 3 : 1;
        this.transform.position += this.transform.forward * mMoveSpeed * mAddSpeed * f;
    };
    mRun.OnExit += (state) => {
        Debug.LogError ("Exit---" + mRun._Name);
        if (state._Name != "ATK1")
            mYuKaAnim.SetInteger ("move", 0);
    };

    //mWalkBack------------------
    mWalkBack = new MyState ("Walkback");
    mWalkBack.OnEnter += (state) => {
        print ("Enter---" + mWalkBack._Name);
        mYuKaAnim.SetInteger ("move", -1);
        mMoveSpeed = 1;
    };
    mWalkBack.OnUpdate += (f) => {
        if (Input.GetKey (KeyCode.W) && !Input.GetKey (KeyCode.S)) {
            mIsRun = true;
        }
    };
    mWalkBack.OnUpdate += (f) => {
        float v = Input.GetAxis ("Vertical");
        if (v > -0.1f && !Input.GetKey (KeyCode.W)) {
            mIsAction = false;
            mIsRun = true;
        }
    };
    mWalkBack.OnUpdate += (f) => {
        this.transform.position += -this.transform.forward * mMoveSpeed * f;
    };
    mWalkBack.OnExit += (state) => {
        Debug.LogError ("Exit---" + mWalkBack._Name);
        mYuKaAnim.SetInteger ("move", 0);
    };

    //mRun_To_mWalkBack------------------
    mRun_To_mWalkBack = new MyTransition ("Run_To_WalkBack", mRun, mWalkBack);
    mRun_To_mWalkBack.OnDetection += () => {
        return !mIsRun;
    };
    mRun._AddTransition (mRun_To_mWalkBack);

    //mWalkBack_To_mRun------------------
    mWalkBack_To_mRun = new MyTransition ("WalkBack_To_Run", mWalkBack, mRun);
    mWalkBack_To_mRun.OnDetection += () => {
        return mIsRun;
    };
    mWalkBack._AddTransition (mWalkBack_To_mRun);

    //mATK1------------------
    mATK1 = new MyState ("ATK1");
    mATK1.OnEnter += (state) => {
        print ("Enter---" + mATK1._Name);
        mYuKaAnim.SetInteger ("atk", 1);
        mRig.velocity = Vector3.zero;
        mRig.velocity = this.transform.forward * 10 + new Vector3 (0, 1, 0) * 10;
    };
    mATK1.OnUpdate += (f) => {
        if (mYuKaAnim.GetCurrentAnimatorStateInfo (0).IsName ("Atk1")) {
            if (mYuKaAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 0.9f && !mIsATK2)
                mIsATK1 = false;
            if (mYuKaAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 0.7f &&
                Input.GetKeyDown (KeyCode.E)) {
                mIsATK2 = true;
                mIsATK1 = false;
            } else
                mIsATK2 = false;
        }
    };
    mATK1.OnExit += (state) => {
        Debug.LogError ("Exit---" + mATK1._Name);
        int num = state._Name == "ATK2" ? 2 : 0;
        mYuKaAnim.SetInteger ("atk", num);
    };

    //mATK2
    mATK2 = new MyState ("ATK2");
    mATK2.OnEnter += (state) => {
        print ("Enter---" + mATK2._Name);
        mYuKaAnim.SetInteger ("atk", 2);
        mRig.velocity = Vector3.zero;
        mRig.velocity = this.transform.forward * 10;
        mIsATK1 = false;
    };
    mATK2.OnUpdate += (f) => {
        if (mYuKaAnim.GetCurrentAnimatorStateInfo (0).IsName ("Atk2")) {
            if (mYuKaAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 0.95f)
                mIsATK2 = false;
        }
    };

    //mRun_To_mATK1
    mRun_To_mATK1 = new MyTransition ("Run_To_ATK1", mRun, mATK1);
    mRun_To_mATK1.OnDetection += () => {
        return mIsATK1;
    };
    mRun._AddTransition (mRun_To_mATK1);

    //mATK1_To_mRun
    mATK1_To_mRun = new MyTransition ("Run_To_ATK1", mATK1, mRun);
    mATK1_To_mRun.OnDetection += () => {
        return !mIsATK1;
    };
    mATK1._AddTransition (mATK1_To_mRun);

    //mATK1_To_mATK2
    mATK1_To_mATK2 = new MyTransition ("ATK1_To_ATK2", mATK1, mATK2);
    mATK1_To_mATK2.OnDetection += () => {
        return mIsATK2;
    };
    mATK1._AddTransition (mATK1_To_mATK2);

    //mATK2_To_mIdle
    mATK2_To_mIdle = new MyTransition ("ATK2_To_Idle", mATK2, mIdle);
    mATK2_To_mIdle.OnDetection += () => {
        return !mIsATK2;
    };
    mATK2._AddTransition (mATK2_To_mIdle);








    //mIdle------------------
    mIdle = new MyState ("Idle");
    mIdle.OnEnter += (IState state) => {
        Debug.LogError ("-----Enter-----" + mIdle._Name);
        mYuKaAnim.SetInteger ("move", 0);
        mYuKaAnim.SetInteger ("atk", 0);
        mYuKaAnim.SetInteger ("jump", 0);
    };
    mIdle.OnUpdate += _IdleState_To_ActionState;
    mIdle.OnExit += (state) => {
        Debug.LogError ("-----Exit-----" + mIdle._Name);
    };

    //mAction------------------
    mAction = new MyStateMachine ("Action", mRun);
    mAction._AddState (mWalkBack);
    mAction.OnEnter += (IState state) => {
        Debug.LogError ("-----Enter-----" + mAction._Name);
        mRun._EnterCallBack (mIdle);
    };
    mAction.OnExit += (state) => {
        Debug.LogError ("-----Exit-----" + mAction._Name);
    };




    //mIdle_To_mAction------------------
    mIdle_To_mAction = new MyTransition ("Idle_To_Action", mIdle, mAction);
    mIdle_To_mAction.OnDetection += () => {
        return mIsAction;
    };
    mIdle._AddTransition (mIdle_To_mAction);
    //mAction_To_mIdle------------------
    mAction_To_mIdle = new MyTransition ("Action_To_Idle", mAction, mIdle);
    mAction_To_mIdle.OnDetection += () => {
        return !mIsAction;
    };
    mAction._AddTransition (mAction_To_mIdle);



    //mFSM_YuKa------------------
    mFSM_YuKa = new MyStateMachine ("YuKaFSM", mIdle);
    mFSM_YuKa._AddState (mAction);
}


private void _InitData(){
    mIsAction = false;
    mIsRun = true;
    mIsATKOrIsJump = false;
    mAddSpeed = 1;
    mIsATK1 = false;
}

//Idle
private void _IdleState_To_ActionState(float f){
    if (Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S))
        mIsAction = true;
    else if (!mIsATKOrIsJump)
        mIsAction = false;
    if (Input.GetKeyDown (KeyCode.E)) {
        mIsATK1 = true;
        mIsAction = true;
    }
}

//mRun
private void _Run_To_OtherActions(float f){
    if (Input.GetKey (KeyCode.S) && !Input.GetKey (KeyCode.W))
        mIsRun = false;
    if (Input.GetKey (KeyCode.E))
        mIsATK1 = true;
}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,504评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,434评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,089评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,378评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,472评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,506评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,519评论 3 413
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,292评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,738评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,022评论 2 329
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,194评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,873评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,536评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,162评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,413评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,075评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,080评论 2 352

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,646评论 18 139
  • 本节通过一个简单的射击子弹的示例来介绍Transform的用法。子弹射击本身很容易制作,只要制作一个子弹Prefa...
    OneMore2018阅读 546评论 0 2
  • 今天一整天都在开会,也没有时间想设计做设计。下午的会议上倒是有一点感悟。 我们是做问卷编辑系统,其中有一个功能叫"...
    haily0506阅读 1,392评论 6 1
  • --- 【轰轰·心语】 --- 父爱如山,感谢父亲一路走来对我的默默支持! 今天说说...
    风海晨阅读 544评论 0 1
  • 写游记,赢门票啦~ 没错!您没有看错! 石门河景区门票免费领!! 小伙伴们,心动不如行动!!! 赢免费门票有一些小...
    石门河景区阅读 210评论 0 0