Back to project page ZorbsCity.
The source code is released under:
GNU General Public License
If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jonathan.geoffroy.zorbscity.model; /*w w w .j av a2 s .co m*/ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.HashMap; import jonathan.geoffroy.zorbscity.model.helpers.Coord2D; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; /** * The Galaxy is a container of Cities. So all Cities are in the Galaxy. * The Galaxy is a global view of the World, i.e. on all player (& computer) cities. * While you can see or search any cities, no one is loaded by the Galaxy. You have to choose one and begin to play with to load it. * @author Jonathan GEOFFROY * @version 0.1 */ public class Galaxy implements Serializable { /** * auto-generated serial (using eclipse IDE) */ private static final long serialVersionUID = 7732169533388534682L; /** * The name of the galaxy * useful to load the galaxy */ private String name; /** * Contains all cities of the map, describing by there coordinates * Note that as explained previously, on the name of cities are loaded, not the cities themselves */ private HashMap<Coord2D, String> cities; public Galaxy(String name) { super(); this.name = name; cities = new HashMap<Coord2D, String>(); } /** * Load the galaxy galaxyName * @param galaxyName the name of the Galaxy * @return the loaded Galaxy, or null if the galaxy don't exist */ public static Galaxy load(String galaxyName) { Galaxy g = null; try { FileHandle handle = Gdx.files.local("data/galaxies/" + galaxyName + ".galaxy"); ObjectInputStream reader = null; reader = new ObjectInputStream(new FileInputStream(handle.file())); g = (Galaxy) reader.readObject(); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return g; } /** * Save this galaxy in the file: ./data/galaxies/_name_.galaxy * erase the old file if the galaxy already exists */ public void save() { try { FileHandle handle = Gdx.files.local("data/galaxies/" + name + ".galaxy"); ObjectOutputStream writer = new ObjectOutputStream(handle.write(false)); writer.writeObject(this); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String getName() { return name; } public HashMap<Coord2D, String> getCities() { return cities; } public Object getCity(Coord2D coord) { return cities.get(coord); } }