///@brief 为状态机添加子状态,以及子状态对应的subStateController ///@param name是子状态的名称,例如进入房间1,进入boss房间,钥匙获取战 ///@param substate 是子状态的名称,对应的脚本应该提前挂载到StateController中 public bool addSubscene(int stateId, string subSceneClassName) { if (!mapToSubState.ContainsKey(stateId)) { //这里一定要注意要将subState冻结掉,不然一堆substate会出事 Subscene sub = ((GetComponent(subSceneClassName)) as Subscene); sub.enabled = false; mapToSubState.Add(stateId, sub); return(true); } else { return(false); } }
///@brief update函数负责不断查询当前状态是否转移,因为在初始时所有的子状态脚本都被添加到了状态管理器上,并且将enable设置为false public void Update() { Subscene sub = ((mapToSubState[currentStateId]) as Subscene); if (sub != null && sub.isTransitionTriggered()) { //](currentStateId); //执行善后工作 sub.onSubsceneDestory(); //不再执行update操作控制逻辑 sub.enabled = false; previousStateId = currentStateId; //更换subscene currentStateId = sub.GetNextSubscene(); //不要忘记设置enable ((mapToSubState[currentStateId]) as Subscene).enabled = true; ((mapToSubState[currentStateId]) as Subscene).onSubsceneInit(); } }