我试图不将我的整个基于图块的地图加载到内存中以节省RAM客户端.地图将是巨大的,已经是需要1GB客户端(多层地图).
我已经对Game Dev SO有了一些看法.我正在尝试将我的游戏地图的区域/块加载到内存中(即300x300),然后当玩家移动100步时移动阵列并根据方向加载100个新的图块.我已经尝试过这个版本的缩放版本,现在有一个通用的问题.
当playerX/Y坐标位于地图的周边时(这会导致块在地图之外),我需要帮助
这是我到目前为止所提出的内容(注意:玩家位于chunk和chunk的中心总是奇数大小)......它有以下问题(当角色位于地图的边缘时):
将characterX/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++;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢您的帮助.
我不确定这是否是您正在寻找的,但通常您会检查填充 的嵌套 for 循环内的边界groundLayer,而不是检查计数变量。这样会更加稳健。像这样的东西:
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++;
}
}
Run Code Online (Sandbox Code Playgroud)
让我知道这是否是您正在寻找的以及它是否有效!
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |