Lua情节链设计2

来源:互联网 发布:平克弗洛伊德知乎 编辑:程序博客网 时间:2024/04/30 18:40
 
--[[ file name .    GameList.lua author  .      Clark/陈泽丹 created .      8.30.2011 purpose .      双向队列--]]module("GameList", package.seeall)--双向队列function list_newList()    local m_first = 1    local m_last = 0    local m_list = {}    local m_listManager = {}    function m_listManager.pushFront(_tempObj)        m_first = m_first - 1        m_list[m_first] = _tempObj    end    function m_listManager.pushBack(_tempObj)        m_last = m_last + 1        m_list[m_last] = _tempObj    end    function m_listManager.temp_getFront()        if m_listManager.bool_isEmpty() then            return nil        else            local val = m_list[m_first]            return val        end    end    function m_listManager.temp_getBack()        if m_listManager.bool_isEmpty() then            return nil        else            local val = m_list[m_last]            return val        end    end    function m_listManager.popFront()        m_list[m_first] = nil        m_first = m_first + 1    end    function m_listManager.popBack()        m_list[m_last] = nil        m_last = m_last - 1    end    function m_listManager.clear()        while false == m_listManager.bool_isEmpty() do        m_listManager.popFront()    end    end    function m_listManager.bool_isEmpty()        if m_first > m_last then            m_first = 1            m_last = 0            return true        else            return false        end    end    function m_listManager.d_getSize()        if  m_listManager.bool_isEmpty() then            return 0        else            return m_last - m_first + 1        end    end    return m_listManagerend
--[[ file name :    GameEvents.lua author  :      Clark/陈泽丹 created :      8:30:2011 purpose :      故事情节线--]]module("GameEvents", package.seeall)pakList = require "GameList"pakGameCallBack = require "GameCallBack"g_fun = nil--死亡回调函数function onDieCallBack()    g_fun(1)end--事件流function events_newEvents()local TYPE = {FUN_TYPE = 0, TGR_TYPE = 1}local m_funs = pakList.list_newList()          --隐藏数据local m_events = {}                               --公开数据--添加事件function m_events.pushFun(_fun)local tFun = {fun = _fun, fun_type = TYPE.FUN_TYPE}m_funs.pushBack(tFun)endfunction m_events.pushTgr(_fun)function setTgr()print("setTgr ")g_fun = onTgrendfunction onTgr(_callbackPar)print("onTgr " .. _callbackPar)g_fun = nil_fun(_callbackPar)m_events.doEvents()endlocal tFun = {fun = setTgr, fun_type = TYPE.TGR_TYPE}m_funs.pushBack(tFun)end--清空事件function m_events.clear()print("m_events.clear")m_funs.clear()end--开始剧情function m_events.doEvents()while false == m_events.bool_isEmpty() dolocal tFun = m_funs.temp_getFront()m_funs.popFront()tFun.fun()tFun.fun = nilif TYPE.FUN_TYPE ~= tFun.fun_type thentFun = nilreturnendendif m_events.bool_isEmpty() thenm_events.clear()endend--是否还有事件function m_events.bool_isEmpty()return m_funs.bool_isEmpty()endreturn m_events                   --返回公开数据end

 

 

--[[ file name :    GameMain.lua author  :      Clark/陈泽丹 created :      8:30:2011 purpose :      客户端程序--]]module("GameMain", package.seeall)pakGameEvents = require "GameEvents"------------------------ 客户端 -----------------------------function map_newMap()    local m_map = {}local m_ETs = pakGameEvents.events_newEvents()function m_map.Fun1()print("1")endfunction m_map.Fun2()print("2")endfunction m_map.Fun3()print("3")end    --初始化    function m_map.initScene()        print("---------- initScene ----------")m_ETs.pushFun(m_map.Fun1)m_ETs.pushTgr(m_map.Fun2)m_ETs.pushFun(m_map.Fun3)m_ETs.pushTgr(m_map.Fun2)m_ETs.doEvents()pakGameEvents.onDieCallBack()pakGameEvents.onDieCallBack()    end    --结束    function m_map.uninitScene()        print("---------- uninitScene ----------")    end    return m_mapendfunction main()    m_map  = map_newMap(007)    m_map.initScene()endmain()-------------------------------------------------------------


结果:

 

>lua -e "io.stdout:setvbuf 'no'" "GameMain.lua"
---------- initScene ----------
1
setTgr
onTgr 1
2
3
setTgr
onTgr 1
2
m_events.clear
>Exit code: 0


原创粉丝点击