126 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			126 lines
		
	
	
		
			3.3 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								文件名:StateMachineClass.nut
							 | 
						||
| 
								 | 
							
								路径:User/Object/StateMachine/StateMachineClass.nut
							 | 
						||
| 
								 | 
							
								创建日期:2024-11-29	23:14
							 | 
						||
| 
								 | 
							
								文件用途:状态机
							 | 
						||
| 
								 | 
							
								*/
							 | 
						||
| 
								 | 
							
								class FiniteStateMachineClass {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态
							 | 
						||
| 
								 | 
							
								    State = -1;
							 | 
						||
| 
								 | 
							
								    //状态时间
							 | 
						||
| 
								 | 
							
								    StateTime = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态注册组
							 | 
						||
| 
								 | 
							
								    StateFuncArr = null;
							 | 
						||
| 
								 | 
							
								    //条件注册组
							 | 
						||
| 
								 | 
							
								    ConditionStateArr = null;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    constructor() {
							 | 
						||
| 
								 | 
							
								        //初始化
							 | 
						||
| 
								 | 
							
								        StateFuncArr = {};
							 | 
						||
| 
								 | 
							
								        //初始化
							 | 
						||
| 
								 | 
							
								        ConditionStateArr = {};
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态检查
							 | 
						||
| 
								 | 
							
								    function OnCheck(gState) {
							 | 
						||
| 
								 | 
							
								        if ("OnCheck" in StateFuncArr[gState]) {
							 | 
						||
| 
								 | 
							
								            local Flag = StateFuncArr[gState].OnCheck();
							 | 
						||
| 
								 | 
							
								            if (Flag) return Flag;
							 | 
						||
| 
								 | 
							
								            else return false;
							 | 
						||
| 
								 | 
							
								        } else return true;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态进入
							 | 
						||
| 
								 | 
							
								    function OnStart() {
							 | 
						||
| 
								 | 
							
								        if ("OnStart" in StateFuncArr[State]) StateFuncArr[State].OnStart();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态退出
							 | 
						||
| 
								 | 
							
								    function OnEnd() {
							 | 
						||
| 
								 | 
							
								        if ("OnEnd" in StateFuncArr[State]) StateFuncArr[State].OnEnd();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态中全帧
							 | 
						||
| 
								 | 
							
								    function OnProc(dt) {
							 | 
						||
| 
								 | 
							
								        if ("OnProc" in StateFuncArr[State]) StateFuncArr[State].OnProc(dt, StateTime);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态中144
							 | 
						||
| 
								 | 
							
								    function OnProcFrom144(MillisecondsDuration) {
							 | 
						||
| 
								 | 
							
								        if ("OnProcCon" in StateFuncArr[State]) StateFuncArr[State].OnProcCon(MillisecondsDuration);
							 | 
						||
| 
								 | 
							
								        foreach(Key, Func in ConditionStateArr) {
							 | 
						||
| 
								 | 
							
								            if (Func()) ChangeState(Key);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //注册普通状态
							 | 
						||
| 
								 | 
							
								    function RegisterStaticState(StateIndex, T) {
							 | 
						||
| 
								 | 
							
								        StateFuncArr.rawset(StateIndex, T);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //注册条件状态
							 | 
						||
| 
								 | 
							
								    function RegisterConditionState(StateIndex, Func) {
							 | 
						||
| 
								 | 
							
								        ConditionStateArr.rawset(StateIndex, Func);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //更改状态
							 | 
						||
| 
								 | 
							
								    function ChangeState(StateIndex) {
							 | 
						||
| 
								 | 
							
								        //判断有这个状态才执行
							 | 
						||
| 
								 | 
							
								        if (StateIndex in StateFuncArr) {
							 | 
						||
| 
								 | 
							
								            //如果有检查 先调用检查
							 | 
						||
| 
								 | 
							
								            local Flag = OnCheck(StateIndex);
							 | 
						||
| 
								 | 
							
								            //如果Flag成立
							 | 
						||
| 
								 | 
							
								            if (Flag) {
							 | 
						||
| 
								 | 
							
								                //如果自身状态不等于-1
							 | 
						||
| 
								 | 
							
								                if (State != -1) {
							 | 
						||
| 
								 | 
							
								                    //调用状态退出
							 | 
						||
| 
								 | 
							
								                    OnEnd();
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                //更新状态
							 | 
						||
| 
								 | 
							
								                State = StateIndex;
							 | 
						||
| 
								 | 
							
								                //重置状态时间
							 | 
						||
| 
								 | 
							
								                StateTime = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								                //调用状态进入
							 | 
						||
| 
								 | 
							
								                OnStart();
							 | 
						||
| 
								 | 
							
								                return true;
							 | 
						||
| 
								 | 
							
								            } else return false;
							 | 
						||
| 
								 | 
							
								        } else return false;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //Ani播放KeyFlag
							 | 
						||
| 
								 | 
							
								    function ChangeAniKeyFlag(Index) {
							 | 
						||
| 
								 | 
							
								        if ("OnAniKeyFlag" in StateFuncArr[State]) StateFuncArr[State].OnAniKeyFlag(Index);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //Ani播放完成回调
							 | 
						||
| 
								 | 
							
								    function ChangeAniEndFlag() {
							 | 
						||
| 
								 | 
							
								        if ("OnAniEndFlag" in StateFuncArr[State]) StateFuncArr[State].OnAniEndFlag();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //攻击到目标的回调
							 | 
						||
| 
								 | 
							
								    function ChangeOnAttack(Damager) {
							 | 
						||
| 
								 | 
							
								        if ("OnAttack" in StateFuncArr[State]) StateFuncArr[State].OnAttack(Damager);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态机持续执行函数 144帧
							 | 
						||
| 
								 | 
							
								    function OnUpdateFrom144(MillisecondsDuration) {
							 | 
						||
| 
								 | 
							
								        //如果自身状态不等于-1
							 | 
						||
| 
								 | 
							
								        if (State != -1) {
							 | 
						||
| 
								 | 
							
								            //状态中144
							 | 
						||
| 
								 | 
							
								            OnProcFrom144(MillisecondsDuration);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    //状态机持续执行函数
							 | 
						||
| 
								 | 
							
								    function OnUpdate(dt) {
							 | 
						||
| 
								 | 
							
								        StateTime += dt;
							 | 
						||
| 
								 | 
							
								        //如果自身状态不等于-1
							 | 
						||
| 
								 | 
							
								        if (State != -1) {
							 | 
						||
| 
								 | 
							
								            //状态中
							 | 
						||
| 
								 | 
							
								            OnProc(dt);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |