Ejemplo n.º 1
0
  /** Updates graph: finds closest station to each actor in world. Does not occur at every frame. */
  public void update() {
    if (frames_since_update++ < UPDATE_AFTER_FRAMES) return;
    frames_since_update = 0;

    // For each vehicle in the world, determine which station is the closest.
    Actor[] actors = world.getActors();
    for (Actor actor : actors) {
      if (!(actor instanceof Vehicle)) continue;
      Vehicle vehicle = (Vehicle) actor;

      // Iterate over each station until the closest one is found.
      Station closest_station = null;
      float closest_station_distance = Float.MAX_VALUE;
      for (Station station : stations) {
        if (station == null) continue;

        float new_distance = vehicle.distance(station);
        if (new_distance < closest_station_distance) {
          closest_station = station;
          closest_station_distance = new_distance;
        }
      }

      vehicle.setClosestStation(closest_station);
    }
  }