Пример #1
0
  void createShips() {

    Global.log("Creating ships...");
    int n = numPlayers();

    for (int i = 0; i < n; ++i) {

      Global.log("Creating ship for Player " + (i + 1) + " [" + playerNames[i] + "]...");

      final int I = i;

      if (i == playerID) {
        final PlayerShip ship = new PlayerShip(name, nextColor(), this, new Controls());
        ship.getControls().enabled = false;
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.SendFPS;
                  }

                  public void run() {
                    try {
                      sendMessage(ship.getControlBytes());
                    } catch (IOException ex) {
                      System.err.println(
                          "An exception occured via the ship of Player "
                              + (I + 1)
                              + " ["
                              + playerNames[I]
                              + "]: "
                              + ex.toString());
                      Global.onException();
                      stop();
                    }
                  }
                }));
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.ReceiveFPS;
                  }

                  public void run() {
                    byte[] data = null;
                    try {
                      data = playerByteStreams[I].read();
                    } catch (IOException ex) {
                      System.err.println("Error reading ship from server: " + ex);
                      Global.onException();
                      stop();
                      return;
                    }
                    if (data == null) return;
                    ship.fromBytes(data);
                  }
                }));
        addShip(ship);
      } else {
        final Ship ship = new Ship(playerNames[i], nextColor());
        timers.add(
            new FixedTimer(
                new FixedTask() {
                  public boolean fixedRate() {
                    return true;
                  }

                  public float FPS() {
                    return Global.ReceiveFPS;
                  }

                  public void run() {
                    byte[] data = null;
                    try {
                      data = playerByteStreams[I].read();
                    } catch (IOException ex) {
                      System.err.println("Error reading ship from server: " + ex);
                      Global.onException();
                      stop();
                      return;
                    }
                    if (data == null) return;
                    ship.fromBytes(data);
                  }
                }));
        addShip(ship);
      }

      Global.log("Done!");
    }

    Global.log("Done creating ships!");
  }