cococococococos 2 d-lua 3.0~3.15汎用状態マシン使用に関する他の例<2>シーン変換で状態マシンを使用します.

5124 ワード

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");