Java 将1d数组拆分为块

Java 将1d数组拆分为块,java,arrays,Java,Arrays,我试图不将整个基于分片的映射加载到内存中以保存RAM客户端。地图将是巨大的,并且已经需要1GB的客户端(多层地图) 我对游戏开发有了一些看法。我正在尝试将游戏地图的区域/区块加载到内存中(即300x300),然后当玩家移动100步时,移动阵列并根据方向加载100个新分幅。我已经尝试过这个的缩放版本,现在有一个通用的问题 当playerX/Y坐标位于地图的周界(这会导致区块位于地图之外)时,我需要帮助。 以下是我到目前为止的想法(注意:玩家在区块的中心&区块大小总是奇数)。。。它有以下问题(当角

我试图不将整个基于分片的映射加载到内存中以保存RAM客户端。地图将是巨大的,并且已经需要1GB的客户端(多层地图)

我对游戏开发有了一些看法。我正在尝试将游戏地图的区域/区块加载到内存中(即300x300),然后当玩家移动100步时,移动阵列并根据方向加载100个新分幅。我已经尝试过这个的缩放版本,现在有一个通用的问题


当playerX/Y坐标位于地图的周界(这会导致区块位于地图之外)时,我需要帮助。

以下是我到目前为止的想法(注意:玩家在区块的中心&区块大小总是奇数)。。。它有以下问题(当角色位于地图边缘时):

  • 将字符X/Y更改为0,0,左下角(0,2)坐标将不正确地为7

    0,0,0

    0,1,1

    7,1,8

  • 将characterX/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++;
            }
        }

    }
}
公共类MapChunkLoad{
公共静态void main(字符串[]args){
短[]底层;
int mapWidth=9;
int MAPHEIGH=9;
int chunkWidth=mapWidth/3;//3
int chunkHeight=mapHeight/3;//3
int字符x=8;
int字符y=8;
String map=“1,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";
字符串[]strArr=map.split(“,”);
groundLayer=新短[块宽*块高];
//将块装入底层
int-arrayIndex=0;
int count=(characterX-(chunkWidth/2))+((characterY-(chunkHeight/2))*mapWidth);//块内左上角的平铺
对于(int y=0;y-1&&count

非常感谢您的帮助。

我不确定这是否是您正在寻找的,但通常您会检查填充
底层的嵌套for循环中的边界,而不是使用计数变量进行检查。这样做会更加稳健。大概是这样的:

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++;
    }
}
for(int y=0;y=0&&cellX=0&&cellY

让我知道这是否是你一直在寻找的,它是否有效

所以我认为你检查计数是否超出界限的逻辑是错误的

我认为需要更复杂的解释,你的数组代表一个二维图形。我怀疑对于大于3x3的块,你会得到更多的错误,这些错误很难描述。考虑这个图,每个方块的值是它的索引。< /P>
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(x == chunkWidth && y == chunkWidth) break; 
我会说得更具体一些,但我在加载你的原始帖子作为参考时遇到了麻烦


我认为这可以解决一切问题。如果您有任何问题,请留下评论。祝你好运

你也许应该省去人们搜索代码的痛苦,告诉我们你已经完成了什么,还需要帮助。对不起,我想我更清楚了。。。将编辑。当playerX/Y坐标在地图的周长上时,我需要帮助(这会导致块在地图之外)。好吧,你付出了很多努力(可能太多了,但没关系),因此upvoteYou先生是个天才(我的大脑被击中了lol):D只要确保编辑
if(x == chunkWidth && y == chunkWidth) break;