【发布时间】:2015-12-05 16:20:47
【问题描述】:
这里的情况:我正在开发一个旧的 Tiled 基础游戏的克隆。我正在使用 LIBGDX api。我想知道我应该如何管理资源的加载。
我的地图很少(3000*3000 个图块)。
我可以轻松地使用 tmx 文件来表示我的地图并加载它,但是: 我所有的地板纹理都是在不同瓷砖中切割的大型无缝纹理,我需要在它们之间动态生成过渡瓷砖。
我尝试生成所有转换图块并使用 tmx 文件,但它占用了大量空间,而且我尝试复制的游戏没有这些图块,而且启动速度很快。
所以我尝试创建一个 TileMap,通过读取我得到的基本地图文件来填充它。
这里是代码:
MapLayers layers = worldMap.getLayers();
TiledMapTileSets tileSets = worldMap.getTileSets();
TiledMapTileLayer floorLayer = new TiledMapTileLayer(WORLD_WIDTH,WORLD_HEIGHT,TILE_WIDTH, TILE_HEIGHT);
TiledMapTileSet floorTileSet = new TiledMapTileSet();
layers.add(floorLayer);
tileSets.addTileSet(floorTileSet);
floorLayer.setCell(0, 0, new Cell());
byte[] rawData = null;
try {
rawData = java.nio.file.Files.readAllBytes(Paths.get(".."+System.getProperty("file.separator")+"core"+System.getProperty("file.separator")+"assets"+System.getProperty("file.separator")+"map"+System.getProperty("file.separator")+"v2_worldmap.map"));
} catch (IOException e) {
e.printStackTrace();
}
CompressedMap originalMap = new CompressedMap(rawData);
UncompressedMap uncompMap = originalMap.getUncompressedMap();
NormalizedMap normalizedMap = uncompMap.getNormalizedMap();
int originalMapTileid = 20;
int newTileId = 0;
int tileSetId = 0;
for(int y = 0; y < 3072; y++){
for(int x = 0; x < 3072 ;x++){
floorLayer.setCell(x, 3071-y, new Cell());
originalMapTileid = normalizedMap.getIdAtPos(x, y);
String textname = ToolKit.findTextureNameByIdAndPos(assetManager, (short) originalMapTileid, x, y);
if(!textureNameToTileId.containsKey(textname)){
textureNameToTileId.put(textname, newTileId);
//System.out.println(ToolKit.textPathTable.get(textname).toString());
floorTileSet.putTile(newTileId, new StaticTiledMapTile(new TextureRegion(new Texture(ToolKit.textPathTable.get(textname)))));
tileSetId = newTileId;
newTileId++;
}else{
tileSetId = textureNameToTileId.get(textname);
}
floorLayer.getCell(x, 3071-y).setTile(floorTileSet.getTile(tileSetId));
}
}
我在这里所做的与在 Tmx 文件中所做的基本相同。我列出了我在可能的地图中使用它们所需的资源。
----算法---
对于每个图块 id,我正在查找链接的纹理
如果纹理不包含在 TiledMapTileSet 中
我将该纹理存储在 TileMapTileSet 然后我把瓦片放到图层中
其他
我把瓦片放到图层里
---结束---
计算并加载资源大约需要 2 分钟。它很多,在旧游戏中它比这更快(20秒)。如果我使用 tmx,它实际上也会更快,但我不会在之前生成所有平铺的过渡。
libgdx 加载如何与 tmx 文件一起工作以使其加载速度如此之快?
如何高效地生成地图?
游戏中关于资源加载的最佳做法是什么?
【问题讨论】: