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; //from w ww. j av a 2s .c om import java.util.LinkedList; import jonathan.geoffroy.zorbscity.model.cityObjects.buildings.Construction; /** * The time-line count elapsed time (at each calling of nextStep). It check and call the trigger of elapsed events * Adding & Removing will be sort the list. You should always call the method add(SimulatorEvent) & remove(SimulatorEvent). Don't force the index of adding. * This is clearly a violation of List because it not add at the end of the List * I will fix this semantic problem on a next version. * @author Jonathan GEOFFROY * @version 0.1 */ public class TimeLine extends LinkedList<SimulatorEvent> { /** * Auto-generated serial (using eclipse) */ private static final long serialVersionUID = 1547372495953597740L; /** * The time since for the city is running * Corresponding to the number of steps the simulator has already ran. */ private long time; @Override public boolean add(SimulatorEvent e) { assert(e.getHandlerTime() > time) : "you cannot add an event if it handlerTime is already reached"; int i = 0; for(SimulatorEvent val : this) { if(val.compareTo(e) > 0) { add(i, e); return true; } i++; } super.add(e); return true; } /** * Remove all events created by <construction> * @param construction */ public void removeEventsFrom(Construction construction) { LinkedList<SimulatorEvent> toRemove = new LinkedList<SimulatorEvent>(); for(SimulatorEvent e : this) { if (e.getCreator().equals(construction)) { toRemove.add(e); } } removeAll(toRemove); } /** * Do the next step of the simulation * call onTrigger(<sim>) for each SimulatorEvent which have reached the time of the next step * @param sim */ public void nextStep(Simulator sim) { time++; SimulatorEvent event; boolean continued = true; while (continued && !isEmpty()) { event = getFirst(); if(event.compareTo(time) <= 0) { removeFirst(); event.onTrigger(sim); } else { continued = false; } } } public long getTime() { return time; } }