/// <summary> /// 重置方法,默认都执行 /// </summary> /// <param name="state"></param> /// <param name="player"></param> public void TransitionState(PlayerBaseState state, PlayerFSMSystem player) { // Debug.Log(currentState.name + "====>" + state.name); currentState?.Exit(player); // 如果为空则不执行 currentState = state; currentState?.Enter(player); }
private void SetRootCurrentState(PlayerBaseState state) { if (this.curState == state) { return; // 避免重复转换状态 } // Debug.Log(this.curState.name + "==>" + state.name); this.curState = state; }
/// <summary> /// /// </summary> /// <param name="state"></param> /// <param name="player"></param> /// <param name="executeEnter">是否执行 Enter</param> /// <param name="executeExit">是否执行 Exit</param> public void TransitionState(PlayerBaseState state, PlayerFSMSystem player, bool executeEnter, bool executeExit) { if (executeExit) { currentState?.Exit(player); } currentState = state; if (executeEnter) { currentState?.Enter(player); // 如果为空则不执行 } }
private void Awake() { onGroundState = new OnGroundState(); inTheAirState = new InTheAirState(); inClimbState = new InClimbState(); // 把自己注册进事件中心 EventManager.Register(this, EventID.GraspWall, EventID.OnGround); rb = GetComponent <Rigidbody2D>(); coll = GetComponent <BoxCollider2D>(); ccoll = GetComponent <CapsuleCollider2D>(); // 最开始的状态 curState = onGroundState; }
/// <summary> /// 上面方法的重置 /// </summary> /// <param name="state"></param> /// <param name="executeEnter"></param> /// <param name="executeExit"></param> private void SetRootCurrentState(PlayerBaseState state, bool executeEnter, bool executeExit) { if (this.curState == state) { return; } if (executeExit) { curState?.Exit(this); } curState = state; if (executeEnter) { curState?.Enter(this); // 如果为空则不执行 } }
/// <summary> /// 取得当前的状态 /// </summary> /// <param name="state"></param> /// <returns></returns> public string GetCurrentStateName(PlayerBaseState state) { var str = ""; var i = 0; while (i < 10) // 防止死循环,最大遍历 10 次 { i++; if (state != null) { str += state.name + "==>"; state = state.currentState; continue; } break; } return(str + i); }
/// <summary> /// 设置其它对象的状态 /// </summary> /// <param name="target">被设置状态的目标</param> /// <param name="state"></param> /// <param name="player"></param> /// <param name="executeEnter"></param> /// <param name="executeExit"></param> public void TransitionOtherState(PlayerBaseState target, PlayerBaseState state, PlayerFSMSystem player, bool executeEnter, bool executeExit) { target.TransitionState(state, player, executeEnter, executeExit); }
/// <summary> /// 设置其它对象的状态 /// </summary> /// <param name="target">被设置状态的目标</param> /// <param name="state"></param> /// <param name="player"></param> public void TransitionOtherState(PlayerBaseState target, PlayerBaseState state, PlayerFSMSystem player) { target.TransitionState(state, player); }