【吃鸡】客户端视野管理,管理掉落物

<1>原因:

在吃鸡场景中,有很多掉落物(每种掉落物模型各不相同),服务器在进入场景的时候推送了所有掉落物,后面服务器只做更新掉落和删除掉落操作

客户端需要对这些掉落物做视野管理,模型太多手机内存开销太大,也很可能因为内存过高崩溃

<2>思路:

把地图划分成均匀网格,每个掉落物存在网格之中,玩家每次更新自身格子索引,把九宫格之内的物体加载显示,把上次九宫格之内的物体做回收,公共部分不用动

<3>代码:

代码都是在Lua这边实现

--掉落物管理相关
local mapX = 0 --地图宽
local mapY = 0 --地图长
local mapCenterX = 107 --地图中心X
local mapCenterY = 176 --地图中心Y
local rowSize = 24 --格子行高 rowSize
local colSize = 24 --格子列高 colSize
local maxRow = math.ceil(mapX/rowSize) --多少行
local maxCol = math.ceil(mapY/colSize) --多少列
local allCount = maxRow*maxCol ---格子总数
local startX =  (-mapX/2)--+mapCenterX -- (-(mapX/2-mapCenterX))/2--格子起始坐标X
local startY = (mapY/2)--+mapCenterY --(mapY-mapCenterY)/2--格子起始坐标Y

  

关键方法提供

1.根据玩家当前格子索引获取九宫格索引

--获取格子周围所有格子
function BattleRoyaleDropMgr:getRoundIndex(index)
	    local lst = {}
		--top 没有上 r1r2r3 --bottom没有下 r6r7r8 --left没有左r1r4r6  --right没有右r3r5r8
		local isTop = index<=maxRow
		local isBottom = index > (maxCol-1)*maxRow
		local isLeft = index%maxRow == 1
		local isRight = index%maxRow == 0
		local filter = {}
		local r1 = index - maxRow - 1
		local r2 = index - maxRow
		local r3 = index - maxRow + 1
		local r4 = index - 1
		local r5 = index + 1
		local r6 = index + maxRow - 1
		local r7 = index + maxRow
		local r8 = index + maxRow + 1
		if isTop then
		    filter[r1] = true
			filter[r2] = true
			filter[r3] = true
	    end
		if isBottom then
		    filter[r6] = true
			filter[r7] = true
			filter[r8] = true
	    end
		if isLeft then
		    filter[r1] = true
			filter[r4] = true
			filter[r6] = true
	    end
		if isRight then
		    filter[r3] = true
			filter[r5] = true
			filter[r8] = true
	    end	   
		local result = {r1,r2,r3,r4,r5,r6,r7,r8}
		for i =1,#result do
		    if filter[result[i]] == nil then
		        lst[result[i]] = true 
			end
		end
		lst[index] = true 
		return lst
end 

  

 

posted on 2019-05-07 15:00  tianjiuwan  阅读(197)  评论(0编辑  收藏  举报