package ruscz.game.gameOfProd.Battle; import ruscz.game.gameOfProd.Cells.InteractionCell; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Windows.WindowOfBattle; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import javax.swing.*; import java.awt.*; import java.util.Collections; import java.util.LinkedList; import java.util.Random; public class Battle { private final LinkedList enemies; private final LinkedList heroes; private final WindowOfBattle windowOfBattle; private Timer timer; public void setTurnBelongTo(BattlePerson turnBelongTo) { this.turnBelongTo = turnBelongTo; } private BattlePerson turnBelongTo; private LinkedList turnOrder; private int turnCount; private int delay; private boolean playerAttack; public void setDelay(int delay) { this.delay = delay; } public boolean isTimerStopped() { return timerStopped; } public void setTimerStopped(boolean timerStopped) { this.timerStopped = timerStopped; } private boolean timerStopped; public BattlePerson getTurnBelongTo() { return turnBelongTo; } public void setPlayerAttack(boolean playerAttack) { windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false); this.playerAttack = playerAttack; } public Battle(LinkedList heroes, LinkedList enemies, WindowOfBattle window) { this.heroes = heroes; this.enemies = enemies; this.windowOfBattle = window; } public LinkedList getEnemies() { return enemies; } public LinkedList getHeroes() { return heroes; } public boolean checkHealthOfEnemies(){ return enemies.isEmpty(); } public boolean checkHealthOfHeroes(){ return heroes.isEmpty(); } public void startBattle() { turnBelongTo = heroes.getFirst(); windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.repaint(); turnOrder = new LinkedList<>(); timer = new Timer(1000, actionEvent -> { for (Enemy e: enemies) System.out.println(e.health); if (checkHealthOfEnemies()) endBattle(true); if (checkHealthOfHeroes()) endBattle(false); if(playerAttack) attackByHero(randomEnemy()); else if (delay != 0) delay--; else if (turnBelongTo == null) nextTurn(); else if (turnBelongTo.getClass().getName().equals(Enemy.class.getName())) attackByMonster(randomHero()); else { returnControlToPlayer(); } }); } private void endBattle(boolean b) { timer.stop(); timerStopped = true; windowOfBattle.setVisible(false); WindowOfFreeMove windowOfFreeMove = windowOfBattle.getGame().getFreeMove(); windowOfBattle.getMenuOfActions().getAttack().removeActionListener(windowOfBattle.getMenuOfActions().getAttack().getActionListeners()[0]); windowOfBattle.getMenuOfActions().getConsole().removeActionListener(windowOfBattle.getMenuOfActions().getConsole().getActionListeners()[0]); windowOfBattle.getMenuOfActions().getEscape().removeActionListener(windowOfBattle.getMenuOfActions().getEscape().getActionListeners()[0]); windowOfBattle.getMenuOfActions().getInventory().removeActionListener(windowOfBattle.getMenuOfActions().getInventory().getActionListeners()[0]); if (windowOfFreeMove.getMapManager() != null) { InteractionCell ic; if (b) ic = new InteractionCell(-1, -1, windowOfBattle.getWinCutscene(), new Dialog(windowOfFreeMove.getDialogBar()), windowOfFreeMove.getMapManager().getActualMap(), windowOfFreeMove, true); else ic = new InteractionCell(-1, -1, windowOfBattle.getLoseCutscene(), new Dialog(windowOfFreeMove.getDialogBar()), windowOfFreeMove.getMapManager().getActualMap(), windowOfFreeMove, true); ic.startEvent(); windowOfFreeMove.getForegroundPanel().setColorForeground(0, 0, 0, 255); windowOfFreeMove.setVisible(true); windowOfFreeMove.requestFocus(); windowOfBattle.getGame().remove(windowOfBattle); windowOfBattle.getMusicManager().stopMusic(); } // SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager(); // saveManager.saveGame(); } private void returnControlToPlayer(){ timer.stop(); timerStopped = true; windowOfBattle.getMenuOfActions().setVisible(true); for (Component e: windowOfBattle.getMenuOfActions().getComponents()) e.setEnabled(true); windowOfBattle.getEnemyDescription().setVisible(true); } private void attackByHero(Enemy e) { turnCount++; windowOfBattle.repaint(); turnBelongTo.attack(e); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); //TODO Animation of Attack //TODO Animation of Death if (e.health <= 0) enemies.remove(e); turnOrder.add(turnBelongTo); playerAttack = false; turnBelongTo = null; delay = 2; } private void nextTurn() { windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false); windowOfBattle.repaint(); LinkedList turns = new LinkedList<>(enemies); turns.addAll(heroes); if (checkIfTurnOrderIsFull(turns)) turnOrder = new LinkedList<>(); Collections.shuffle(turns); turnBelongTo = turns.getFirst(); while (turnOrder.contains(turnBelongTo)){ Collections.shuffle(turns); turnBelongTo = turns.getFirst(); } System.out.println(turnBelongTo.getClass().getName()); } private void attackByMonster(Hero e) { turnCount++; turnBelongTo.attack(e); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); //TODO Animation of Attack //TODO Animation of Death if (e.health <= 0) heroes.remove(e); turnOrder.add(turnBelongTo); delay = 2; turnBelongTo = null; } private Enemy randomEnemy(){ LinkedList list = getEnemies(); Random rand = new Random(); return (Enemy) list.get(rand.nextInt(list.size())); } private Hero randomHero(){ LinkedList list = getHeroes(); Random rand = new Random(); return (Hero) list.get(rand.nextInt(list.size())); } public Timer getTimer() { return timer; } public boolean isPlayerAttack() { return playerAttack; } private boolean checkIfTurnOrderIsFull(LinkedList allBattlePerson){ for(BattlePerson battlePerson: allBattlePerson){ if (!turnOrder.contains(battlePerson)) return false; } return true; } }