游戏中的事件系统(lua实现)
程序员文章站
2024-03-25 22:58:28
...
事件系统管理类
local EventSystem = Class("EventSystem ")
function EventSystem:New()
self.is_deleted = false
self.event_list = {} -- 事件列表
self.need_fire_events = {} -- 需要激发的事件(延后调用方式)
end
function EventSystem:__delete()
self.is_deleted = true
end
--调用已经处于派发队列中的Event
function EventSystem:Update()
if #self.need_fire_events > 0 then
local events = self.need_fire_events
self.need_fire_events = {}
for k, v in pairs(events) do
v.event:Fire(v.arg_list)
end
end
end
function EventSystem:GetEventNum(t)
for k, v in pairs(self.event_list) do
t["global_event : " .. k] = v.bind_num
end
end
function EventSystem:Bind(event_id, event_func)
if event_id == nil then
error("Try to bind to a nil event_id")
return nil
end
if self.is_deleted then
return nil
end
local tmp_event = self.event_list[event_id]
if tmp_event == nil then
tmp_event = Event.New(event_id)
self.event_list[event_id] = tmp_event
end
return tmp_event:Bind(event_func)
end
function EventSystem:UnBind(event_handle)
if event_handle == nil or event_handle.event_id == nil then
error("Try to unbind a nil event_id")
return
end
if self.is_deleted then
return
end
local tmp_event = self.event_list[event_handle.event_id]
if tmp_event ~= nil then
tmp_event:UnBind(event_handle)
end
end
--立即触发
function EventSystem:Fire(event_id, ...)
if event_id == nil then
error("Try to call EventSystem:Fire() with a nil event_id")
return
end
if self.is_deleted then
return
end
local tmp_event = self.event_list[event_id]
if tmp_event ~= nil then
tmp_event:Fire({...})
end
end
--下一帧触发
function EventSystem:FireNextFrame(event_id, ...)
if event_id == nil then
error("Try to call EventSystem:FireNextFrame() with a nil event_id")
return
end
if self.is_deleted then
return
end
local tmp_event = self.event_list[event_id]
if tmp_event ~= nil then
table.insert(self.need_fire_events, {event = tmp_event, arg_list = {...}})
end
end
function EventSystem:GetDebugEventCount(t)
t.event_count = 0
for k, v in pairs(self.event_list) do
t.event_count = t.event_count + v.bind_num
end
end
retutn EventSystem
事件类
local Event = Class("Event")
function Event:New(event_id)
self.event_id = event_id
self.bind_id_count = 0
self.bind_num = 0
self.event_func_list = {}
end
function Event:Fire(arg_table)
for _, func in pairs(self.event_func_list) do
func(unpack(arg_table))
end
end
function Event:UnBind(obj)
if obj.event_id == self.event_id then
self.bind_num = self.bind_num - 1
self.event_func_list[obj.bind_id] = nil
end
end
function Event:Bind(event_func)
self.bind_num = self.bind_num + 1
self.bind_id_count = self.bind_id_count + 1
local obj = {event_id = self.event_id, bind_id = self.bind_id_count}
self.event_func_list[obj.bind_id] = event_func
return obj
end
return Event
上一篇: Unity3d实现翻书效果
推荐阅读
-
游戏中的事件系统(lua实现)
-
利用Laravel事件系统如何实现登录日志的记录详解
-
利用Laravel事件系统如何实现登录日志的记录详解
-
python基于pygame实现响应游戏中事件的方法(附源码)
-
python基于pygame实现响应游戏中事件的方法(附源码)
-
PopupWindow进阶用法——android上实现类似UCweb的自定义menu,完全模拟系统事件
-
PopupWindow进阶用法——android上实现类似UCweb的自定义menu,完全模拟系统事件
-
python基于pygame实现响应游戏中事件的方法(附源码)
-
python基于pygame实现响应游戏中事件的方法(附源码)