コード例 #1
1
  public void protocolGameInfo() {
    /* On vérifie que c'est bien la première fois qu'on reçoit les informations (pour cela on regarde
     * si l'ensemble des joueurs est vide) */
    if (pong.setPlayers.isEmpty()) {
      /* On place la balle à la bonne position */
      int positionBallX = Integer.parseInt(tab[1]);
      int positionBallY = Integer.parseInt(tab[2]);
      Ball ball = pong.getBall();
      ball.setPosition(new Point(positionBallX, positionBallY));
      pong.setBall(ball);

      /* On instancie tous les joueurs distants */
      for (int i = 3; i < tab.length; i++) {
        String[] curPlayer = tab[i].split(";");
        PlayerID playerID = PlayerID.valueOf(curPlayer[0]);
        int score = Integer.parseInt(curPlayer[1]);
        int positionRacketX = Integer.parseInt(curPlayer[2]);
        int positionRacketY = Integer.parseInt(curPlayer[3]);
        String host = curPlayer[4];
        int port = Integer.parseInt(curPlayer[5]);

        Player player = new Player(playerID, score, positionRacketX, positionRacketY, host, port);
        pong.setPlayers.add(player);
      }
    }
  }
コード例 #2
0
  public void protocolPlayer() {
    String[] curPlayer = tab[1].split(";");
    PlayerID playerID = PlayerID.valueOf(curPlayer[0]);
    int positionRacketX = Integer.parseInt(curPlayer[2]);
    int positionRacketY = Integer.parseInt(curPlayer[3]);

    /* On met à jour la position du joueur */
    Iterator<Player> it = pong.setPlayers.iterator();
    while (it.hasNext()) {
      Player player = it.next();
      if (player.getPlayerID() == playerID) {
        Racket racket = player.getRacket();

        /* Anti-cheat */
        if ((positionRacketX > (racket.getPositionX() + racket.getSpeed()) && racket.getSpeed() > 0)
            || (positionRacketX < (racket.getPositionX() + racket.getSpeed())
                && racket.getSpeed() < 0)
            || (positionRacketY > (racket.getPositionY() + racket.getSpeed())
                && racket.getSpeed() > 0)
            || (positionRacketY < (racket.getPositionY() + racket.getSpeed())
                && racket.getSpeed() < 0)) {
          errorCheat("Raquette avec coordonnées invalides (cheat)");
        }

        racket.setPosition(new Point(positionRacketX, positionRacketY));
        player.setRacket(racket);
      }
    }
  }
コード例 #3
0
  public void protocolBall() {
    /* On met à jour la position de la balle */
    int positionBallX = Integer.parseInt(tab[1]);
    int positionBallY = Integer.parseInt(tab[2]);

    Ball ball = pong.getBall();

    /* Anti-cheat */
    if ((positionBallX > (ball.getPositionX() + ball.getSpeedX()) && ball.getSpeedX() > 0)
        || (positionBallX < (ball.getPositionX() + ball.getSpeedX()) && ball.getSpeedX() < 0)
        || (positionBallY > (ball.getPositionY() + ball.getSpeedY()) && ball.getSpeedY() > 0)
        || (positionBallY < (ball.getPositionY() + ball.getSpeedY()) && ball.getSpeedY() < 0)) {
      errorCheat("Balle avec coordonnées invalides (cheat)");
    }

    ball.setPosition(new Point(positionBallX, positionBallY));
    pong.setBall(ball);

    /* On met à jour les scores */
    for (int i = 3; i < tab.length; i++) {
      String[] curPlayer = tab[i].split(";");
      PlayerID playerID = PlayerID.valueOf(curPlayer[0]);
      int score = Integer.parseInt(curPlayer[1]);

      Iterator<Player> it = pong.setPlayers.iterator();
      while (it.hasNext()) {
        Player player = it.next();
        if (player.getPlayerID() == playerID) {
          player.setScore(score);
        }
      }
    }
  }
コード例 #4
0
  public void protocolNewPlayer() {
    /* On instancie un nouveau joueur distant */
    String[] curPlayer = tab[1].split(";");
    PlayerID playerID = PlayerID.valueOf(curPlayer[0]);
    int score = Integer.parseInt(curPlayer[1]);
    int positionRacketX = Integer.parseInt(curPlayer[2]);
    int positionRacketY = Integer.parseInt(curPlayer[3]);
    String host = curPlayer[4];
    int port = Integer.parseInt(curPlayer[5]);

    Player player = new Player(playerID, score, positionRacketX, positionRacketY, host, port);
    pong.setPlayers.add(player);
  }