Exemplo n.º 1
0
  /** Displays the character window */
  public void characterWindow() {
    if (characterWindow != null) {
      characterWindow.dispose();
    }
    characterWindow = new JFrame("Character");
    characterWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    JTextField health = new JTextField(player.health());
    health.setEditable(false);

    JButton weapon = new JButton(player.weapon().split(" ")[1]);
    if (player.weapon().split(" ")[1].equals("none")) {
      weapon.setEnabled(false);
    }
    weapon.addActionListener(this);

    JButton armor = new JButton(player.armor().split(" ")[1]);
    if (player.armor().split(" ")[1].equals("none")) {
      armor.setEnabled(false);
    }
    armor.addActionListener(this);

    characterWindow.setBounds(
        mainWindow.getX() + mainWindow.getWidth(), mainWindow.getY(), 200, 150);
    characterWindow.setResizable(false);
    characterWindow.setLayout(new GridLayout(3, 1));
    characterWindow.add(health);
    characterWindow.add(weapon);
    characterWindow.add(armor);

    characterWindow.setVisible(true);
  }
Exemplo n.º 2
0
 /** Displays the inventory window */
 public void inventoryWindow() {
   if (inventoryWin != null) {
     inventoryWin.dispose();
   }
   inventoryWin = new JFrame("Inventory");
   inventoryWin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
   Inventory inv = player.getInventory();
   int y = inv.getSize();
   if (y == 0) {
     displayMessage("Inventory is empty");
     return;
   }
   int cols = 2;
   int rows = (y - y % cols) / cols;
   if (y % cols != 0) rows = rows + 1;
   inventoryWin.setLayout(new GridLayout(0, cols * 2));
   for (int i = 0; i < y; i++) {
     JButton b = new JButton(inv.getItem(i).getName());
     inventoryWin.add(b);
     b.addActionListener(this);
     JButton d = new JButton("drop " + inv.getItem(i).getName());
     inventoryWin.add(d);
     d.addActionListener(this);
   }
   // inventoryWin.setBounds(350, 0, 300, 80*rows);
   inventoryWin.setLocation(mainWindow.getX(), mainWindow.getY() + mainWindow.getHeight());
   inventoryWin.pack();
   inventoryWin.setVisible(true);
 }
Exemplo n.º 3
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.º 4
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();
          }
        });
  }