Lua 리 디자인 줄거리 체인

--[[
 file name .    GameList.lua
 author  .      Clark/ 
 created .      8.13.2011
 purpose .       
--]]


module("GameList", package.seeall)

-- 
function list_newList()
    local first = 1
    local last = 0
    local list = {}
	local listManager = {}
    function listManager.pushFront(_tempObj)
        first = first - 1
        list[first] = _tempObj
    end
    function listManager.pushBack(_tempObj)
        last = last + 1
        list[last] = _tempObj
    end
    function listManager.temp_getFront()
        if listManager.bool_isEmpty() then
            return nil
        else
            local val = list[first]
            return val
        end
    end
    function listManager.temp_getBack()
        if listManager.bool_isEmpty() then
            return nil
        else
            local val = list[last]
            return val
        end
    end
    function listManager.popFront()
        list[first] = nil
        first = first + 1
    end
    function listManager.popBack()
        list[last] = nil
        last = last - 1
    end
    function listManager.clear()
        while false == listManager.bool_isEmpty() do
        listManager.popFront()
    end
    end
    function listManager.bool_isEmpty()
        if first > last then
			first = 1
			last = 0
            return true
        else
            return false
        end
    end
    function listManager.d_getSize()
        if  listManager.bool_isEmpty() then
            return 0
        else
            return last - first + 1
        end
    end
    return listManager
end

 -------------------------------------------------------------------------------------
 
--[[
 file name :    GameEvents.lua
 author  :      Clark/ 
 created :      8:13:2011
 purpose :       
--]]

module("GameEvents", package.seeall)
pakList = require "GameList"

function manager_newEventManager()
	-- 
	function events_newEvents(_ID)
		local ID = _ID or os.time()
		local cmdpars = pakList.list_newList()          -- 
		local events = {}                               -- 
		function events.id_getID()
			return ID
		end
		-- 
		function events.push(_trigger, _tTgrPar, _fun, _tFunPar)
			local t = { trigger = _trigger, tTgrPar = _tTgrPar or {}, fun = _fun, tFunPar = _tFunPar or {}}
			cmdpars.pushBack(t)
		end
		-- 
		function events.clear()
			cmdpars.clear()
		end
		-- 
		function events.setEvent()
			local tVal = cmdpars.temp_getFront()
			if nil ~= tVal then
				if nil ~= tVal.trigger then
					tVal.trigger(events.id_getID(), tVal.tTgrPar)
				end
			end
		end
		-- 
		function events.onEvent(tEventPar)
			local tEventPar = tEventPar or {}
			local tVal = cmdpars.temp_getFront()
			if nil ~= tVal then
				cmdpars.popFront()
				if nil ~= tVal.fun then
					tVal.fun(events.id_getID(), tVal.tFunPar, tEventPar)
				end
			end
		end
		-- 
		function events.bool_isEmpty()
			local val = cmdpars.bool_isEmpty()
			return val
		end
		return events                   -- 
	end

	-- 
	local et = {}
	local manager = {}
	function manager.events_newEvents(_ID)
		local event = events_newEvents(_ID)
		if nil ~= event then
			et[event.id_getID()] = event
		end
		return event
	end
	function manager.events_getEvents(_ID)
		return et[_ID]
	end
	function manager.deleteEvents(_ID)
		et[_ID] = nil
	end
	return manager
end

-------------------------------------------------------------------------------------
 
--[[
 file name :    GameCallBack.lua
 author  :      Clark/ 
 created :      8:13:2011
 purpose :       
--]]


module("GameCallBack", package.seeall)
pakEvents = require "GameEvents"



EVENT_MANAGER = pakEvents.manager_newEventManager()


-- 
function onGotoCallBack(_tEventPar)
	local et = EVENT_MANAGER.events_getEvents(_tEventPar)
	if nil ~= et then
		et.onEvent()
		et.setEvent()
	end
end

-- 
function onDieCallBack(_tEventPar)
	local et = EVENT_MANAGER.events_getEvents(_tEventPar)
	if nil ~= et then
		et.onEvent()
		et.setEvent()
	end
end

 
 -------------------------------------------------------------------------------------
 
 
--[[
 file name :    GameMain.lua
 author  :      Clark/ 
 created :      8:13:2011
 purpose :       
--]]


module("GameMain", package.seeall)
pakCallBack = require "GameCallBack"



------------------------   -----------------------------
function map_newMap(_ID)
	local ID = _ID
	local map = {}

	-- ID
	function map.id_getID()
		return ID
	end

	-- 
	function map.event_KillMonster()
		local tgr = false
		local et = {}
		function et.setEvent(eventID, tTgrPar)
			print("on EVENTS:", eventID, "| Map: ", map.id_getID(), " on ", tTgrPar[1])
			tgr = true
			pakCallBack.onDieCallBack(eventID)
		end
		function et.onEvent(eventID, tFunPar, tEventPar)
			print("do EVENTS:", eventID, "| Map: ", map.id_getID(), " -> ", tFunPar[1])
			tgr = false
		end
		return et
	end

	-- 
	function map.event_Exit()
		local tgr = false
		local et = {}
		function et.setEvent(eventID, tTgrPar)
			print("on EVENTS:", eventID, "| Map: ", map.id_getID(), " on ", tTgrPar[1])
			tgr = true
			pakCallBack.onGotoCallBack(eventID)
		end
		function et.onEvent(eventID, tFunPar, tEventPar)
			print("do EVENTS:", eventID, "| Map: ", map.id_getID(), " -> ", tFunPar[1])
			tgr = false
			map.uninitScene()
		end
		return et
	end



	local EVENTS_1_ID = os.time() -- os.time() , 
	local EVENTS_2_ID = EVENTS_1_ID + 1


	-- 
	function map.initScene()
		print("---------- initScene ----------")
		local et = nil

		-- 1
		local ets1 = pakCallBack.EVENT_MANAGER.events_newEvents(EVENTS_1_ID)
		et = map.event_KillMonster()
		ets1.push(et.setEvent, {"Wolf die"}, et.onEvent, {"give A"})

		-- 2
		local ets2 = pakCallBack.EVENT_MANAGER.events_newEvents(EVENTS_2_ID)
		ets2.push(et.setEvent, {"tiger die"}, et.onEvent, {"give B"})
		et = map.event_Exit()
		ets2.push(et.setEvent, {"exiting"}, et.onEvent, {"exited"})

		-- 
		ets1.setEvent()
		ets2.setEvent()
	end

	-- 
	function map.uninitScene()
		print("---------- uninitScene ----------")
		pakCallBack.EVENT_MANAGER.deleteEvents(EVENTS_1_ID)
	end


    return map
end


function main()
	map  = map_newMap(007)
	map.initScene()
end

main()
-------------------------------------------------------------

result:
>lua -e "io.stdout:setvbuf 'no'""Main.lua" ---------- initScene ---------- on EVENTS: 1313262659 | Map:  7  on  Wolf die do EVENTS: 1313262659 | Map:  7  ->  give A on EVENTS: 1313262660 | Map:  7  on  tiger die do EVENTS: 1313262660 | Map:  7  ->  give B on EVENTS: 1313262660 | Map:  7  on  exiting do EVENTS: 1313262660 | Map:  7  ->  exited ---------- uninitScene ---------->Exit code: 0

좋은 웹페이지 즐겨찾기