Example #1
0
  /**
   * Handles a pickup packet from the server. The level then gives the appropriate player the item
   * parsed in the packet.
   *
   * @param username - username of the player picking up the item
   * @param tileID - ID of the tile that the item was on
   * @param itemID - ID of the item to be picked up
   */
  public void handlePickUp(String username, int tileID, int itemID) {

    Player player = getPlayer(username);

    // We do not want to handle a pickup from the player on this computer
    if (player.equals(game.getPlayer())) return;

    Room currentRoom = player.getRoom();
    Tile tile = currentRoom.getTile(player, tileID);

    if (tile != null) tile.onEnter(player);
    else System.err.println("Tile is NULL");
  }
Example #2
0
  /**
   * Handles an interact packet sent from the server. Interactions are then performed by the
   * appropriate player on the appropriate tile.
   *
   * @param username - username of the player interacting
   * @param ID - ID of the tile to interact with
   */
  public void handleInteract(String username, int ID) {

    Player player = getPlayer(username);

    // We do not want to handle an interact from the player on this computer
    if (player.equals(game.getPlayer())) return;

    Room currentRoom = player.getRoom();
    Tile tile = currentRoom.getTile(player, ID);

    if (tile != null) {
      // System.out.println(tile.getClass().getName());
      tile.onInteract(player);
    }

    for (Player p : players) {
      // System.out.println(p.getUsername() + " is in: "
      // + p.getRoom().getName());
    }
  }
Example #3
0
  /**
   * Set the teams and assign the players models according to their teams.
   *
   * @param players - array of player usernames.
   * @param teams - team allocating, "0" for guard and "1" for spy.
   */
  public void setTeams(String[] players, String[] teams) {

    // System.out.println("Players: " + players.length + ", Teams: "
    // + teams.length);
    //
    // for (String s : teams)
    // System.out.print(s + ", ");
    // System.out.println("===================");
    //
    // System.out.println(this.players.size());
    //
    // for (String s : players)
    // System.out.print(s + ", ");
    // System.out.println("===================");

    for (int i = 0; i < players.length; i++) {

      // Assign the team
      Player p = getPlayer(players[i]);

      if (p == null) System.err.println("Player " + players[i] + " is null");

      p.setSide((teams[i].equals("0") ? Team.GUARD : Team.SPY));

      // The string will reference a pre-loaded model in the renderer
      String model = (teams[i].equals("0") ? "Guard" : "Spy");

      // Translation and rotations
      SpawnPoint spawn = getNextSpawn(p.getSide());
      p.setRoom(spawn.room);
      p.setX(spawn.x);
      p.setY(spawn.y);
      Vec3 trans = new Vec3(p.getX(), 0, p.getY());
      Vec3 rot = new Vec3(0, -p.getRotation(), 0);
      Vec3 scale = new Vec3(0.1, 0.1, 0.1);

      // Player model

      // Sets up the renderer for drawing the teams correctly
      R_Player.Team rteam = R_Player.Team.SPY;
      if (p.getSide() == Team.GUARD) {
        rteam = R_Player.Team.GUARD;
      }
      R_Player pl =
          new R_Player(p.getUsername(), game.getR_ModelData(model), rteam, trans, rot, scale);

      // Assign the model to the player and the renderer
      p.setModel(pl);
      // game.r_addModel(pl);
    }

    // for (Player p : this.players) {
    // if (p.getRoom().equals(game.getPlayer().getRoom()))
    // game.r_addModel(p.getModel());
    // }
    // for (Player p : this.players) {
    // System.out.println(p.getUsername() + " is a "
    // + p.getSide().toString());
    // }

    // Now that the models have been loaded we can finally render the level
    readyToRender = true;
  }
Example #4
0
  /**
   * Go through each player and check what action they are doing. Also, go through each tile and
   * update it.
   */
  public void tick() {

    // System.out.println(game.getPlayer().getUsername() + "'s game");

    // for (Player p : players)
    // System.out.println(p.getUsername() + "is in: " +
    // p.getRoom().getName());
    // Go through players
    for (Player p : players) {
      if (!p.isAlive()) {
        game.r_removeModel(p.getUsername());
      }

      // Only render the player if they are alive
      if (readyToRender && p.isAlive()) {

        // If the player is in the same room as the player on this
        // computer, add them to the renderer
        if (p.getRoom().equals(game.getPlayer().getRoom())) {
          if (game.r_addModel(p.getModel())) {
            // System.out.println("Adding " + p.getUsername()
            //		+ "'s model in "
            //		+ game.getPlayer().getUsername() + "'s game");
          }
        }

        // If the player is not in the same room as the
        // player on this computer, remove them from the
        // renderer
        else if (!p.getRoom().equals(game.getPlayer().getRoom())) {
          game.r_removeModel(p.getUsername());
        }

        // Update the room
        if (!p.isRoomLoaded() && p.equals(game.getPlayer())) {

          if (p.getOldRoom() != null) p.getOldRoom().removeTiles(game.getRenderer());

          p.getRoom().initTiles(game.getRenderer());
          p.setRoomLoaded(true);
        }

        // Update player
        p.tick();

        // Packet to be sent to the server
        Packet packet = null;

        // Player shooting
        if (p.isShooting()) {
          packet = new Packet03Engage(p.getUsername());
          // packet.writeData(game.getClient());
        }

        // Player interacting
        // Check if player is interacting with a tile
        if (p.isInteracting()
            && p.getRoom() != null
            && p.equals(game.getPlayer())
            && p.getRoom().validPosition(p, p.getX(), p.getY())) {
          Tile tile = p.getRoom().getTile(p, p.getX(), p.getY());
          // isInteracting = false;
          // tile.onInteract(p);

          // Find the type of interaction
          switch (p.getInteraction()) {
            case CHEST:
            case DOOR:
            case TERMINAL:
              packet = new Packet06Interact(p.getUsername(), tile.getID());
              break;

            case NONE:
            default:
              break;
          }

          // Interact with the tile
          tile.onInteract(p);

          // Reset the interaction back to NONE
          p.resetInteraction();

          p.setInteracting(false);

          if (packet != null) packet.writeData(game.getClient());
        }

        // Check health
        if (!p.getUsername().equals(game.getPlayer().getUsername())) {
          Player pl = getPlayer(game.getPlayer().getUsername());

          if (pl.getRoom().equals(p.getRoom())
              && pl.getSide() == Team.GUARD
              && p.getSide() == Team.SPY
              && pl.inRange(p.getX(), p.getY())) {
            // getPlayer(game.getPlayer().getUsername())
            // .takeDamage(0.1);
            packet = new Packet04Damage(p.getUsername(), p.getUsername(), 0.5);
            packet.writeData(game.getClient());
          }
        }

        // Player moving
        if (p.isMoving()) {
          packet =
              new Packet02Move(
                  p.getUsername(),
                  ((PlayerMP) p).getID(),
                  p.getX(),
                  p.getY(),
                  p.getZ(),
                  true,
                  p.getRotation());
          packet.writeData(game.getClient());
        }

        // Player picking up item
        if (p.itemPickedUp()) {
          Tile tile = p.getRoom().getTile(p, p.getX(), p.getY());
          Item last = p.getLastItem();
          p.setItemPickedUp(false);
          packet = new Packet10Pickup(p.getUsername(), tile.getID(), last.getID());
          packet.writeData(game.getClient());
        }

        // Finally tick through the room that the player is in
        p.getRoom().tick(game.getRenderer(), game.getPlayer());
      }
    }
  }
Example #5
0
 /**
  * Handles a damage packet from the server. The method then gives the appropriate amount of damage
  * to the appropriate player.
  *
  * @param username - username of player affected
  * @param damage - amount of damage to deal
  */
 public void handleDamage(String username, double damage) {
   Player player = getPlayer(username);
   if (player.isAlive()) player.takeDamage(damage);
   else if (!player.isAlive()) game.getRenderer().deleteModel(username);
 }