Ejemplo n.º 1
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);
        }
      }
    }
  }
Ejemplo n.º 2
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;
    }
  }
Ejemplo n.º 3
0
  private void onGameStateEnd() {
    Point position = ball.getPosition();
    if (position.getX() <= 0.0f) {
      wonControl.setPosition(
          new Point((displayMode.getWidth() * (3.0f / 4.0f)), (displayMode.getHeight() / 2.0f)));
      lostControl.setPosition(
          new Point((displayMode.getWidth() / 4.0f), (displayMode.getHeight() / 2.0f)));
    } else {
      wonControl.setPosition(
          new Point((displayMode.getWidth() / 4.0f), (displayMode.getHeight() / 2.0f)));
      lostControl.setPosition(
          new Point((displayMode.getWidth() * (3.0f / 4.0f)), (displayMode.getHeight() / 2.0f)));
    }
    wonControl.setVisible(true);
    lostControl.setVisible(true);

    gameState = GameState.Ready;
  }
Ejemplo n.º 4
0
 public static Point getOpenGlPosition(Size displaySize, Point position) {
   return new Point(position.getX(), (displaySize.getHeight() - position.getY()));
 }