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.cityObjects.buildings; //w ww. ja v a2s .c om import jonathan.geoffroy.zorbscity.model.City; import jonathan.geoffroy.zorbscity.model.Simulator; import jonathan.geoffroy.zorbscity.model.SimulatorEvent; import jonathan.geoffroy.zorbscity.model.TimeLine; import jonathan.geoffroy.zorbscity.model.helpers.Coord2D; import jonathan.geoffroy.zorbscity.model.simulator.events.WeaknessEvent; public class Construction implements Structure { /** * Auto-generated serial (using eclipse) */ private static final long serialVersionUID = -4923918125032064842L; public static final int TIME_TO_DECREASE_WEAKNESS = 16; /** * the coordinates of Structure in the city */ private Coord2D coords; /** * The weakness of a Building * Increased at each simulationAct * This Building should be destroyed when the weakness equals WEAKNESS_WHEN_DESTROYED. */ private int weakness; /** * the number of people who's inside the Building. * For House, it's the number of inhabitants * For Enterprise, it's the number of employees. * The that a Person can be in a House and in an Enterprise. */ protected int numberPeople; public Construction() { weakness = 100; } public Construction(Construction other) { this.coords = other.coords; this.weakness = other.weakness; } @Override public void addInto(City city, Coord2D coords) { this.coords = coords; Coord2D mappingCoords = new Coord2D(coords.x, coords.y); int size = getMappingSize(); for(int i = 0; i < size; i++) { mappingCoords.y = coords.y + i; for(int j = 0; j < size; j++) { mappingCoords.x = coords.x + j; city.setMapping(City.CONSTRUCTION_MAPPING, mappingCoords); } } // city.setMapping(City.CONSTRUCTION_MAPPING, coords); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((coords == null) ? 0 : coords.hashCode()); result = prime * result + numberPeople; result = prime * result + weakness; return result; } public boolean equals(Coord2D other) { return coords.x <= other.x && coords.x + getMappingSize() -1 >= other.x && coords.y <= other.y && coords.y + getMappingSize() -1 >= other.y; } @Override public int getMappingSize() { return 2; } public int getNumberPeople() { return numberPeople; } public void addPeople(Simulator sim, int numberPeople) { this.numberPeople += numberPeople; } public void setNumberPeople(int numberPeople) { this.numberPeople = numberPeople; } public Coord2D getCoords() { return coords; } /** * Called when a SimulatorEvent submitted by this Building is triggered * To nothing from this implementation: it used to catch the simulator events from each king of Building * @param simulatorEvent * @param sim */ public void onTriggeredEvent(SimulatorEvent simulatorEvent, Simulator sim) { if(simulatorEvent instanceof WeaknessEvent) { onTriggeredEvent((WeaknessEvent) simulatorEvent, sim); } } public void onTriggeredEvent(WeaknessEvent simulatorEvent, Simulator sim) { weakness--; if(weakness <= 0) { sim.destroy(this); } else { simulatorEvent.incHandlerTime(TIME_TO_DECREASE_WEAKNESS); sim.getTimeLine().add(simulatorEvent); } } /** * Should be called when the building has just being placed into the city * Useful to create the first event which initialize this building into simulator * @param sim */ public void onAddedIntoCity(Simulator sim) { TimeLine timeline = sim.getTimeLine(); timeline.add(new WeaknessEvent(this, timeline.getTime() + TIME_TO_DECREASE_WEAKNESS)); } public int getWeakness() { return weakness; } public void setWeakness(int weakness) { this.weakness = weakness; } }