/** * Makes all the works that must be done by the map in a step * * @return */ public void step() { this.removeCollisions(); this.moveCars(); this.emitCars(); this.codify(); for (Car car : this.travelingCars) car.perceiveAndReason(); }
/** 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); } } }
/** * @param id * @return */ public Car getCar(long id) { for (Car car : travelingCars) { if (car.getId() == id) return car; } return null; }
/** * 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()); }
/** @param pos */ private void add(CarPosition pos) { Car car = availableCars.pop(); car.init(pos); add(car); }
/** * Adds a list of norms to all the cars in the simulation * * @param norms */ public void broadcastRemoveNorm(Norm norm) { for (Car c : allCars) { c.getReasoner().removeNorm(norm); } }
/** * Adds a list of norms to all the cars in the simulation * * @param norms */ public void broadcastAddNorm(Norm norm) { for (Car c : allCars) { c.getReasoner().addNorm(norm); } }