@Override public void render(GameContainer gc, Graphics g) throws SlickException { backgroundImage.draw(); Airplane airplaneHandle; Iterator<Airplane> it; it = airQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.render(g, airplaneImage); } it = groundQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.render(g, airplaneImage); } airplaneHandle = runway.getCurrentAirplane(); if (airplaneHandle != null) airplaneHandle.render(g, airplaneImage); g.setColor(Color.white); g.drawString("Airport Simulator", gc.getWidth() - 170, 10); g.drawString("Update: " + currentUpdate, gc.getWidth() - 130, gc.getHeight() - 30); }
@Override public void update(GameContainer gc, int delta) throws SlickException { rand.nextDouble(); if (System.currentTimeMillis() - lastUpdate > simulationDelay) { lastUpdate = System.currentTimeMillis(); runSimulation(); } Airplane airplaneHandle; Iterator<Airplane> it; it = airQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.update(delta); } it = groundQueue.iterator(); while (it.hasNext()) { airplaneHandle = it.next(); airplaneHandle.update(delta); } airplaneHandle = runway.getCurrentAirplane(); if (airplaneHandle != null) airplaneHandle.update(delta); }
public void run() { boolean isAlreadyTakenOff = false; while (!isAlreadyTakenOff) { if (RUNWAY.getTakingOffPlane() == null) { // если взлетная полоса свободна RUNWAY.setTakingOffPlane(this); System.out.println(getName() + " взлетает"); takingOff(); // взлетает System.out.println(getName() + " уже в небе"); isAlreadyTakenOff = true; RUNWAY.setTakingOffPlane(null); } else if (!this.equals( RUNWAY.getTakingOffPlane())) { // если взлетная полоса занята самолетом System.out.println(getName() + " ожидает"); waiting(); // ожидает } } }
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(); }