/** closes the window on game end */ @Override public void dispose() { if (inventoryWin != null && inventoryWin.isVisible()) { inventoryWin.dispose(); } if (characterWindow != null && characterWindow.isVisible()) { characterWindow.dispose(); } mainWindow.dispose(); }
@Override public int loop() { if (window.isVisible()) { return 1000; } return -1; }
// The main method which starts the program. Goes in a loop while the window is still open and // while in the loop it displays certain JPanels depending on the variable mode public static void main(String[] args) { // Creates a Bomberman object Bomberman bomberman = new Bomberman(); // Loads all the images and sprites bomberman.load(); // Loops while the window is open while (window.isVisible()) { // Depending on mode, a certain JPanel will be made visible by calling a method switch (mode) { case 1: bomberman.options(); break; case 2: bomberman.rules(); break; case 3: bomberman.controls(); break; case 4: bomberman.credits(); break; case 5: if (game) bomberman.newGame(); break; } } }
@Override public void actionPerformed(ActionEvent e) { AbstractButton button = (AbstractButton) e.getSource(); try { if (button == btnQuit) { sendCommand(Command.QUIT); socket.close(); socket = null; setButtonStatus(false); } else if (button == btnConnection) { if (socket == null) connect(); else if (socket.isConnected()) { sendCommand(Command.QUIT); Thread.sleep(1000); socket.close(); connect(); } } else if (button == btnReset) { sendCommand(Command.RESET); } else if (button == btnShutdown) { sendCommand(Command.DISPOSE); } else if (button == btnSpeed1) { sendSpeed(1); } else if (button == btnSpeed2) { sendSpeed(2); } else if (button == btnSpeed3) { sendSpeed(3); } else if (button == btnSpeed4) { sendSpeed(4); } else if (button == btnSpeed5) { sendSpeed(5); } else if (button == btnCamera) { if (videoFrame == null || !videoFrame.isVisible()) { showVideo(); } else { hideVideo(); } } else if (commandMap.containsKey(button)) { sendCommand(commandMap.get(button)); } } catch (Exception ex) { ex.printStackTrace(); } }
/** * converts from actionEvent (from inventory button) to InputEvent2D and notifies * * @param arg0 the ActionEvent */ @Override public void actionPerformed(ActionEvent event) { if (event.getSource().getClass().equals(JButton.class)) { JButton pressed = (JButton) (event.getSource()); if (pressed.equals(inventoryButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.inventory, null))); if (inventoryWin != null && inventoryWin .isDisplayable()) { // closes window if its already open when you click the button inventoryWin.dispose(); } else { inventoryWindow(); } } else if (pressed.equals(characterButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.character, null))); if (characterWindow != null && characterWindow.isDisplayable()) { characterWindow.dispose(); } else { characterWindow(); } } else if (pressed.equals(undoButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.undo, null))); } else if (pressed.equals(redoButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.redo, null))); } else if (pressed.equals(helpButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.help, null))); } else if (pressed.equals(quitButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.quit, null))); } else if (pressed.equals(saveButton)) { notifyInputListeners(new InputEvent2D(new Command(CommandWord.save, null))); } else if (pressed.getClass().equals(JButton.class)) { // inventory or character button JButton src = (JButton) event.getSource(); if (src.getText().startsWith("drop")) { notifyInputListeners( new InputEvent2D(new Command(CommandWord.drop, "" + src.getText().substring(5)))); } else { notifyInputListeners(new InputEvent2D(new Command(CommandWord.use, "" + src.getText()))); } // updates windows if (inventoryWin != null && inventoryWin.isVisible()) { inventoryWindow(); } if (characterWindow != null && characterWindow.isVisible()) { characterWindow(); } } } if (event.getSource().getClass().equals(JTextField.class)) { JTextField source = (JTextField) (event.getSource()); displayMessage(source.getText()); String word1 = null; String word2 = null; Scanner tokenizer = new Scanner(source.getText()); if (tokenizer.hasNext()) { word1 = tokenizer.next(); // get first word if (tokenizer.hasNext()) { word2 = tokenizer.next(); // get second word // note: we just ignore the rest of the input line. } } tokenizer.close(); Command toNotify = new Command(CommandWord.getCommandFromString(word1), word2); notifyInputListeners(new InputEvent2D(toNotify)); source.setText(""); } }
/** * 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(); } }); }
/** * Provides information on visibility of the Canvas. * * @return true if canvas is visible, false otherwise */ public boolean isVisible() { return frame.isVisible(); }