Exemple #1
0
  private boolean bounceBall(BounceableControl control) {
    /*
    log.debug("Cheking...");
    log.debug("\tcontrol: "+control.getPosition().toString()+", ball: "+ball.getPosition().toString());
    log.debug("\tvector: x="+ballUnitVector.getX()+", y="+ballUnitVector.getY());
    */

    boolean isHit = false;
    if ((control.isVisible()) && (control.isHit(ball.getPosition()))) {
      log.debug("bounced!!!!!!!!!!!!!!");

      float angleDelta = 90.0f - control.getRotation();

      // get current angle of ball vector
      Point previousBallPosition = new Point(0.0f, 0.0f);
      Point currentBallPosition = new Point(ballUnitVector.getX(), ballUnitVector.getY());
      // float ballVectorMagnitude = PointUtility.getDistance(previousBallPosition,
      // currentBallPosition);
      float currentBallAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
      log.debug("\t\tcurrentBallAngle=" + currentBallAngle);

      // get new vector
      float targetBallAngle = (currentBallAngle + angleDelta);
      float newX = (float) Math.cos(Math.toRadians(targetBallAngle));
      float newY = (float) Math.sin(Math.toRadians(targetBallAngle));
      log.debug("\t\tnew vector: x=" + newX + ", y=" + newY);
      newX = (-1 * newX);

      // back to original angle
      previousBallPosition.set(0.0f, 0.0f);
      currentBallPosition.set(newX, newY);
      log.debug("\t\tnewX=" + newX + ", newY=" + newY);
      float newAngle = PointUtility.getAngle(previousBallPosition, currentBallPosition);
      targetBallAngle = (newAngle - angleDelta);
      log.debug("\t\tnew angle=" + newAngle + ", targetBallAngle=" + targetBallAngle);

      newX = (float) Math.cos(Math.toRadians(targetBallAngle));
      newY = (float) Math.sin(Math.toRadians(targetBallAngle));
      ballUnitVector.set(newX, newY);
      ballUnitVector = ballUnitVector.normalise(null);
      log.debug("\t\tfinal: x=" + ballUnitVector.getX() + ", y=" + ballUnitVector.getY());

      isHit = true;
    }

    return isHit;
  }
Exemple #2
0
  private void handleTouchs(TouchEvent touchEvent, boolean ended) {
    if (GameState.Ready.equals(gameState)) {
      gameState = GameState.Start;
      return;
    }
    if (!GameState.Playing.equals(gameState)) {
      return;
    }

    // log.debug("handleTouches: "+touchEvent.getObjectObserverEvents().size()+", "+ended);
    if (ended) {
      for (BounceableControl paddle : paddles.values()) {
        paddle.setVisible(false);
      }
    } else {
      List<ObjectObserverEvent> ooes = touchEvent.getObjectObserverEvents();

      // try finding the pair
      for (int i = 0; i < NUMBER_OF_PADDLES; i++) {
        int idA = (i * 2);
        int idB = (i * 2) + 1;
        ObjectObserverEvent ooeA = null;
        ObjectObserverEvent ooeB = null;
        for (ObjectObserverEvent ooe : ooes) {
          if (ooe.getId() == idA) {
            ooeA = ooe;
          } else if (ooe.getId() == idB) {
            ooeB = ooe;
          }
        }

        BounceableControl paddle = paddles.get(i);
        if ((ooeA != null) && (ooeB != null)) {
          Point pointA = new Point(ooeA.getX(), ooeA.getY());
          Point pointB = new Point(ooeB.getX(), ooeB.getY());
          float distance = PointUtility.getDistance(pointA, pointB);
          float angle = PointUtility.getAngle(pointA, pointB, false);
          Point center =
              new Point(
                  (pointA.getX() + ((pointB.getX() - pointA.getX()) / 2.0f)),
                  (pointA.getY() + ((pointB.getY() - pointA.getY()) / 2.0f)));

          // log.debug("center: "+center.toString());
          // log.debug("angle: "+angle);
          // log.debug("distance: "+distance);

          paddle.setVisible(true);
          paddle.setSize(new Size(distance, PADDLE_SIZE));
          paddle.setPosition(center);
          paddle.setRotation(angle);
        } else {
          paddle.setVisible(false);
        }
      }
    }
  }
Exemple #3
0
  private void onGameStatePlaying() {
    // ball
    Point position = ball.getPosition();
    if (shadowCount >= SHADOW_SKIP) {
      if (previousBallPositions.size() >= NUMBER_OF_BALL_SHADOWS) {
        previousBallPositions.removeLast();
      }
      previousBallPositions.addFirst(new Point(position));
      shadowCount = 0;
    } else {
      shadowCount += 1;
    }
    position.add((ballSpeed * ballUnitVector.getX()), -(ballSpeed * ballUnitVector.getY()));
    ball.setPosition(position);
    ball.setVisible(true);

    for (int i = 0; i < NUMBER_OF_BALL_SHADOWS; i++) {
      VisualControl visualControl = ballShadows.get(i);
      if (previousBallPositions.size() > i) {
        visualControl.setPosition(previousBallPositions.get(i));
        visualControl.setVisible(true);
      } else {
        visualControl.setVisible(false);
      }
    }

    // bounce
    if (bouncedControl != null) {
      if (!bouncedControl.isHit(ball.getPosition())) {
        bouncedControl = null;
      }
    } else {
      for (BounceableControl bounceableControl : paddles.values()) {
        bounceableControls.add(bounceableControl);
      }

      for (BounceableControl bounceableControl : bounceableControls) {
        if (bounceBall(bounceableControl)) {
          bouncedControl = bounceableControl;
          break;
        }
      }

      for (BounceableControl bounceableControl : paddles.values()) {
        bounceableControls.remove(bounceableControl);
      }
    }

    // goal
    if ((position.getX() <= 0.0f) || (position.getX() >= (float) displayMode.getWidth())) {
      gameState = GameState.End;
    }
  }
Exemple #4
0
  private void loadImages() throws Exception {
    // background
    backgroundControl = new FramedControl();
    backgroundControl.setColor(Color.BLACK);
    backgroundControl.setOpacity(0.5f);
    backgroundControl.setSize(new Size(displayMode.getWidth(), displayMode.getHeight()));
    backgroundControl.setTopLeftPosition(new Point(0, 0));
    WindowManager.getInstance().setBackgroundControl(backgroundControl);

    // middle dot
    middleDotControl = new VisualControl();
    middleDotControl.setTexture(getClass().getResource(URL_IMAGE_MIDDLE_DOT));
    Size middleDotControlSize = middleDotControl.getSize();
    middleDotControl.setSize(new Size(middleDotControlSize.getWidth(), displayMode.getHeight()));
    middleDotControl.setTopLeftPosition(
        new Point(((displayMode.getWidth() / 2.0f) - (middleDotControlSize.getWidth() / 2.0f)), 0));

    // top bar
    BounceableControl topBar = new BounceableControl();
    topBar.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    topBar.setSize(new Size(displayMode.getWidth(), BAR_SIZE));
    topBar.setTopLeftPosition(new Point(0, 0));
    bounceableControls.add(topBar);

    // bottom bar
    BounceableControl bottomBar = new BounceableControl();
    bottomBar.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    bottomBar.setSize(new Size(displayMode.getWidth(), BAR_SIZE));
    bottomBar.setTopLeftPosition(new Point(0, (displayMode.getHeight() - BAR_SIZE)));
    bounceableControls.add(bottomBar);

    // win/lose
    wonControl = new VisualControl();
    // wonControl.setColor(Color.BLACK);
    wonControl.setTexture(getClass().getResource(URL_IMAGE_WON));
    wonControl.setVisible(false);
    lostControl = new VisualControl();
    // lostControl.setColor(Color.BLACK);
    lostControl.setTexture(getClass().getResource(URL_IMAGE_LOST));
    lostControl.setVisible(false);

    // paddles
    for (int i = 0; i < NUMBER_OF_PADDLES; i++) {
      BounceableControl paddle = new BounceableControl();
      paddle.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
      paddle.setVisible(false);
      paddles.put(i, paddle);
    }

    // ball shadows
    for (int i = 0; i < NUMBER_OF_BALL_SHADOWS; i++) {
      float opacity = (1.0f - ((1.0f / (float) (NUMBER_OF_BALL_SHADOWS + 1)) * (i + 1)));
      float size =
          (BALL_SIZE * (NUMBER_OF_BALL_SHADOWS / (float) (NUMBER_OF_BALL_SHADOWS + (i + 1))));
      log.debug("opacity: " + opacity + ", size: " + size);

      VisualControl visualControl = new VisualControl();
      visualControl.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
      visualControl.setSize(new Size(size, size));
      visualControl.setOpacity(opacity);
      visualControl.setVisible(false);
      ballShadows.addLast(visualControl);
    }

    // ball
    ball = new VisualControl();
    // ball.setColor(Color.BLACK);
    ball.setTexture(getClass().getResource(URL_IMAGE_WHITE_RECTANGLE));
    ball.setSize(new Size(BALL_SIZE, BALL_SIZE));
    // ball.setMargin(1.0f);
    ball.setVisible(false);

    // listeners
    backgroundControl.addTouchListener(this);
  }