我试图为了节省客户端内存而不将整个基于瓷砖的地图加载到内存中。这张地图非常巨大,已经需要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++;
}
}
}
}
非常感谢您的任何帮助。