--LogicRandom 只是用于逻辑中使用,不能用于表现 local LogicRandom = {} local RandomCount = 1000 local RandomCache = LuaExt.CreateTable(RandomCount, 0) local _randomPool = LuaExt.CreateTable(RandomCount, 0) local _randomIndex = 1 local FP = FP --返回定点数(前闭后闭) function LogicRandom.GetRandom01(entity ,markid) local result = _randomPool[_randomIndex] if result then -- CompareSyncLogManager.TryColectLog(LuaConst.CheckBattleLogType.RandomNum , entity , _randomIndex , markid , result[1]) else logging.error(LTAG.ERROR, "【gws】 :发现了随机数是空的情况 ,randomindex =", _randomIndex) end _randomIndex = _randomIndex + 1 if _randomIndex > RandomCount then _randomIndex = 1 end return result end --返回定点数 function LogicRandom.GetRangeValue(entity, minValue, maxValue, markid) local random01 = LogicRandom.GetRandom01(entity ,markid) local result = FP.newV1(0) result:Reset(maxValue):Sub(minValue):Mul(random01):Add(minValue) return result end --返回number 整数 给索引类用(前闭后闭) function LogicRandom.GetIntegerValue(entity, minValue, maxValue, markid) local random01 = LogicRandom.GetRandom01(entity ,markid) local result = FP.newV1(0) result:Reset(maxValue):Sub(minValue):Add(0.999):Mul(random01):Add(minValue) result = FP.Math.Floor(result) return FP.tonumberV1(result) end --设置随机数 function LogicRandom.SetRandomPool(seed) LuaExt.RandomBySeed(seed, RandomCount, RandomCache) for i = 1, RandomCount do if _randomPool[i] then _randomPool[i]:Reset(RandomCache[i]) else _randomPool[i] = FP.newV1(RandomCache[i]) end end _randomIndex = 998 end --在圆心为(0,0)的圆中随机1个坐标,FP.V2 function LogicRandom.GetRandomPointInCircle(entity,radius ,markid ,result) -- local rho = LogicRandom.GetRangeValue(entity, 0, radius ,markid) --极坐标ρ -- local theta = LogicRandom.GetRangeValue(entity, FP.Const.Zero, FP.Const.PI2 ,markid) --极坐标θ -- local sin = FP.Math.Sin(theta) -- local cos = FP.Math.Cos(theta) -- result = result or FP.newV2(0, 0) -- result.x:Reset(rho):Mul(cos) -- result.z:Reset(rho):Mul(sin) -- return result return LogicRandom.GetRandomPointInRing(entity, 0, radius, markid, result) end --在圆心为(0,0)、指定内径和外径的圆环中随机1个坐标,FP.V2 function LogicRandom.GetRandomPointInRing(entity, innerRadius, outterRadius, markid ,result) local rho = LogicRandom.GetRangeValue(entity, innerRadius, outterRadius ,markid) --极坐标ρ local theta = LogicRandom.GetRangeValue(entity, FP.Const.Zero, FP.Const.PI2 ,markid) --极坐标θ local sin = FP.Math.Sin(theta) local cos = FP.Math.Cos(theta) result = result or FP.newV2(0, 0) result.x:Reset(rho):Mul(cos) result.z:Reset(rho):Mul(sin) return result end --随机一个点,x取值范围[-width/2, width/2], y取值范围[-height/2, height/2]。 --注意关卡元素的Length指的是x方向上的长度,Width指的是z方向上的长度,分别对应这里参数的width和height function LogicRandom.GetRandomPointInRect(entity, width, height, markid, result) local x = LogicRandom.GetRangeValue(entity, -width / 2, width / 2 , markid) local z = LogicRandom.GetRangeValue(entity, -height / 2, height / 2 , markid) result = result or FP.newV2(0, 0) result.x:Reset(x) result.z:Reset(z) return result end function LogicRandom.GetRandomPointInLevelItem(entity, itemData, markid, result) local pos = result or FP.newV2(0, 0) --itemData.Type -- 0:点 1:圆 2:矩形 if itemData.Type == LuaConst.EBornItemAreaType.POINT then pos:Reset(itemData.ItemPos) elseif itemData.Type == LuaConst.EBornItemAreaType.CIRCLE then LogicRandom.GetRandomPointInCircle(entity, itemData.Radius, markid, pos):Add(itemData.ItemPos) elseif itemData.Type == LuaConst.EBornItemAreaType.RECT then --关卡元素里,Length是x轴的长度,Width是z轴的长度 LogicRandom.GetRandomPointInRect(entity, itemData.Length, itemData.Width, markid, pos):Add(itemData.ItemPos) else if itemData.LevelItemType ~= LuaConst.LevelItemType.TargetPoint then logging.error(LTAG.ERROR, "HYW : 非法的出生点关卡元素形状类型 : ", itemData.type, " 元素ID : ", itemData.LevelItemID) end pos:Reset(itemData.ItemPos) end return pos end return LogicRandom