cocos-lua 实现状态机(组件模式)

状态机实现

环境: quick-cocos
简述:提供状态列表设置的接口setupState(),状态列表中包含状态名称及相关调用函数,提供执行状态函数doEvent()

1. 在状态的execute函数内编写状态切换判断的逻辑,该函数在对象重写并传入到对象的内建状态机。

2. myStateMachine继承自quick的Component类(含有事件派发机制)

代码:basestate

local BaseState = class("BaseState")
function BaseState:ctor(str)
    self.name = str
    self.stateFuncList = {}
end

local _stateFuncList = {
                    ["Enter"] = {callFunc = function () print("need init Enter") end},
                    ["Execute"] = {callFunc = function () print("need init Execute") end},
                    ["Exit"] = {callFunc = function () print("need init Exit") end},
                }

--设置传入的函数列表
function BaseState:SetStateFuncList(list)
    self.stateFuncList = list
end

--进入状态时回调
function BaseState:Enter(...)
    local fun_list = self.stateFuncList["Enter"] or _stateFuncList["Enter"]
    fun_list.callFunc(...)
end

--执行状态
function BaseState:Execute(...)
    local fun_list = self.stateFuncList["Execute"] or _stateFuncList["Execute"]
    fun_list.callFunc(...)
end

--退出状态
function BaseState:Exit(...)
    local fun_list = self.stateFuncList["Exit"] or _stateFuncList["Exit"]
    fun_list.callFunc(...)
end
`

代码: myStateMachine

local Component = import(".Component")
local BaseState = import(".base_state")
local myStateMachine = class("myStateMachine", Component)

function myStateMachine:ctor()
    myStateMachine.super.ctor(self, "myStateMachine")
    self.state_list = {}
end

--设置状态列表
function myStateMachine:setupState(cfg)
    for k,v in pairs(cfg) do
        if self.state_list[k] then
            self.state_list[k]:SetStateList(v)
        else
            self.state_list[k] = BaseState:new(k)
            self.state_list[k]:SetStateFuncList(v)
        end
    end
end

--切换状态
function myStateMachine:ChangeState(state)
    self.state = self.state or state
    self.state_list[self.state]:Exit()
    self.state = state
    self.state_list[self.state]:Enter()
    self:ChangeCallBack()
end

function myStateMachine:ChangeCallBack(state)
end

--触发状态
function myStateMachine:doEvent(state)
    self:ChangeState(state)
    if self.state_list[self.state] then 
        self.state_list[self.state]:Execute(self)
    end
end

return myStateMachine

使用:

--加载组件
self:addComponent("app.components.myStateMachine")
self.fsm__ = self:getComponent("app.components.myStateMachine")

local state_list = {
            ["idel"] = {
                ["Enter"] = {callFunc = function () print("hello,im idel") end},
                ["Execute"] = {callFunc = bindFunc(self, self.idelUpdate)},
                ["Exit"] = {callFunc = function () print("good bye") end},
            },
            ["battle"] = {
                ["Enter"] = {callFunc = function () print("hello,im battle") end},
                ["Execute"] = {callFunc = bindFunc(self, self.battleUpdate)},
                ["Exit"] = {callFunc = function () print("good bye") end},
            },  
            ["occupy"] = {
                ["Enter"] = {callFunc = function () print("hello,im occupy") end},
                ["Execute"] = {callFunc = bindFunc(self, self.occupyUpdate)},
                ["Exit"] = {callFunc = function () print("good bye") end},
            },  
        }   

self.fsm__:setupState(state_list)
self.fsm__:doEvent("idel")

你可能感兴趣的:(游戏开发)