add MaterialTileMappingJsonLoader

This commit is contained in:
Gered 2014-04-15 10:09:00 -04:00
parent d37c67e4ba
commit 0260286f96

View file

@ -0,0 +1,52 @@
package ca.blarg.gdx.tilemap3d.json.tilemesh;
import ca.blarg.gdx.graphics.atlas.TextureAtlas;
import ca.blarg.gdx.loaders.textureatlas.TextureAtlasJsonLoader;
import ca.blarg.gdx.tilemap3d.tilemesh.MaterialTileMapping;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Json;
public class MaterialTileMappingJsonLoader {
public static JsonMaterialMapping load(FileHandle file) {
Json json = new Json();
return json.fromJson(JsonMaterialMapping.class, file);
}
public static MaterialTileMapping create(JsonMaterialMapping definition) {
return create(definition, null);
}
public static MaterialTileMapping create(JsonMaterialMapping definition, AssetManager assetManager) {
if (definition.materials == null || definition.materials.size() == 0)
throw new RuntimeException("No material mappings defined.");
TextureAtlas atlas;
if (assetManager != null)
atlas = assetManager.get(definition.textureAtlas, TextureAtlas.class);
else
atlas = TextureAtlasJsonLoader.loadAndCreate(Gdx.files.internal(definition.textureAtlas));
MaterialTileMapping materialMapping = new MaterialTileMapping();
for (int i = 0; i < definition.materials.size(); ++i) {
JsonMaterialMapping.Material mapping = definition.materials.get(i);
materialMapping.add(
mapping.name, atlas.get(mapping.tile),
mapping.minU, mapping.maxU, mapping.minV, mapping.maxV
);
}
return materialMapping;
}
public static MaterialTileMapping loadAndCreate(FileHandle file) {
JsonMaterialMapping definition = load(file);
return create(definition);
}
public static MaterialTileMapping loadAndCreate(FileHandle file, AssetManager assetManager) {
JsonMaterialMapping definition = load(file);
return create(definition, assetManager);
}
}