2020-04-20 12:29:51 +02:00

89 lines
2.9 KiB
Java

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<Enemy> enemies;
private LinkedList<Hero> heroes;
private WindowOfBattle windowOfBattle;
private int turnCount;
private boolean battleEnded;
private BattlePerson turnBelongTo;
public BattlePerson getTurnBelongTo() {
return turnBelongTo;
}
public Battle(LinkedList<Hero> heroes, LinkedList<Enemy> enemies, WindowOfBattle window) {
this.heroes = heroes;
this.enemies = enemies;
this.windowOfBattle = window;
battleEnded = false;
}
public LinkedList<Enemy> getEnemies() {
return enemies;
}
public LinkedList<Hero> 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<BattlePerson> 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();
}
}