lua中FSM实现


-- 有限状态机

local M = cclass("StateMachine")

function M:ctor(smName)
    self.name = smName
    self.states = {}
    self.locked = false
    self.curState = nil
end

-- 初始化所有状态
function M:InitStates(tempStates)
    if tempStates then
        for k, v in pairs(tempStates) do
            self.states[k] = v
        end
        tempStates = nil
    end
end

-- 添加状态
function M:AddState(baseState)
    self.states[baseState.stateName] = baseState
end

-- 获取状态
function M:GetState(stateName)
    return self.states[stateName]
end

-- 更新当前状态
function M:Update()
    if self.curState then
        self.curState:Update()
    end
end

-- 切换状态
function M:ChangeState(stateName)
    local state = self:GetState(stateName)
    if state == nil then
        print('状态类型未注册'..stateName)
        return
    end

    if self.curState then
        if self.locked then
            print('不允许在 OnLeave 方法中进入其它状态.')
            return
        end
        self.locked = true
        if self.curState.stateName ~= stateName then
            self.curState:OnLeave()
        end
        self.locked = false
    end
    self.curState = state
    self.curState:OnEnter()
end

-- 游戏暂停
function M:OnApplicationPause(pause)
    if self.curState then
        self.curState:OnApplicationPause(pause)
    end
end

-- 游戏退出
function M:OnApplicationQuit()
    if self.curState then
        self.curState:OnApplicationQuit()
    end
end

-- 状态机释放
function M:Release()
    for k, v in pairs(self.states) do
        if v then
            v = nil
        end
    end
    self.states = {}
    self.locked = false
    self.curState = nil
end

return M

-- 状态基类

local M = cclass("BaseState")

function M:ctor(stateName)
    self.stateName = stateName
end

function M:OnEnter()
    
end

function M:OnLeave()
    
end

function M:Update()
    
end

function M:OnApplicationPause(pause)
    
end

function M:OnApplicationQuit()
    
end

return M


-- 主场景状态

local M = cclass("MainState", require "Base.BaseState")

function M:ctor(stateName)
    super.ctor(self, stateName)
end

function M:OnEnter()
    print('进入Main State')
end

function M:OnLeave()
    print('退出Main State')
end

return M

状态机使用示例:

RunMgr = {}
require "Base.class"
local SM = require "Base.StateMachine"
local MainState = require "Logic.MainState"
local GameState = require "Logic.GameState"
local LauncherState = require "Logic.LauncherState"
local states = {}
function RunMgr.Init()
	log("RunMgr.Init--->>");
	RunMgr.InitData()
	local sm = SM:new('SlotGame')
	sm:InitStates(states)
	sm:ChangeState('MainState')
	sm:ChangeState('LauncherState')
	sm:ChangeState('GameState')
end

function RunMgr.InitData()
	states['MainState'] = MainState:new('MainState')
	states['GameState'] = GameState:new('GameState')
	states['LauncherState'] = LauncherState:new('LauncherState')
end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据提供的引用内容,我们可以了解到Lua没有类似于JavaScript的splice函数。但是,我们可以使用Lua的table库来实现类似于splice函数的功能。下面是一个简单的Lua函数,可以实现类似于JavaScriptsplice函数的功能: ```lua function splice(t, start, deleteCount, ...) local len = #t start = start or 1 if start < 0 then start = len + start + 1 end deleteCount = deleteCount or (len - start + 1) local removed = {} for i = start, start + deleteCount - 1 do table.insert(removed, t[i]) end local args = {...} local addCount = #args local newLen = len - deleteCount + addCount for i = len, start + deleteCount - 1, -1 do t[i + addCount] = t[i] end for i = 1, addCount do t[start + i - 1] = args[i] end for i = len, newLen + 1, -1 do t[i] = nil end return removed end ``` 这个函数接受一个table作为第一个参数,表示要操作的数组;第二个参数表示要删除或插入的起始位置;第三个参数表示要删除的元素个数;后面的参数表示要插入的元素。函数返回一个数组,表示被删除的元素。 下面是一个使用示例: ```lua local t = {1, 2, 3, 4, 5} local removed = splice(t, 2, 2, 6, 7) print(table.concat(t, ", ")) -- 输出:1, 6, 7, 5 print(table.concat(removed, ", ")) -- 输出:2, 3 ``` 这个示例,我们将数组t从第2个位置开始的2个元素删除,并在这个位置插入了两个新元素6和7。最后,我们打印了修改后的数组t和被删除的元素数组removed。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值