Example #1
0
 private Icon(String s, int xOffset, int numberOfIcons) {
   xmlList = ConfigManager.getStringList(ConfigManager.scope(s, "xmlList"));
   Image img =
       new Image(
           getClass()
               .getClassLoader()
               .getResourceAsStream(ConfigManager.getString(ConfigManager.scope(s, "image"))));
   image = new ImageView(img);
   image.setFitWidth(WIDTH / (numberOfIcons * 3 / 2));
   image.setPreserveRatio(true);
   image.setSmooth(true);
   image.setCache(true);
   setGraphic(image);
   setLayoutY(
       ConfigManager.getInt(ConfigManager.scope(this.getClass().getName(), "yPos"), HEIGHT / 3));
   setLayoutX(xOffset - (image.getBoundsInParent().getWidth() + 18) / 2);
   iconName = s;
 }
  /**
   * Change properties of shapes to animate them
   *
   * <p>Note, there are more sophisticated ways to animate shapes, but these simple ways work too.
   */
  public void step(double elapsedTime) {
    // update attributes
    myBouncer.setX(myBouncer.getX() + BOUNCER_SPEED * elapsedTime);
    myTopBlock.setRotate(myBottomBlock.getRotate() - 1);
    myBottomBlock.setRotate(myBottomBlock.getRotate() + 1);

    // check for collisions
    // with shapes, can check precisely
    Shape intersect = Shape.intersect(myTopBlock, myBottomBlock);
    if (intersect.getBoundsInLocal().getWidth() != -1) {
      myTopBlock.setFill(Color.MAROON);
    } else {
      myTopBlock.setFill(Color.RED);
    }
    // with images can only check bounding box
    if (myBottomBlock.getBoundsInParent().intersects(myBouncer.getBoundsInParent())) {
      myBottomBlock.setFill(Color.BURLYWOOD);
    } else {
      myBottomBlock.setFill(Color.BISQUE);
    }
  }
  @Override
  public void step(double elapsedTime) {
    Random rand = new Random();
    if (getTimeRemaining() < 0 && getGameStarted()) {
      exitLevel("You got beat, you didn't defeat Goodman!!");
    }
    if (getGameStarted()) {
      getMyRoot().getChildren().remove(getTimerLabel());
      setTimeRemaining(getTimeRemaining() - elapsedTime);
      Label timerLabel = new Label(Long.toString(Math.round(getTimeRemaining() - elapsedTime)));
      timerLabel.setTextFill(Color.BLACK);
      timerLabel.setStyle("-fx-font-size: 4em;");
      timerLabel.setLayoutX(getMyWidth() / 2 - getMyWidth() * .04);
      timerLabel.setLayoutY(getMyHeight() * .15);
      setTimerLabel(timerLabel);
      getMyRoot().getChildren().add(getTimerLabel());
      if (myDodgeball == null && rand.nextInt(100) == 0) {

        myDodgeball =
            new Dodgeball(
                getMyWidth() * .45,
                getMyHeight() * 2 / 3,
                villainPlayerIV.getX(),
                villainPlayerIV.getY(),
                20,
                Color.RED,
                false,
                false);

        getBallsInFlightList().add(myDodgeball);
        getMyRoot().getChildren().add(myDodgeball);
      }
      if (tossBall()) {
        int highOrLow = rand.nextInt(3) * 20;
        Dodgeball newEnemyBall =
            new Dodgeball(
                villainPlayerIV.getX(),
                villainPlayerIV.getY(),
                getMyPlayerIV().getX(),
                getMyPlayerIV().getY() + highOrLow,
                20,
                Color.PURPLE,
                true,
                true);

        getBallsInFlightList().add(newEnemyBall);
        getMyRoot().getChildren().add(newEnemyBall);
      }
      Iterator<Dodgeball> iter = getBallsInFlightList().iterator();
      while (iter.hasNext()) {
        Dodgeball currBall = iter.next();

        if (currBall.isBeingThrown()) {
          if (!currBall.isEnemyBall()) {
            currBall.setCenterX(currBall.getCenterX() + villainPlayer.getMyTossSpeed());
            currBall.setCenterY(
                currBall.getCenterX() * currBall.getTrajectorySlope()
                    + currBall.getTrajectoryYIntercept());
            // check for collisions
            if (villainPlayerIV.getBoundsInParent().intersects(currBall.getBoundsInParent())) {
              int currLives = enemyLivesHBox.getChildren().size();
              getMyRoot().getChildren().removeAll(currBall, enemyLivesHBox);
              setEnemyLivesHBox(currLives - 1);
              getMyRoot().getChildren().add(getEnemyLivesHBox());
              iter.remove();
              myDodgeball = null;
              if (enemyLivesHBox.getChildren().size() == 0) {
                exitLevel("You won!!");
              }
            }
          } else {
            currBall.setCenterX(currBall.getCenterX() - villainPlayer.getMyTossSpeed());
            currBall.setCenterY(
                currBall.getCenterX() * currBall.getTrajectorySlope()
                    + currBall.getTrajectoryYIntercept());
            //                     check for collisions
            if (getMyPlayerIV().getBoundsInParent().intersects(currBall.getBoundsInParent())) {
              iter.remove();
              getMyRoot().getChildren().removeAll(currBall, getMyLivesHBox());

              setMyLivesHBox(getMyLivesHBox().getChildren().size() - 1);
              getMyRoot().getChildren().add(getMyLivesHBox());
              if (getMyLivesHBox().getChildren().size() == 0) {
                exitLevel("You got beat!!");
              }
            }
          }
          double currX = currBall.getCenterX();
          if (currX < 0) {
            iter.remove();
            getMyRoot().getChildren().remove(currBall);
          }
        }
      }
    }
  }