package ruscz.game.gameOfProd.BattleUI; import ruscz.game.gameOfProd.Battle.Battle; import ruscz.game.gameOfProd.Scripts.Utils; import ruscz.game.gameOfProd.Windows.WindowOfBattle; import javax.swing.*; import java.awt.*; import java.util.Random; import java.util.logging.Logger; import static ruscz.game.gameOfProd.Windows.WindowOfBattle.*; /** * JPanel with menu of Action, that User can do */ public class MenuOfActions extends JPanel { private final JButton attack; private final JButton console; private final JButton inventory; private final JButton escape; private static final Logger LOG = Utils.setSettingsToLogger(Logger.getLogger(Battle.class.getName()), "BattleLog"); public MenuOfActions(WindowOfBattle window) { setLayout(new GridLayout(4,1)); setSize(CELL_SIZE * CANVAS_WIDTH / 3, CELL_SIZE * CANVAS_HEIGHT / 4); setLocation(CELL_SIZE * CANVAS_WIDTH * 2 / 3, CELL_SIZE * CANVAS_HEIGHT * 3 / 4); setVisible(false); setBorder(BorderFactory.createLineBorder(new Color(213, 108, 18), 2)); attack = window.style(new JButton("Attack")); console = window.style(new JButton("Console")); inventory = window.style(new JButton("Items")); escape = window.style(new JButton("Run Away")); attack.addActionListener(e -> { attack.setEnabled(false); console.setEnabled(false); inventory.setEnabled(false); escape.setEnabled(false); window.getBattle().setPlayerAttack(true); window.getBattle().getTimer().start(); }); console.addActionListener(e -> { attack.setEnabled(false); console.setEnabled(false); inventory.setEnabled(false); escape.setEnabled(false); window.getConsoleInput().setVisible(true); }); inventory.addActionListener(e -> { attack.setEnabled(false); console.setEnabled(false); inventory.setEnabled(false); escape.setEnabled(false); window.getItemsInventory().refreshItemsInField(); window.getItemsInventory().setVisible(true); }); escape.addActionListener(e -> { attack.setEnabled(false); console.setEnabled(false); inventory.setEnabled(false); escape.setEnabled(false); if (window.isRunAwayPossible()) { LOG.fine("Try to escape"); Random r = new Random(); if (r.nextBoolean()) { window.getBattle().endBattle(true); LOG.fine("RunAway is successful"); } else { window.getMenuOfActions().setVisible(false); window.getEnemyDescription().setVisible(false); window.getSituationText().setVisible(true); window.writeToOutput("Runaway was not successful."); LOG.fine("Runaway was not successful."); window.getBattle().addToTurnOrder(window.getBattle().getHeroes().getFirst()); window.getBattle().setTurnBelongTo(null); window.getBattle().setDelay(2); window.getBattle().getTimer().start(); } } else { window.getMenuOfActions().setVisible(false); window.getEnemyDescription().setVisible(false); window.getSituationText().setVisible(true); window.writeToOutput("Runaway is not possible."); LOG.fine("Runaway is not possible."); window.getBattle().addToTurnOrder(window.getBattle().getHeroes().getFirst()); window.getBattle().setTurnBelongTo(null); window.getBattle().setDelay(2); window.getBattle().getTimer().start(); } }); add(attack); add(console); add(inventory); add(escape); } public JButton getAttack() { return attack; } public JButton getConsole() { return console; } public JButton getInventory() { return inventory; } public JButton getEscape() { return escape; } }