update TileMapJsonLoader to use an AssetManager. add support for lighting mode saving/loading

This commit is contained in:
Gered 2014-04-15 14:24:19 -04:00
parent 09d9fb0f57
commit 51a71a9e32

View file

@ -4,62 +4,62 @@ import ca.blarg.gdx.Strings;
import ca.blarg.gdx.tilemap3d.ChunkVertexGenerator; import ca.blarg.gdx.tilemap3d.ChunkVertexGenerator;
import ca.blarg.gdx.tilemap3d.TileChunk; import ca.blarg.gdx.tilemap3d.TileChunk;
import ca.blarg.gdx.tilemap3d.TileMap; import ca.blarg.gdx.tilemap3d.TileMap;
import ca.blarg.gdx.tilemap3d.json.tilemap.JsonTileMap; import ca.blarg.gdx.tilemap3d.lighting.LightSpreadingTileMapLighter;
import ca.blarg.gdx.tilemap3d.json.tilemap.TileDataSerializer; import ca.blarg.gdx.tilemap3d.lighting.LitChunkVertexGenerator;
import ca.blarg.gdx.tilemap3d.lighting.SimpleTileMapLighter;
import ca.blarg.gdx.tilemap3d.lighting.TileMapLighter; import ca.blarg.gdx.tilemap3d.lighting.TileMapLighter;
import ca.blarg.gdx.tilemap3d.tilemesh.TileMeshCollection; import ca.blarg.gdx.tilemap3d.tilemesh.TileMeshCollection;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder; import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json; import com.badlogic.gdx.utils.Json;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
public class TileMapJsonLoader { public class TileMapJsonLoader {
public static TileMap load(String mapFile, TileMeshCollection tileMeshes) { public static JsonTileMap load(FileHandle file) {
return load(Gdx.files.internal(mapFile), tileMeshes); Json json = new Json();
return json.fromJson(JsonTileMap.class, file);
} }
public static TileMap load(FileHandle mapFile, TileMeshCollection tileMeshes) { public static TileMap create(JsonTileMap definition, AssetManager assetManager) {
if (mapFile == null) if (definition.chunks == null || definition.chunks.size() == 0)
throw new IllegalArgumentException();
if (tileMeshes == null)
throw new IllegalArgumentException();
Json json = new Json();
JsonTileMap jsonMap = json.fromJson(JsonTileMap.class, mapFile);
if (jsonMap.chunks == null || jsonMap.chunks.size() == 0)
throw new RuntimeException("Invalid map: no chunks."); throw new RuntimeException("Invalid map: no chunks.");
int numChunks = (jsonMap.widthInChunks * jsonMap.heightInChunks * jsonMap.depthInChunks); int numChunks = (definition.widthInChunks * definition.heightInChunks * definition.depthInChunks);
if (jsonMap.chunks.size() != numChunks) if (definition.chunks.size() != numChunks)
throw new RuntimeException("Inconsistent map dimensions and number of chunks."); throw new RuntimeException("Inconsistent map dimensions and number of chunks.");
if (definition.tileMeshes == null)
throw new RuntimeException("No tile mesh collection specified.");
TileMeshCollection tileMeshes = assetManager.get(definition.tileMeshes, TileMeshCollection.class);
ChunkVertexGenerator chunkVertexGenerator = null; ChunkVertexGenerator chunkVertexGenerator = null;
TileMapLighter lighter = null; TileMapLighter lighter = null;
if (Strings.isNullOrEmpty(jsonMap.lightingMode)) { if (Strings.isNullOrEmpty(definition.lightingMode)) {
chunkVertexGenerator = new ChunkVertexGenerator(); chunkVertexGenerator = new ChunkVertexGenerator();
lighter = null; lighter = null;
} else if (jsonMap.lightingMode.equalsIgnoreCase("simple")) { } else if (definition.lightingMode.equalsIgnoreCase("simple")) {
throw new NotImplementedException(); chunkVertexGenerator = new LitChunkVertexGenerator();
} else if (jsonMap.lightingMode.equalsIgnoreCase("skyAndSources")) { lighter = new SimpleTileMapLighter();
throw new NotImplementedException(); } else if (definition.lightingMode.equalsIgnoreCase("skyAndSources")) {
chunkVertexGenerator = new LitChunkVertexGenerator();
lighter = new LightSpreadingTileMapLighter(true, true);
} else } else
throw new RuntimeException("Invalid lighting mode."); throw new RuntimeException("Invalid lighting mode.");
TileMap tileMap = new TileMap( TileMap tileMap = new TileMap(
jsonMap.chunkWidth, jsonMap.chunkHeight, jsonMap.chunkDepth, definition.chunkWidth, definition.chunkHeight, definition.chunkDepth,
jsonMap.widthInChunks, jsonMap.heightInChunks, jsonMap.depthInChunks, definition.widthInChunks, definition.heightInChunks, definition.depthInChunks,
tileMeshes, tileMeshes,
chunkVertexGenerator, chunkVertexGenerator,
lighter lighter
); );
for (int i = 0; i < jsonMap.chunks.size(); ++i) { for (int i = 0; i < definition.chunks.size(); ++i) {
String encodedChunk = jsonMap.chunks.get(i); String encodedChunk = definition.chunks.get(i);
TileChunk outputChunk = tileMap.getChunks()[i]; TileChunk outputChunk = tileMap.getChunks()[i];
byte[] chunkBytes = Base64Coder.decode(encodedChunk); byte[] chunkBytes = Base64Coder.decode(encodedChunk);
@ -83,8 +83,12 @@ public class TileMapJsonLoader {
jsonMap.heightInChunks = tileMap.getHeight() / jsonMap.chunkHeight; jsonMap.heightInChunks = tileMap.getHeight() / jsonMap.chunkHeight;
jsonMap.depthInChunks = tileMap.getDepth() / jsonMap.chunkDepth; jsonMap.depthInChunks = tileMap.getDepth() / jsonMap.chunkDepth;
// TODO: figure out real lighting mode from the types of vertex generator / lighter objects set if (tileMap.lighter == null)
jsonMap.lightingMode = null; jsonMap.lightingMode = null;
else if (tileMap.lighter instanceof SimpleTileMapLighter)
jsonMap.lightingMode = "simple";
else if (tileMap.lighter instanceof LightSpreadingTileMapLighter)
jsonMap.lightingMode = "skyAndSources";
// each serialized chunk will be the same size in bytes (same number of tiles in each) // each serialized chunk will be the same size in bytes (same number of tiles in each)
int chunkSizeInBytes = tileMap.getChunks()[0].getData().length * TileDataSerializer.TILE_SIZE_BYTES; int chunkSizeInBytes = tileMap.getChunks()[0].getData().length * TileDataSerializer.TILE_SIZE_BYTES;