/** Executes step method for each car and adds them to their new position */ private void moveCars() { this.carsToRemove.clear(); this.positionsToCheck.clear(); // Cars compute their new position for (Car car : travelingCars) car.move(); // Move cars in the map for (Car car : travelingCars) { int x = car.getX(); int y = car.getY(); // Move car if (!this.isPositionOutOfBounds(x, y)) { map.moveTo(car, x, y); this.addPositionToCheck(car.getPosition().getGridPoint()); } // Car moved out of the map. Add for removal else { this.addCarToRemove(car); } } // Remove cars out of bounds for (Car car : this.carsToRemove) { remove(car); } // Manage collisions for (String posId : this.positionsToCheck.keySet()) { GridPoint pos = this.positionsToCheck.get(posId); int x = pos.getX(); int y = pos.getY(); if (this.getNumElements(x, y) > 1) { Collision col = new Collision(x, y, map); Iterable<TrafficElement> elements = map.getObjectsAt(x, y); remove(elements); context.add(col); map.moveTo(col, x, y); this.collisions.add(col); } } }
/** * Adds a car to the simulation * * @param car */ private void add(Car car) { travelingCars.add(car); context.add(car); map.moveTo(car, car.getX(), car.getY()); }