/** * Load a terrain object from a JSON file * * @param mapFile * @return * @throws FileNotFoundException */ public static Terrain load(File mapFile) throws FileNotFoundException { Reader in = new FileReader(mapFile); JSONTokener jtk = new JSONTokener(in); JSONObject jsonTerrain = new JSONObject(jtk); int width = jsonTerrain.getInt("width"); int depth = jsonTerrain.getInt("depth"); Terrain terrain = new Terrain(width, depth); JSONArray jsonSun = jsonTerrain.getJSONArray("sunlight"); float dx = (float) jsonSun.getDouble(0); float dy = (float) jsonSun.getDouble(1); float dz = (float) jsonSun.getDouble(2); terrain.setSunlightDir(dx, dy, dz); JSONArray jsonAltitude = jsonTerrain.getJSONArray("altitude"); for (int i = 0; i < jsonAltitude.length(); i++) { int x = i % width; int z = i / width; double h = jsonAltitude.getDouble(i); terrain.setGridAltitude(x, z, h); } if (jsonTerrain.has("trees")) { JSONArray jsonTrees = jsonTerrain.getJSONArray("trees"); for (int i = 0; i < jsonTrees.length(); i++) { JSONObject jsonTree = jsonTrees.getJSONObject(i); double x = jsonTree.getDouble("x"); double z = jsonTree.getDouble("z"); terrain.addTree(x, z); } } if (jsonTerrain.has("others")) { JSONArray jsonTrees = jsonTerrain.getJSONArray("others"); for (int i = 0; i < jsonTrees.length(); i++) { JSONObject jsonTree = jsonTrees.getJSONObject(i); double x = jsonTree.getDouble("x"); double z = jsonTree.getDouble("z"); terrain.addOther(x, z); } } if (jsonTerrain.has("roads")) { JSONArray jsonRoads = jsonTerrain.getJSONArray("roads"); for (int i = 0; i < jsonRoads.length(); i++) { JSONObject jsonRoad = jsonRoads.getJSONObject(i); double w = jsonRoad.getDouble("width"); JSONArray jsonSpine = jsonRoad.getJSONArray("spine"); double[] spine = new double[jsonSpine.length()]; for (int j = 0; j < jsonSpine.length(); j++) { spine[j] = jsonSpine.getDouble(j); } terrain.addRoad(w, spine); } } return terrain; }