108 lines
3.6 KiB
Lua
Raw Permalink Normal View History

2025-01-04 23:00:19 +08:00
require("Script.Global.EventManager.EventConfig")
EventManager = EventManager or {}
--- 判断 bEnableActionTick
EventManager.TickArray = {}
EventManager.TickHandle = nil
EventManager.LastTickTime = 0.
function EventManager.InitEventManager()
EventManager.BindingEvent()
EventManager.EnableTickAction()
end
function EventManager.DestroyEventManager()
EventManager.DisableTickAction()
end
--------------------------------------- Tick ---------------------------------------
function EventManager.EnableTickAction()
if EventManager.TickHandle == nil then
EventManager.TickHandle = UGCEventSystem.SetTimerLoop(UGCGameSystem.GameState, EventManager.Tick, 1. / EventConfig.TickFrequency)
end
end
function EventManager.DisableTickAction()
if EventManager.TickHandle then
UGCEventSystem.StopTimer(EventManager.TickHandle)
end
end
--- 执行所有Action的Tick
function EventManager.Tick()
local NowTime = UGCSystemLibrary.GetGameTime()
--UGCLogSystem.Log("[EventManager_Tick] TickArray Count:%d", #EventManager.TickArray)
if #EventManager.TickArray > 0 then
local DeltaSeconds = NowTime - EventManager.LastTickTime
for i = #EventManager.TickArray, 1, -1 do
--UGCLogSystem.Log("[EventManager_Tick] i:%d", i)
local TempAction = EventManager.TickArray[i]
if TempAction.bEnableActionTick then
TempAction:Update(DeltaSeconds)
else
UGCLogSystem.Log("[EventManager_Tick] Remove TickArray Count:%d", #EventManager.TickArray)
table.remove(EventManager.TickArray, i)
end
end
end
EventManager.LastTickTime = NowTime
end
function EventManager.ActionJoinToTick(TargetAction)
if TargetAction and TargetAction.Update then
EventManager.TickArray[#EventManager.TickArray + 1] = TargetAction
UGCLogSystem.Log("[EventManager_ActionJoinToTick] TickArray Count:%d", #EventManager.TickArray)
end
end
------------------------------------- Tick End -------------------------------------
function EventManager.BindingEvent()
for EventType, AttachInfo in pairs(EventConfig.EventAttach) do
if EventConfig.CheckEventAttach(EventType) then
UGCEventSystem.AddListener(EventType, function(...)
EventManager.ExeAttachActions(EventType, ...)
end)
else
UGCLogSystem.LogError("[EventManager_BindingEvent] CheckEventAttach(%s) is Error", tostring(EventType))
end
end
end
function EventManager.ExeAttachActions(EventType, ...)
local Inputs = table.pack(...)
-- 创建传递的Event参数对照表
local TempEventParams = {}
for i = 1, #EventConfig.EventParam[EventType] do
TempEventParams[EventConfig.EventParam[EventType][i]] = Inputs[i]
end
for ActionType, ParamMap in pairs(EventConfig.EventAttach[EventType]) do
local RequireFile = require(EventConfig.ActionInfo[ActionType].ActionPath)
local TempAction = setmetatable({}, { __index = RequireFile, __metatable = RequireFile })
-- 设置默认参数
for ParamName, ParamDefaultValue in pairs(EventConfig.ActionInfo[ActionType].ActionParam) do
TempAction[ParamName] = ParamDefaultValue
end
-- 设置传递参数
for EventParamName, ActionParamName in pairs(ParamMap) do
TempAction[ActionParamName] = TempEventParams[EventParamName]
end
-- 执行Action
TempAction:Execute(table.unpack(TempEventParams))
-- 判断是否需要Tick
if TempAction.bEnableActionTick then
EventManager.ActionJoinToTick(TempAction)
end
end
end