示例#1
0
  public void
      checkTPress() // this method whether T key is being pressed o not and move the object
                    // according do following.
      {
    if (alive) // check if the ship alive
    {
      DeepSpace myDeepSpace = (DeepSpace) getWorld(); // get referance to the DeepSpace class
      FuelTank myFuelTank =
          myDeepSpace
              .myFuelTank(); // use myFuelTank method that return reference of FuelTank class. So
                             // you can call method of Fueltank class.

      if (Greenfoot.isKeyDown("t")
          && !myFuelTank
              .empty()) // if "t" key is being pressed by user and if i have enough fuel, do
                        // following.
      {

        move(3); // move forward direction of object
        setImage("rocket.png"); // set the image rocket that seems like burning fuel.
        myFuelTank
            .burn(); // call the Fueltank class method in order to decrese level of fuel on screen
      } else
        setImage("rocket_noThrust.png"); // if no key is being press than set the regular picture.
    }
  }
 public void useReserve() {
   if (reserves >= 1) {
     if (tank.isEmpty()) {
       dead = false;
     }
     tank.setFuelLevel(tank.getFuelCapacity());
     reserves--;
   }
 }
 public void move(int distance, Direction direction) {
   if (!dead) {
     move(distance, direction, true);
     if (tank.isEmpty()) {
       dead = true;
       notifyPropertyChangeListeners(new PropertyChangeEvent(this, "dead", false, true));
     }
   }
 }
 public void move(int distance, Direction direction, boolean useFuel) {
   switch (direction) {
     case UP:
       location.y -= distance;
       break;
     case DOWN:
       location.y += distance;
       break;
     case LEFT:
       location.x -= distance;
       break;
     case RIGHT:
       location.x += distance;
       break;
     default:
       assert false : "Bad direction: " + location;
   }
   if (useFuel) tank.useFuel(1);
 }
示例#5
0
  /** Example. */
  @Test
  public void example() {
    double dblReserve = TankSize.RESERVE.size() * 2.0; // RESERVE size is set to 1600
    FuelTank largeReserve = new FuelTank(dblReserve);
    FuelTank tripleReserve = new FuelTank(TankSize.RESERVE.multi(3));
    FuelTank normalReserve = new FuelTank(TankSize.RESERVE);
    FuelTank miniTank = new FuelTank(2); // 2 Gallon tank
    FuelTank fiveGalTank = new FuelTank(5);
    FuelTank largeFuelTank = new FuelTank(TankSize.LARGE);
    Airplane smallPlane =
        new Airplane(
            new Random().nextInt(),
            Airline.AAL,
            largeFuelTank,
            AirplaneState.WAITINHANGAR,
            Airline.AAL.getHangar());
    Airplane p1 = new Airplane();
    p1.setId(1);
    p1.setOwner(Airline.getRandom());
    p1.setTank(new FuelTank(new Random().nextGaussian() * 100));
    p1.setHolder(p1.getOwner().getHangar());

    /**
     * //How a taxi would be created Taxi t1 = new Taxi(new Random().nextInt(), null, new
     * FuelTank(TankSize.SMALL.multi((new Random().nextGaussian()*.5))), VehicleState.WAITING,
     * Taxiway.A); Taxi t2 = new Taxi(); t2.setId(2); t2.setOwner(Airport.AUS); t2.setTank(new
     * FuelTank(new Random().nextGaussian()*15)); t2.setHolder(t2.getOwner().chooseTaxiway());
     */
    /*
     * All Tanks are empty, you must either fill() or add(Fuel f)
     * Vehicles can refuel() although this fills to FULL not MAX
     */
    try {
      tripleReserve.fill(); // Magically fill tank to capacity
      largeReserve.fill(tripleReserve); // Fill tank from source
      normalReserve.fill(tripleReserve);
    } catch (FuelTankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      assert (tripleReserve.isEmpty());
      assert (largeReserve.isFull());
      assert (normalReserve.isFull());
    }

    try {
      largeFuelTank.fill(normalReserve);
      fiveGalTank.fill(largeFuelTank);
      miniTank.fill(largeFuelTank);
      smallPlane.getTank().fill(largeFuelTank);

    } catch (FuelTankException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      assert (!largeFuelTank.isEmpty());
      assert (!normalReserve.isEmpty());
      assert (miniTank.isFull());
      assert (smallPlane.getTank().isFull());
    }
  }
 public void refuel(int fuel) {
   tank.refuel(fuel);
   if (!tank.isEmpty()) {
     setDead(false);
   }
 }