package com.arretadogames.pilot.util.parsers; import android.util.Log; import com.arretadogames.pilot.entities.EntityType; import com.arretadogames.pilot.entities.PlayerNumber; import com.arretadogames.pilot.levels.EntityDescriptor; import com.arretadogames.pilot.levels.GroundDescriptor; import com.arretadogames.pilot.levels.HoleDescriptor; import com.arretadogames.pilot.levels.LevelDescriptor; import com.arretadogames.pilot.levels.PlayerDescriptor; import com.arretadogames.pilot.levels.WaterDescriptor; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; public class LevelParser { public static boolean parseJSON(String jsonString, LevelDescriptor level) { try { JSONObject master = new JSONObject(jsonString); JSONArray entitiesJsonArray = master.getJSONArray("entities"); List<EntityDescriptor> entities = new ArrayList<EntityDescriptor>(); for (int i = 0 ; i < entitiesJsonArray.length(); i++) { JSONObject jsonEntity = entitiesJsonArray.getJSONObject(i); // Convert jsonEntity to EntityDescriptor String entityType = jsonEntity.getString("type"); EntityDescriptor entity; if (EntityType.BOX.toString().equals(entityType)) { // BOX entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.BOX, (float) jsonEntity.getDouble("size")); } else if (EntityType.SEED.toString().equals(entityType)) { // COIN entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.SEED); } else if (EntityType.PLAYER.toString().equals(entityType)) { // PLAYER PlayerNumber pNumber; int pNumberInt = jsonEntity.getInt("number"); switch (pNumberInt) { case 1: pNumber = PlayerNumber.ONE; break; case 2: pNumber = PlayerNumber.TWO; break; default: Log.e("LevelDescriptor.jsonParse()", "PlayerNumber " + pNumberInt + " not defined"); continue; } entity = new PlayerDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.PLAYER, pNumber); } else if (EntityType.ONEWAY_WALL.toString().equals(entityType)){ // ONEWAY_WALL entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.ONEWAY_WALL); } else if (EntityType.TREE.toString().equals(entityType)) { entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.TREE); } else if (EntityType.SHRUB.toString().equals(entityType)) { entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.SHRUB); } else if (EntityType.GRASS.toString().equals(entityType)) { entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.GRASS); } else if (EntityType.BREAKABLE.toString().equals(entityType)) { // BREAKABLE entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.BREAKABLE); } else if (EntityType.FINALFLAG.toString().equals(entityType)) { // FINAL FLAG entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.FINALFLAG); }else if(EntityType.WATER.toString().equals(entityType)){ entity = new WaterDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.WATER, (float) jsonEntity.getDouble("width"), (float) jsonEntity.getDouble("height"), (float) jsonEntity.getDouble("density")); }else if(EntityType.HOLE.toString().equals(entityType)){ entity = new HoleDescriptor( (float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("distance")); }else if(EntityType.BOX_ITEM.toString().equals(entityType)){ entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.BOX_ITEM); } else if (EntityType.TREELOG.toString().equals(entityType)) { // BOX entity = new EntityDescriptor((float) jsonEntity.getDouble("x"), (float) jsonEntity.getDouble("y"), EntityType.TREELOG, (float) jsonEntity.getDouble("size")); }else { // Entity not defined Log.e("LevelDescriptor.jsonParse()", "Entity " + entityType + " not defined"); continue; } entities.add(entity); } GroundDescriptor groundDescriptor = new GroundDescriptor(); // Set Level Data level.setData(entities, groundDescriptor); float levelLength = (float) master.getDouble("width"); level.setLevelLength(levelLength); return true; } catch (JSONException ex) { ex.printStackTrace(); } return false; } }