//启动时调用 void Start() { UIController = GetComponent <GameUIController>(); gm = GetComponent <GameManager>(); //初始化GameManager静态实例gm mainCamera = Camera.main; //获取摄像机 stateMachineMap = new Dictionary <string, BaseGLStateMachine>(); gameStateQueue = new Queue <string>(); playerCustomProperties = new ExitGames.Client.Photon.Hashtable { { "Score", 0 }, { "Death", 0 } }; //初始化玩家得分 PhotonNetwork.player.SetCustomProperties(playerCustomProperties); //初始化队伍得分 currentScoreOfAttackerTeam = 0; currentScoreOfDefenderTeam = 0; //初始化状态机映射和队列 initStateMachineMapAndQueue(); //将队列首的状态机设置为当前状态机 curGLStatetMachine = stateMachineMap[gameStateQueue.Peek()]; curGLStatetMachine.Enter(); m_gameResult = GameResult.none; winTeam = null; #if (!UNITY_ANDROID) UIController.setCursorState(lockCursor); #endif }
//根据名字创建并获取状态机实例 static public BaseGLStateMachine getGLStateMachineByName(string name, GameManager gm) { BaseGLStateMachine instance = null; switch (name) { case "prepare": instance = GLStateMachineFactory <PrepareGLStateMachine> .Create(gm); break; case "end": instance = GLStateMachineFactory <EndGLStateMachine> .Create(gm); break; case "compete": instance = GLStateMachineFactory <CompeteGLStateMachine> .Create(gm); break; case "getTreasure": instance = GLStateMachineFactory <GetTreasureGLStateMachine> .Create(gm); break; case "transport": instance = GLStateMachineFactory <TransportGLStateMachine> .Create(gm); break; } return(instance); }
//根据状态机实例判断其名字 static public string getNameByGLStateMachine(BaseGLStateMachine sm) { string smName = ""; switch (sm.GetType().FullName) { case "PrepareGLStateMachine": smName = "prepare"; break; case "EndGLStateMachine": smName = "end"; break; case "CompeteGLStateMachine": smName = "compete"; break; case "GetTreasureGLStateMachine": smName = "getTreasure"; break; case "TransportGLStateMachine": smName = "transport"; break; } return(smName); }
void stateMachineChangeForce(string stateMachineName) { if (stateMachineName == null || stateMachineName == "") { return; } gameStateQueue.Clear(); //清空队列 if (curGLStatetMachine != null) { curGLStatetMachine.Exit(); } curGLStatetMachine = stateMachineMap[stateMachineName]; if (curGLStatetMachine != null) { curGLStatetMachine.Enter();//强制执行新的状态机 } }
void stateMachineChange() { if (curGLStatetMachine != null) { gameStateQueue.Dequeue(); //当前状态机移除队列 并执行exit函数 curGLStatetMachine.Exit(); } if (gameStateQueue.Count == 0) { return; } curGLStatetMachine = stateMachineMap[gameStateQueue.Peek()]; //从队列中找到新的状态机 if (curGLStatetMachine != null) { curGLStatetMachine.Enter();//执行enter函数 } }