美文网首页FSM
OOFSM(面向对象)

OOFSM(面向对象)

作者: _Arturia | 来源:发表于2018-03-01 09:19 被阅读0次
    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;
    }
    

    }

    相关文章

      网友评论

        本文标题:OOFSM(面向对象)

        本文链接:https://www.haomeiwen.com/subject/trowxftx.html