UGCProjects/GZJ/Script/Manager/CommandQueneManager.lua
2025-01-08 22:46:12 +08:00

111 lines
4.3 KiB
Lua

---@class CommandQueneManager
CommandQueneManager = CommandQueneManager or {
bGameStateReady = false;
bPlayerCtrlReady = false;
bPlayerPawnReady = false;
bPlayerStateReady = false;
bHasInit = false;
InitCommandList = {};
CommandList = {};
};
require('Script.Global.GlobalFunctions')
local TableHelper = require('Script.Global.TableHelper')
function __FILE__() return debug.getinfo(2, 'S').source end
function __LINE__() return debug.getinfo(2, 'l').currentline end
function __FUNC__() return debug.getinfo(2, 'n').name end
--- 添加初始化命令
---@param InObject any
---@param InFunc function @函数变量
function CommandQueneManager:AddInitCommand(InObject, InFunc)
if UE.IsValid(InObject) == false then
return
end
UE.Log("[CommandQueneManager] *** 新增初始化命令 Object[%s] InFunc[%s]", TableHelper.GetName(InObject), tostring(InFunc))
table.insert(CommandQueneManager.InitCommandList, {Object=InObject, Func=InFunc})
end
function CommandQueneManager:RemoveInitCommand(InObject)
local RemovedFuncNum = 0
for index, FuncData in pairs(CommandQueneManager.InitCommandList) do
if FuncData.Object == InObject then
CommandQueneManager.InitCommandList[index] = nil
RemovedFuncNum = RemovedFuncNum + 1
end
end
UE.Log("[CommandQueneManager] *** 删除初始化命令 Object[%s] RemovedNum=%d", TableHelper.GetName(InObject), RemovedFuncNum)
end
--- 添加常规执行命令
---@param InObject any
---@param InFunc function @函数变量
function CommandQueneManager:AddCommand(InObject, InFunc)
UE.Log("[CommandQueneManager] *** 新增命令 Object[%s] InFunc[%s]", TableHelper.GetName(InObject), tostring(InFunc))
table.insert(CommandQueneManager.CommandList, {Object=InObject, Func=InFunc})
end
function CommandQueneManager:Update()
--- 没有初始化前先调用初始化命令
if not CommandQueneManager.bHasInit
and CommandQueneManager.bGameStateReady
and CommandQueneManager.bPlayerCtrlReady
and CommandQueneManager.bPlayerPawnReady
and CommandQueneManager.bPlayerStateReady then
CommandQueneManager.bHasInit = true
local PendingKillObject = {}
UE.Log("[CommandQueneManager] *** >>> =====开始处理初始化命令===== Num = %d", table.getCount(CommandQueneManager.InitCommandList))
local CommandIndex = 1
for _, FuncData in pairs(CommandQueneManager.InitCommandList) do
if FuncData.Object ~= nil then
if UE.IsValid(FuncData.Object) then
FuncData.Func(FuncData.Object)
UE.Log("[CommandQueneManager] *** >>> Index = %d, objName = %s, Func = %s", CommandIndex, TableHelper.GetName(FuncData.Object), tostring(FuncData.Func))
else
UE.Log("[CommandQueneManager] *** >>> Index = %d, objName = %s is invalid, skipped!", CommandIndex, TableHelper.GetName(FuncData.Object))
table.insert(PendingKillObject, FuncData.Object)
end
else
FuncData.Func()
UE.Log("[CommandQueneManager] *** >>> Index = %d, Func = %s", CommandIndex, tostring(FuncData.Func))
end
CommandIndex = CommandIndex + 1
end
UE.Log("[CommandQueneManager] *** >>> =====处理初始化命令完毕=====")
for _, obj in ipairs(PendingKillObject) do
for index, FuncData in pairs(CommandQueneManager.InitCommandList) do
if obj == FuncData.Object then
CommandQueneManager.InitCommandList[index] = nil
end
end
end
end
if CommandQueneManager.bHasInit then
if #CommandQueneManager.CommandList <= 0 then
return
end
UE.Log("[CommandQueneManager] *** >>> 处理命令 %d", #CommandQueneManager.CommandList)
for _, FuncData in pairs(CommandQueneManager.CommandList) do
if FuncData.Object ~= nil then
if UE.IsValid(FuncData.Object) then
FuncData.Func(FuncData.Object)
end
else
FuncData.Func()
end
end
-- 调用完就清除
CommandQueneManager.CommandList = {}
end
end
return CommandQueneManager;