cocos2d-lua 3.0~3.15通用关于状态机使用的其他样例 <2> 在场景转换中使用状态机

by wangyi

状态机封装


local StateMachine           = require("framework.cc.components.behavior.StateMachine");
local AppLoginState          = require("app.State.AppLoginState");
local AppStartState          = require("app.State.AppStartState");
local GameMainScenseState    = require("app.State.GameMainScenseState");
local GameBattleState        = require("app.State.GameBattleState");


--endregion

GameState = {};

function GameState:Create()
    --注册游戏中的状态
    local pFMS = StateMachine.new();
    local tbSetup = {
         events = {
--              {name = "AppStart",             from = {"none","BattleScense"},            to = "MainScense"},
--              {name = "GameBattle",           from = "MainScense",                       to = "BattleScense"},
            {name = "AppStart",             from = "none",                             to = "PlayVideo"},      --程序开始,播放开场动画
            {name = "AppLogin",             from = {"PlayVideo","MainScense"},         to = "Login"},          --进入登陆界面
            {name = "GameMainScense",       from = {"Login","BattleScense"},           to = "MainScense"},     --进入游戏主场景,基地界面
            {name = "GameBattle",           from = "MainScense",                       to = "BattleScense"},   --进入战斗界面
            {name = "QiutApp",              from = {"MainScense","Login"},             to = "none"},           --退出游戏
         },
         callbacks = {
              onbeforestart = function(event) print("[FSM] STARTING UP") end,
              onstart = function(event) print("[FSM] READY") end,
              --AppStart
              onbeforeAppStart      = function(event) handler(AppStartState,AppStartState.onBeforeAppStart)(event) end,
              onenterPlayVideo      = function(event) handler(AppStartState,AppStartState.onEnterPlayVideo)(event) end,
              onleaveBattleScense   = function(event) handler(AppStartState,AppStartState.onLeaveGameBattle)(event) end,
              onenterMainScense     = function(event) handler(AppStartState,AppStartState.onEnterMainScense)(event) end,
              onafterAppStart       = function(event) handler(AppStartState,AppStartState.onAfterAppStart)(event) end,
              --AppLogin
              onbeforeAppLogin  = function(event) handler(AppLoginState,AppLoginState.onBeforeAppLogin)(event) end,
              onleavePlayVideo  = function(event) handler(AppLoginState,AppLoginState.onLeavePlayVideo)(event) end,
              onleaveMainScense = function(event) handler(AppLoginState,AppLoginState.onLeaveMainScense)(event) end,
              onenterLogin      = function(event) handler(AppLoginState,AppLoginState.onEnterLogin)(event) end,
              onafterAppLogin   = function(event) handler(AppLoginState,AppLoginState.onAfterAppLogin)(event) end,
              --GameMainScense
              onbeforeGameMainScense = function(event) handler(GameMainScenseState,GameMainScenseState.onBeforeMainScense)(event) end,
              onleaveLogin           = function(event) handler(GameMainScenseState,GameMainScenseState.onLeaveLogin)(event) end,
              onleaveBattleScense    = function(event) handler(GameMainScenseState,GameMainScenseState.onLeaveBattleScense)(event) end,
              onenterMainScense      = function(event) handler(GameMainScenseState,GameMainScenseState.onEnterMainScense)(event) end,
              onafterGameMainScense  = function(event) handler(GameMainScenseState,GameMainScenseState.onAfterMainScense)(event) end,
              --GameBattle
              onbeforeGameBattle  = function(event) handler(GameBattleState,GameBattleState.onBeforeBattle)(event) end,
              onleaveMainScense   = function(event) handler(GameBattleState,GameBattleState.onLeaveMainScense)(event) end,
              onenterBattleScense = function(event) handler(GameBattleState,GameBattleState.onEnterBattle)(event) end,
              onafterGameBattle   = function(event) handler(GameBattleState,GameBattleState.onAfterBattle)(event) end,
         }
    }
    pFMS:setupState(tbSetup);
    self.pGameState = pFMS;
end

function GameState:SetState(_szState)
    assert(self.pGameState);
    self.pGameState:doEvent(_szState);
end


AppStartState 文件

local AppStartState = {};

function AppStartState:onBeforeAppStart(_event)
     print("onBeforeAppStart");
end

function AppStartState:onAfterAppStart(_event)
     print("onAfterAppStart");
     GameState:SetState("AppLogin");
     
end

function AppStartState:onEnterPlayVideo(_event)
     print("onEnterPlayVideo");
end

function AppStartState:onEnterMainScense(_event)
     print("onEnterMainScense");
   --  GameState:SetState("GameMainScense");
end

return AppStartState;

创建

    GameState:Create();
    GameState:SetState("AppStart");

你可能感兴趣的:(cocos2d-lua 3.0~3.15通用关于状态机使用的其他样例 <2> 在场景转换中使用状态机)