Ejemplo n.º 1
0
  private void runSimulation() {
    System.out.println("Update: " + currentUpdate);
    currentUpdate++;

    Airplane airplaneHandle;

    // Randomly add planes
    for (int i = 0; i < 2; i++) {
      if (newLandingProbability >= Math.pow(rand.nextDouble(), i)) {
        airplaneHandle =
            new Airplane(
                generateAirplaneNumber(),
                app.getWidth(),
                landingY,
                landingX,
                landingY,
                30 * (simulationDelay / 1000.0));
        airQueue.add(airplaneHandle);

        System.out.println("Now queued for landing: " + airplaneHandle);
      }
    }
    if (newTakeoffProbability >= rand.nextDouble()) {
      airplaneHandle =
          new Airplane(
              generateAirplaneNumber(),
              app.getWidth(),
              takeoffY,
              takeoffX,
              takeoffY,
              30 * (simulationDelay / 1000.0));
      groundQueue.add(airplaneHandle);

      System.out.println("Now queued for takeoff: " + airplaneHandle);
    }

    // Load the runway
    if (runway.isVacant())
      if (!airQueue.isEmpty()) {
        runway.setOccupied(airQueue.poll(), true);
        runway.getCurrentAirplane().setDestPos(runwayX, runwayY);

        System.out.println("Now landing: " + runway.getCurrentAirplane());
      } else if (!groundQueue.isEmpty()) {
        runway.setOccupied(groundQueue.poll(), false);
        runway.getCurrentAirplane().setDestPos(runwayX, runwayY);

        System.out.println("Now taking off: " + runway.getCurrentAirplane());
      }

    // Set queued airplane destinations
    Iterator<Airplane> it;

    it = airQueue.iterator();
    int i = 0;
    while (it.hasNext()) {
      airplaneHandle = it.next();

      airplaneHandle.setDestPos(landingX + (airplaneImage.getWidth() + 10) * i, landingY);
      i++;
    }

    it = groundQueue.iterator();
    i = 0;
    while (it.hasNext()) {
      airplaneHandle = it.next();

      airplaneHandle.setDestPos(takeoffX + (airplaneImage.getWidth() + 10) * i, takeoffY);
      i++;
    }

    // Output information about the runway
    System.out.println(
        runway
            + "\nNumber of planes waiting to land: "
            + airQueue.size()
            + "\nNumber of planes waiting to take off: "
            + groundQueue.size());

    // Tick runway
    runway.tick();

    System.out.println();
  }