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 ruscz.game.gameOfProd.Windows.WindowOfBattle;
import javax.swing.*;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Random; import java.util.Random;
public class Battle { public class Battle {
private final String NAME_OF_CLASS_ENEMY = "ruscz.game.gameOfProd.Battle.Enemy";
private LinkedList<Enemy> enemies; private LinkedList<Enemy> enemies;
private LinkedList<Hero> heroes; private LinkedList<Hero> heroes;
private WindowOfBattle windowOfBattle; private WindowOfBattle windowOfBattle;
private Timer timer;
private BattlePerson turnBelongTo;
private BattlePerson turnWas;
private int turnCount; private int turnCount;
private boolean battleEnded; private int delay;
private BattlePerson turnBelongTo; private boolean playerAttack;
public BattlePerson getTurnBelongTo() { public BattlePerson getTurnBelongTo() {
return turnBelongTo; return turnBelongTo;
} }
public void setPlayerAttack(boolean playerAttack) {
this.playerAttack = playerAttack;
}
public Battle(LinkedList<Hero> heroes, LinkedList<Enemy> enemies, WindowOfBattle window) { public Battle(LinkedList<Hero> heroes, LinkedList<Enemy> enemies, WindowOfBattle window) {
this.heroes = heroes; this.heroes = heroes;
this.enemies = enemies; this.enemies = enemies;
this.windowOfBattle = window; this.windowOfBattle = window;
battleEnded = false;
} }
public LinkedList<Enemy> getEnemies() { public LinkedList<Enemy> getEnemies() {
@ -42,38 +47,53 @@ public class Battle {
windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true);
windowOfBattle.repaint(); 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++; turnCount++;
windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getMenuOfActions().setVisible(false);
windowOfBattle.getEnemyDescription().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false);
windowOfBattle.repaint(); windowOfBattle.repaint();
e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength()); e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength());
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName());
nextTurn(); playerAttack = false;
turnBelongTo = null;
delay = 2;
} }
private void nextTurn() { private void nextTurn() {
windowOfBattle.getMenuOfActions().setVisible(false); windowOfBattle.getMenuOfActions().setVisible(false);
windowOfBattle.getEnemyDescription().setVisible(false); windowOfBattle.getEnemyDescription().setVisible(false);
windowOfBattle.repaint(); windowOfBattle.repaint();
LinkedList<BattlePerson> turns = new LinkedList<>(enemies); LinkedList<BattlePerson> turns = new LinkedList<>(enemies);
turns.addAll(heroes); turns.addAll(heroes);
Collections.shuffle(turns); Collections.shuffle(turns);
if (turns.getFirst() != turnBelongTo) {
turnBelongTo = turns.getFirst(); turnBelongTo = turns.getFirst();
} else {
Collections.shuffle(turns);
}
System.out.println(turnBelongTo.getClass().getName()); 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() { private void attackByMonster() {
@ -83,6 +103,21 @@ public class Battle {
Hero e = (Hero) list.get(rand.nextInt(list.size())); Hero e = (Hero) list.get(rand.nextInt(list.size()));
e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength()); e.setHealth(e.getHealth() - turnBelongTo.getLevel() * turnBelongTo.getStrength());
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); 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 addMana(int mana);
public abstract void addDeffence(int def); public abstract void addDefence(int def);
public abstract void addStrength(int str); public abstract void addStrength(int str);

View File

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

View File

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

View File

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

View File

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

View File

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