PGRData/Resources/Scripts/XManager/XLoadingManager.lua

82 lines
1.8 KiB
Lua
Raw Normal View History

2022-12-26 08:36:01 +00:00
LoadingType = {
Fight = "10101", --战斗
Dormitory = "902", --宿舍
}
XLoadingManagerCreator = function()
local XLoadingManager = {}
--初始化
function XLoadingManager.Init()
end
--根据类型以及权重取出loading的tab数据
function XLoadingManager.GetLoadingTab(type)
if not type then
return
end
local loadingList = XLoadingConfig.GetCfgByType(type)
if not loadingList then
XLog.Error("不存在该类型的LoadingList配置type =", type)
return
end
local count = #loadingList
if count == 0 then
XLog.Error("不存在该类型的LoadingList配置type =", type)
return
end
--只有一个直接返回
if count == 1 then
return loadingList[1]
end
--打乱数组
for i = 1, count do
if i < count then
local temp = loadingList[i]
local next = i + 1
local swapIndex = math.random(next, count)
loadingList[i] = loadingList[swapIndex]
loadingList[swapIndex] = temp
end
end
--根据权重获取
local TotalWeight = 0
--先计算权重总和
for _, v in ipairs(loadingList) do
TotalWeight = TotalWeight + v.Weight
end
local maxValue = 0
local index = 1
--选择权重最高的值
for k, v in ipairs(loadingList) do
local weight = math.random(0, TotalWeight)
weight = weight + v.Weight
if maxValue < weight then
maxValue = weight
index = k
end
end
return loadingList[index]
end
XLoadingManager.Init()
return XLoadingManager
end