update TileMapJsonLoader to use an AssetManager. add support for lighting mode saving/loading
This commit is contained in:
parent
09d9fb0f57
commit
51a71a9e32
|
@ -4,62 +4,62 @@ import ca.blarg.gdx.Strings;
|
|||
import ca.blarg.gdx.tilemap3d.ChunkVertexGenerator;
|
||||
import ca.blarg.gdx.tilemap3d.TileChunk;
|
||||
import ca.blarg.gdx.tilemap3d.TileMap;
|
||||
import ca.blarg.gdx.tilemap3d.json.tilemap.JsonTileMap;
|
||||
import ca.blarg.gdx.tilemap3d.json.tilemap.TileDataSerializer;
|
||||
import ca.blarg.gdx.tilemap3d.lighting.LightSpreadingTileMapLighter;
|
||||
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.tilemesh.TileMeshCollection;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.assets.AssetManager;
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.utils.Base64Coder;
|
||||
import com.badlogic.gdx.utils.Json;
|
||||
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TileMapJsonLoader {
|
||||
public static TileMap load(String mapFile, TileMeshCollection tileMeshes) {
|
||||
return load(Gdx.files.internal(mapFile), tileMeshes);
|
||||
public static JsonTileMap load(FileHandle file) {
|
||||
Json json = new Json();
|
||||
return json.fromJson(JsonTileMap.class, file);
|
||||
}
|
||||
|
||||
public static TileMap load(FileHandle mapFile, TileMeshCollection tileMeshes) {
|
||||
if (mapFile == null)
|
||||
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)
|
||||
public static TileMap create(JsonTileMap definition, AssetManager assetManager) {
|
||||
if (definition.chunks == null || definition.chunks.size() == 0)
|
||||
throw new RuntimeException("Invalid map: no chunks.");
|
||||
int numChunks = (jsonMap.widthInChunks * jsonMap.heightInChunks * jsonMap.depthInChunks);
|
||||
if (jsonMap.chunks.size() != numChunks)
|
||||
int numChunks = (definition.widthInChunks * definition.heightInChunks * definition.depthInChunks);
|
||||
if (definition.chunks.size() != numChunks)
|
||||
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;
|
||||
TileMapLighter lighter = null;
|
||||
|
||||
if (Strings.isNullOrEmpty(jsonMap.lightingMode)) {
|
||||
if (Strings.isNullOrEmpty(definition.lightingMode)) {
|
||||
chunkVertexGenerator = new ChunkVertexGenerator();
|
||||
lighter = null;
|
||||
} else if (jsonMap.lightingMode.equalsIgnoreCase("simple")) {
|
||||
throw new NotImplementedException();
|
||||
} else if (jsonMap.lightingMode.equalsIgnoreCase("skyAndSources")) {
|
||||
throw new NotImplementedException();
|
||||
} else if (definition.lightingMode.equalsIgnoreCase("simple")) {
|
||||
chunkVertexGenerator = new LitChunkVertexGenerator();
|
||||
lighter = new SimpleTileMapLighter();
|
||||
} else if (definition.lightingMode.equalsIgnoreCase("skyAndSources")) {
|
||||
chunkVertexGenerator = new LitChunkVertexGenerator();
|
||||
lighter = new LightSpreadingTileMapLighter(true, true);
|
||||
} else
|
||||
throw new RuntimeException("Invalid lighting mode.");
|
||||
|
||||
TileMap tileMap = new TileMap(
|
||||
jsonMap.chunkWidth, jsonMap.chunkHeight, jsonMap.chunkDepth,
|
||||
jsonMap.widthInChunks, jsonMap.heightInChunks, jsonMap.depthInChunks,
|
||||
definition.chunkWidth, definition.chunkHeight, definition.chunkDepth,
|
||||
definition.widthInChunks, definition.heightInChunks, definition.depthInChunks,
|
||||
tileMeshes,
|
||||
chunkVertexGenerator,
|
||||
lighter
|
||||
);
|
||||
|
||||
for (int i = 0; i < jsonMap.chunks.size(); ++i) {
|
||||
String encodedChunk = jsonMap.chunks.get(i);
|
||||
for (int i = 0; i < definition.chunks.size(); ++i) {
|
||||
String encodedChunk = definition.chunks.get(i);
|
||||
TileChunk outputChunk = tileMap.getChunks()[i];
|
||||
|
||||
byte[] chunkBytes = Base64Coder.decode(encodedChunk);
|
||||
|
@ -83,8 +83,12 @@ public class TileMapJsonLoader {
|
|||
jsonMap.heightInChunks = tileMap.getHeight() / jsonMap.chunkHeight;
|
||||
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;
|
||||
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)
|
||||
int chunkSizeInBytes = tileMap.getChunks()[0].getData().length * TileDataSerializer.TILE_SIZE_BYTES;
|
||||
|
|
Loading…
Reference in a new issue