-- 这个脚本是记录主将出生点信息 local Base = require("Battle/Logic/Level/LevelItemFactory/LevelItem") local LevelItem_RandomPool = class("LevelItem_RandomPool", Base) -- 构造函数 ,使用的时候用new ,如果没有特殊的变量设置,不需要这个函数 function LevelItem_RandomPool:initialize(data) Base.initialize(self,data) self._curRandomList = {} -- 当前池子中的随机数据,随机数字都从这里取 { {id,Weight}, {id,Weight},{id,Weight},{id,Weight}, } self._curTotalWeight = FP.newV1(0) --随机的总权重 for i, v in ipairs(data.WeightList) do self._curTotalWeight:Add(v.Weight) table.insert(self._curRandomList, v) end end --region --***************生命周期***************-- --region 生命周期 --region --***************事件相关***************-- function LevelItem_RandomPool:DoAction(actionType, ...) Base.DoAction(self, actionType, ...) if actionType == LuaConst.LevelActionType.RandomPool_Reset then self:ResetPool() end end -- 重置随机池子 function LevelItem_RandomPool:ResetPool() table.clear(self._curRandomList) self._curTotalWeight:Reset() --随机的总权重 for i, v in ipairs(self.ItemData.WeightList) do self._curTotalWeight:Add(v.Weight) table.insert(self._curRandomList, v) end end --endregion 事件相关 --返回一个随机值 function LevelItem_RandomPool:GetRandomValue() local result = nil local index = 0 local weight = 0 local random = LogicRandom.GetRangeValue(nil, FP.Const.Zero, self._curTotalWeight ,55) for i,v in ipairs(self._curRandomList) do random:Sub(v.Weight) if FP.LessEquals(random, 0) then result = v.Value index = i weight = v.Weight break; end end if self.ItemData.NeedTakeOut then self._curTotalWeight:Sub(weight) table.remove(self._curRandomList, index) if #self._curRandomList == 0 then -- 策划需求:默认的逻辑,当随机池空了后,需要重置 self:ResetPool() end end return result end function LevelItem_RandomPool:GetFirstValue() local info = self._curRandomList[1] if info then return info.Value end return nil end return LevelItem_RandomPool