battle update

This commit is contained in:
iryupnik 2020-04-27 12:54:23 +02:00
parent 4509f978f8
commit f587f1a7ab
7 changed files with 72 additions and 42 deletions

View File

@ -2,31 +2,36 @@ package ruscz.game.gameOfProd.Battle;
import ruscz.game.gameOfProd.Windows.WindowOfBattle;
import javax.swing.*;
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 Timer timer;
private BattlePerson turnBelongTo;
private BattlePerson turnWas;
private int turnCount;
private boolean battleEnded;
private int delay;
private BattlePerson turnBelongTo;
private boolean playerAttack;
public BattlePerson getTurnBelongTo() {
return turnBelongTo;
}
public void setPlayerAttack(boolean playerAttack) {
this.playerAttack = playerAttack;
}
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() {
@ -42,38 +47,53 @@ public class Battle {
windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getEnemyDescription().setVisible(true);
windowOfBattle.repaint();
timer = new Timer(1000, actionEvent -> {
if(playerAttack)
attackByHero(randomEnemy());
else if (delay != 0)
delay--;
else if (turnBelongTo == null)
nextTurn();
else if (turnBelongTo.getClass().getName().equals(Enemy.class.getName()))
attackByMonster();
else {
returnControlToPlayer();
}
});
}
public void attackByHero(Enemy e) {
private void returnControlToPlayer(){
timer.stop();
windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getEnemyDescription().setVisible(true);
}
private 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();
playerAttack = false;
turnBelongTo = null;
delay = 2;
}
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() {
@ -83,6 +103,21 @@ public class Battle {
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();
delay = 2;
turnBelongTo = null;
}
private Enemy randomEnemy(){
LinkedList list = getEnemies();
Random rand = new Random();
return (Enemy) list.get(rand.nextInt(list.size()));
}
public Timer getTimer() {
return timer;
}
public boolean isPlayerAttack() {
return playerAttack;
}
}

View File

@ -25,7 +25,7 @@ public abstract class BattlePerson {
public abstract void addMana(int mana);
public abstract void addDeffence(int def);
public abstract void addDefence(int def);
public abstract void addStrength(int str);

View File

@ -31,7 +31,7 @@ public class Enemy extends BattlePerson {
}
@Override
public void addDeffence(int def) {
public void addDefence(int def) {
}
@Override

View File

@ -2,13 +2,13 @@ package ruscz.game.gameOfProd.Battle;
public class Hero extends BattlePerson {
private int mana;
private int deffence;
private int defence;
private boolean isBlocking;
public Hero(int hp, int lvl, String name, int str, int mana, int deffence){
public Hero(int hp, int lvl, String name, int str, int mana, int defence){
super(hp, lvl, name, str);
this.mana = mana;
this.deffence = deffence;
this.defence = defence;
isBlocking = false;
}
@ -33,8 +33,8 @@ public class Hero extends BattlePerson {
}
@Override
public void addDeffence(int def) {
deffence += def;
public void addDefence(int def) {
defence += def;
}
@Override

View File

@ -40,18 +40,18 @@ public class Game extends JFrame {
battle = new WindowOfBattle(this);
//tohle spustí na začátku menu
add(menu);
// add(menu);
//tohle spustí na začátku hru
// add(freeMove);
// freeMove.start(true);
//tohle spustí na začátku bitvu
// add(battle);
// battle.startBattle();
add(battle);
battle.startBattle();
//Tohle spustí na začátku tileset overview
tilesetOverview = new TilesetOverview();
// tilesetOverview = new TilesetOverview();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true);

View File

@ -53,7 +53,7 @@ public class TextParser {
person.heal(item.getMultiplicity());
break;
case DEFENCE:
person.addDeffence(item.getMultiplicity());
person.addDefence(item.getMultiplicity());
break;
case STRENGTH:
person.addStrength(item.getMultiplicity());

View File

@ -6,7 +6,6 @@ import ruscz.game.gameOfProd.Game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
@ -92,9 +91,8 @@ public class WindowOfBattle extends JPanel {
menuOfActions.setVisible(false);
JButton attack = new JButton("Attack");
attack.addActionListener(e -> {
LinkedList list = battle.getEnemies();
Random rand = new Random();
battle.attackByHero((Enemy) list.get(rand.nextInt(list.size())));
battle.setPlayerAttack(true);
battle.getTimer().start();
});
attack.setLayout(null);
menuOfActions.add(attack);
@ -168,19 +166,16 @@ public class WindowOfBattle extends JPanel {
}
public void writeToOutput(String s) {
System.out.println(s);
Font f = new Font(Font.SERIF, Font.BOLD, 20);
if (situationText.getComponents().length != 0)
situationText.remove(0);
JLabel text = new JLabel();
text.setText(s);
text.setFont(f);
text.setLocation(0, 0);
situationText.add(text);
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
situationText.getComponents()[0].repaint();
repaint();
}
class Canvas extends JPanel {