Here the situation: I'm developpping a clone of an old Tiled base game. I'm using LIBGDX api. And i'm wondering about how I should manage the loading of my ressources.
I have few maps (3000*3000 tiles).
I could easily use tmx file to represent my map and load it but : All my floor texture are large seamless texture cut in differents tiles and I need to generate transitions tiles dynamically between them.
I tried to generate all the transitions tiles and to use a tmx file but it take a huge amount of space and the game i'm trying to copy doesn't have those tiles and it launch quickly.
So I tried to create a TileMap that I populate by reading the basic map file I got.
Here the code :
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));
}
}
What I do here is basically the same than in a Tmx file. I list the needed ressource them I use them in may map.
----Algorythm ---
For each tile id, i'm finding the linked texture
If the texture is not contained by the TiledMapTileSet
I store that texture in a TileMapTileSet And I put the tile into the layer
Else
I put the tile into the layer
---end---
To calcul that and to load ressources it take around 2 minutes. Its a lot and in the old game it was quicker than that(20seconds). It is actually quicker also if I use tmx but i won't generated all the tiled for transitions before.
How the libgdx load work with tmx file to make them so fast to load?
How should I do for generate my map efficiently?
What is the best practices in game about ressources loading?