Example #1
0
  private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {

    timePassed += deltaTime;

    for (TouchEvent event : touchEvents) {
      if (event.type == TouchEvent.TOUCH_DOWN) {

        if (event.x < buttonWidth) {
          player.moveLeft();
        } else if (event.x > buttonWidth + game.getGraphics().getHeight()) {
          player.moveRight();
        }
      }
    }

    if (!player.isAlive) {
      connection.write("LOST".getBytes());
      opponent.scored();
      if (opponent.getScore() == goal) {
        ((TronGame) game).saveLose();
        state = GameState.Loser;
      } else {
        countdown = 300.0f;
        state = GameState.Lose;
      }
    } else if (timePassed > 10.0) {
      player.update();
      grid.update(player);
      timePassed -= 10.0f;
    }
  }
Example #2
0
  /**
   * Parse incoming message.
   *
   * @param bytes byte array of received message
   * @param num number of bytes received
   */
  public void parse(byte[] bytes, int num) {
    String msg = new String(bytes);
    if (num > 0) {
      if (msg.charAt(0) == 'X' && state == GameState.Running) {
        Log.d("**TRON**", "receiving coords...");
        Scanner scanner = new Scanner(msg);
        int x = scanner.useDelimiter("[^\\d]+").nextInt();
        int y = scanner.useDelimiter("[^\\d]+").nextInt();
        grid.setOpponent(x, y);
        Log.d("**TRON**", "received coords: " + x + " " + y);
      }
      if (msg.startsWith("LOST")) {
        player.scored();
        if (player.getScore() >= goal) {
          ((TronGame) game).saveWin();
          state = GameState.Winner;
        } else {
          state = GameState.Win;
        }
      }

      if (msg.startsWith("WIN")) {
        if (state == GameState.Running) {
          connection.write("LOST".getBytes());
          opponent.scored();
          if (opponent.getScore() == goal) {
            ((TronGame) game).saveLose();
            state = GameState.Loser;
          } else {
            countdown = 300.0f;
            state = GameState.Lose;
          }
        }
      }

      if (msg.startsWith("READY")) {
        connection.write("START".getBytes());
        countdown = 300.0f;
        state = GameState.Ready;
        restart();
      }
      if (msg.startsWith("START")) {
        countdown = 300.0f;
        state = GameState.Ready;
        restart();
      }
      if (msg.startsWith("EXIT")) {
        countdown = 300.0f;
        state = GameState.Error;
        restart();
      }
    }
  }