add TilePrefabLoader

This commit is contained in:
Gered 2013-10-14 22:21:44 -04:00
parent 8c8582d044
commit dd4b3ac27f

View file

@ -0,0 +1,32 @@
package com.blarg.gdx.tilemap3d.prefabs.json;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.Json;
import com.blarg.gdx.Strings;
import com.blarg.gdx.tilemap3d.json.TileDataSerializer;
import com.blarg.gdx.tilemap3d.prefabs.TilePrefab;
import java.nio.ByteBuffer;
public class TilePrefabLoader {
public static TilePrefab load(FileHandle prefabFile) {
if (prefabFile == null)
throw new IllegalArgumentException();
Json json = new Json();
JsonTilePrefab jsonPrefab = json.fromJson(JsonTilePrefab.class, prefabFile);
if (Strings.isNullOrEmpty(jsonPrefab.data))
throw new RuntimeException("Invalid prefab: no tile data.");
TilePrefab prefab = new TilePrefab(jsonPrefab.width, jsonPrefab.height, jsonPrefab.depth);
byte[] dataBytes = Base64Coder.decode(jsonPrefab.data);
ByteBuffer buffer = ByteBuffer.wrap(dataBytes);
TileDataSerializer.deserialize(buffer, prefab);
return prefab;
}
}