将1d数组拆分为块
我试图不把我的整个瓷砖基地图加载到内存中以保存RAM客户端。 该地图将是巨大的,并且已经占用1GB客户端(多层地图)。
我对Game Dev SO有了一些了解。 我试图将我的游戏地图的区域/块加载到内存中(例如300x300),然后当玩家移动100步时移动阵列并根据方向加载100个新的瓦片。 我试图在这个缩放版本上工作,现在有一个通用的问题。
当玩家X / Y坐标位于地图的边界上时(这会导致地图块在地图外),我需要帮助,
这是我到目前为止所提出的(注意:玩家位于大块和大块的中心,总是奇数大小)......它有以下问题(当角色在地图边缘时):
将字符X / Y更改为0,0,左下角(0,2)坐标错误地为7
0,0,0
0,1,1
7,1,8
将字符X / Y更改为8,8,块的右上角(2,0)坐标错误地为6
1,1,6
1,1,0
0,0,0
这是SSCCE:
public class MapChunkLoad {
    public static void main(String[] args) {
        short[] groundLayer;
        int mapWidth = 9;
        int mapHeight = 9;
        int chunkWidth = mapWidth / 3; //3
        int chunkHeight = mapHeight / 3; //3
        int characterX = 8;
        int characterY = 8;
        String map = "1, 1, 1, 1, 1, 1, 1, 1, 7, " +
                     "1, 8, 8, 1, 1, 1, 1, 1, 1, " +
                     "1, 8, 9, 9, 1, 1, 1, 1, 1, " +
                     "1, 1, 9, 9, 1, 1, 1, 1, 1, " +
                     "1, 1, 1, 1, 1, 1, 1, 1, 1, " +
                     "1, 1, 1, 1, 1, 1, 1, 1, 1, " +
                     "1, 1, 1, 1, 1, 1, 1, 1, 1, " +
                     "1, 1, 1, 1, 1, 1, 1, 1, 1, " +
                     "6, 1, 1, 1, 1, 1, 1, 1, 1";
        String[] strArr = map.split(", ");
        groundLayer = new short[chunkWidth * chunkHeight];
        //load chunk into groundLayer
        int arrayIndex = 0;
        int count = (characterX - (chunkWidth/2)) + ((characterY - (chunkHeight/2)) * mapWidth); //top left tile within chunk
        for (int y = 0; y < chunkHeight; y++){
            for (int x = 0; x < chunkWidth; x++){
                if (count > -1 && count < strArr.length){
                    groundLayer[arrayIndex] = Short.parseShort(strArr[count]);
                    System.out.println("arrayIndex[" + arrayIndex + "] = " + strArr[count]);
                } else {
                    groundLayer[arrayIndex] = 0;
                    System.out.println("arrayIndex[" + arrayIndex + "] = " + 0);
                }
                arrayIndex++;
                count++;
            }
            count += (mapWidth - chunkWidth);
        }
        System.out.println("");
        //print map grid
        int printcount = 0;
        for (int y = 0; y < chunkHeight; y++){
            for (int x = 0; x < chunkWidth; x++){
                if (x == chunkWidth - 1){
                    System.out.println(groundLayer[printcount]);
                } else {
                    System.out.print(groundLayer[printcount] + ", ");
                }
                printcount++;
            }
        }
    }
}
非常感谢您的帮助。
  我不确定这是否是您要查找的内容,但通常情况下,您会检查填充groundLayer嵌套for循环中的groundLayer ,而不是使用count变量进行检查。  这种方式将更加强大。  像这样的东西: 
for(int y = 0;y < chunkHeight;y++) {
    for(int x = 0;x < chunkWidth;x++) {
        //Get the absolute position of the cell.
        int cellX = characterX + x - chunkWidth / 2; //Please check if this is correctly lined out.
        int cellY = characterY + y - chunkHeight / 2;
        if(cellX >= 0 && cellX < mapWidth && cellY >= 0 && cellY < mapHeight) { //Within bounds.
            groundLayer[arrayIndex] = Short.parseShort(strArr[cellX + (cellY * mapWidth)]);
        } else { //Out of bounds, put down a placeholder.
            groundLayer[arrayIndex] = 0;
        }
        arrayIndex++;
    }
}
让我知道这是你在找什么,以及它是否有效!
所以我认为你检查count是否超出范围的逻辑是错误的。
我认为它需要更复杂来说明你的数组代表了一个2D数字。 我怀疑对于大于3x3的块,你会得到更多更难以描述的错误。 考虑这张地图,其中每个方格的值是其索引。
0 1 2
3 4 5
6 7 8
当在右上角(2)时,你的地图应该看起来像这样
0 0 0
1 2 0
4 5 0
但是这会失败,因为在计算xValue * width + yValue时count在某些情况下会是有效的索引,但是您希望它是无效的(映射为0)。 相反,您需要跟踪count的X和Y两个分量,并在其中任何一个超出界限时使地图显示为零。
int countX = characterX - (chunkWidth/2);
int countY = characterY - (chunkHeight/2);
int index = countX + (countY*mapWidth)
然后再。 而不是检查:
if (count > -1 && count < strArr.length)
检查:
if( countX + x >= mapWidth || countY + y >= mapHeight)
编辑:
你可以想象这也会改变你的计数方式。 你也需要一种方法来打破你的循环。 就像是
if(x == chunkWidth && y == chunkWidth) break; 
我会更具体,但我无法加载您的原始帖子作为参考。
我认为这可以修复一切。 如果您有任何问题,请留下评论。 祝你好运!
链接地址: http://www.djcxy.com/p/24099.html