Exemplo n.º 1
0
 /**
  * Moves the character to the coordinates specified by the mouse event
  *
  * @param e the event which contains the coordinates to move to
  */
 public void moveCharacter(InputEvent2D e) {
   for (Drawable2D drawable : drawList) {
     if (drawable.getClass().equals(Player2D.class)) {
       drawable.moveTo(e.getCoordinates());
     }
   }
 }
Exemplo n.º 2
0
 /**
  * handles all events
  *
  * @param e the event to handle
  */
 @Override
 public void handleModelChangeEvent(ModelChangeEvent e) {
   displayMessage(e.getMessage());
   drawList = e.getDrawable();
   for (Drawable2D drawable : drawList) {
     if (drawable.getClass().equals(Player2D.class)) {
       player = (Player2D) drawable;
     }
     if (drawable.getClass().equals(Room2D.class)) {
       mapArea.setCurrentRoom((Room) drawable);
     }
     if (drawable.getClass().equals(Item2D.class)) {
       if (player.collidesWith(drawable)) {
         collidingWithObject = drawable;
       }
     }
   }
   drawArea.updateDrawable(drawList);
 }
Exemplo n.º 3
0
  /**
   * updates all the drawn objects
   *
   * @param delta time since the last update
   */
  @Override
  public void update(double delta) {

    for (Drawable2D drawable : drawList) {
      drawable.update(delta); // update the drawable

      if (!(drawable.equals(player))) { // if the drawable is not the player

        if (drawable.getClass().equals(Room2D.class)) {
          ExitDirection direction = ((Room2D) drawable).inExitBounds(player.getBounds());
          if (direction != null) { // if the player is in the exit bounds
            Point newPlayerLocation =
                new Point(drawable.getBounds().width / 2, drawable.getBounds().height / 2);
            player.setLocation(newPlayerLocation); // set player to the middle of the room
            notifyInputListeners(
                new InputEvent2D(new Command(CommandWord.go, direction.toString())));
          }
          continue;
        }

        // if the drawable is not the player and collides with the player
        if (collidingWithObject == null) {
          if (player.collidesWith(drawable)) {
            if (drawable.getClass().equals(Monster2D.class)) { // if player collides with a monster
              String monsterName =
                  ((Monster2D) drawable).getName(); // send input messages if it does collide
              notifyInputListeners(new InputEvent2D(new Command(CommandWord.attack, monsterName)));

              collidingWithObject = drawable;
              if (characterWindow != null && characterWindow.isVisible()) {
                characterWindow();
              }
            } else if (drawable
                .getClass()
                .equals(Item2D.class)) { // if player collides with an item
              String itemName = ((Item2D) drawable).getName();
              notifyInputListeners(new InputEvent2D(new Command(CommandWord.take, itemName)));

              collidingWithObject = drawable;
              if (inventoryWin != null && inventoryWin.isVisible()) {
                inventoryWindow();
              }
            }
          }
        } else {
          if (!player.collidesWith(
              collidingWithObject)) { // check if you move out of the colliding objects bounds
            collidingWithObject = null; // we are off the other object, set it to null
          }
        }
      }
    }

    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            drawArea.repaint();
            mapArea.repaint();
            textAreaPanel.repaint();
            mainWindow.repaint();
            mainWindow.validate();
          }
        });
  }