zoukankan      html  css  js  c++  java
  • 【吃鸡】客户端视野管理,管理掉落物

    <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 
    

      

  • 相关阅读:
    Compile、Make和Build的区别
    IntelliJ IDEA 学习(三):IntelliJ IDEA 快捷键、配置优化
    【HTML5 】手机重力与方向感应的应用——摇一摇效果
    IntelliJ IDEA 学习(二):Intellij IDEA 创建Web项目并在Tomcat中部署运行IDEA
    html学习一(html简史及doctype)
    IntelliJ IDEA 学习(一):IntelliJ IDEA 破解方法(已验证)
    连接oracle时报错:ORA-28001: the password has expired
    IDEA使用(1)intellIJ idea 配置 svn
    node.js的npm详解
    【朴灵评注】JavaScript 运行机制详解:再谈Event Loop
  • 原文地址:https://www.cnblogs.com/cocotang/p/10825511.html
Copyright © 2011-2022 走看看