/**
  * Returns true if a grid point of the map is free (with no car)
  *
  * @param p
  * @return
  */
 private boolean isFree(GridPoint p) {
   int count = 0;
   Iterable<TrafficElement> elements = map.getObjectsAt(p.getX(), p.getY());
   for (TrafficElement elem : elements) {
     count++;
   }
   return count == 0;
 }
  /**
   * @param x
   * @param y
   * @return
   */
  private int getNumElements(int x, int y) {
    Iterable<TrafficElement> elements = map.getObjectsAt(x, y);
    int elemsCount = 0;

    Iterator<TrafficElement> iterator = elements.iterator();
    while (iterator.hasNext()) {
      iterator.next();
      elemsCount++;
    }
    return elemsCount;
  }
  /** 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);
      }
    }
  }
Пример #4
0
  public void infect() {
    GridPoint pt = grid.getLocation(this);
    List<Object> humans = new ArrayList<Object>();
    for (Object obj : grid.getObjectsAt(pt.getX(), pt.getY())) {
      if (obj instanceof Human) {
        humans.add(obj);
      }
    }

    if (humans.size() > 0) {
      int index = RandomHelper.nextIntFromTo(0, humans.size() - 1);
      Object obj = humans.get(index);
      NdPoint spacePt = space.getLocation(obj);
      Context<Object> context = ContextUtils.getContext(obj);
      context.remove(obj);
      Zombie zombie = new Zombie(space, grid);
      context.add(zombie);
      space.moveTo(zombie, spacePt.getX(), spacePt.getY());
      grid.moveTo(zombie, pt.getX(), pt.getY());

      Network<Object> net = (Network<Object>) context.getProjection("infection network");
      net.addEdge(this, zombie);
    }
  }