package ruscz.game.gameOfProd.Battle; import ruscz.game.gameOfProd.Windows.WindowOfBattle; import java.util.Collections; import java.util.LinkedList; import java.util.Random; public class Battle { private final String NAME_OF_CLASS_ENEMY = "ruscz.game.gameOfProd.Battle.Enemy"; private LinkedList enemies; private LinkedList heroes; private WindowOfBattle windowOfBattle; private int turnCount; private boolean battleEnded; private BattlePerson turnBelongTo; public BattlePerson getTurnBelongTo() { return turnBelongTo; } public Battle(LinkedList heroes, LinkedList enemies, WindowOfBattle window) { this.heroes = heroes; this.enemies = enemies; this.windowOfBattle = window; battleEnded = false; } public LinkedList getEnemies() { return enemies; } public LinkedList getHeroes() { return heroes; } public void startBattle() { turnBelongTo = heroes.getFirst(); windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.repaint(); } public void attackByHero(Enemy e) { turnCount++; windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false); windowOfBattle.repaint(); e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength()); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); nextTurn(); } private void nextTurn() { windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false); windowOfBattle.repaint(); LinkedList turns = new LinkedList<>(enemies); turns.addAll(heroes); Collections.shuffle(turns); if (turns.getFirst() != turnBelongTo) { turnBelongTo = turns.getFirst(); } else { Collections.shuffle(turns); } System.out.println(turnBelongTo.getClass().getName()); if (turnBelongTo.getClass().getName().equals(NAME_OF_CLASS_ENEMY)){ attackByMonster(); } else { windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true); } } private void attackByMonster() { turnCount++; LinkedList list = getHeroes(); Random rand = new Random(); Hero e = (Hero) list.get(rand.nextInt(list.size())); e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength()); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); nextTurn(); } }