End of Battle (works!)

This commit is contained in:
iryupnik 2020-05-10 20:13:02 +02:00
parent 5167562025
commit a8d51bfb88
9 changed files with 151 additions and 24 deletions

View File

@ -13,7 +13,6 @@ public class Battle {
private WindowOfBattle windowOfBattle; private WindowOfBattle windowOfBattle;
private Timer timer; private Timer timer;
private BattlePerson turnBelongTo; private BattlePerson turnBelongTo;
private BattlePerson turnWas;
private int turnCount; private int turnCount;
private int delay; private int delay;
@ -42,12 +41,23 @@ public class Battle {
return heroes; return heroes;
} }
private boolean checkHealthOfEnemies(){
return enemies.isEmpty();
}
private boolean checkHealthOfHeroes(){
return heroes.isEmpty();
}
public void startBattle() { public void startBattle() {
turnBelongTo = heroes.getFirst(); turnBelongTo = heroes.getFirst();
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 -> { timer = new Timer(1000, actionEvent -> {
for (Enemy e: enemies) System.out.println(e.health);
if (checkHealthOfEnemies()) endBattle(true);
if (checkHealthOfHeroes()) endBattle(false);
if(playerAttack) if(playerAttack)
attackByHero(randomEnemy()); attackByHero(randomEnemy());
else if (delay != 0) else if (delay != 0)
@ -55,16 +65,24 @@ public class Battle {
else if (turnBelongTo == null) else if (turnBelongTo == null)
nextTurn(); nextTurn();
else if (turnBelongTo.getClass().getName().equals(Enemy.class.getName())) else if (turnBelongTo.getClass().getName().equals(Enemy.class.getName()))
attackByMonster(); attackByMonster(randomHero());
else { else {
returnControlToPlayer(); returnControlToPlayer();
} }
}); });
} }
private void endBattle(boolean b) {
//TODO Return to Map
timer.stop();
if (b) windowOfBattle.writeToOutput("Win!");
else windowOfBattle.writeToOutput("Lose!");
}
private void returnControlToPlayer(){ private void returnControlToPlayer(){
timer.stop(); timer.stop();
windowOfBattle.getMenuOfActions().setVisible(true); windowOfBattle.getMenuOfActions().setVisible(true);
windowOfBattle.getMenuOfActions().getComponents()[0].setEnabled(true);
windowOfBattle.getEnemyDescription().setVisible(true); windowOfBattle.getEnemyDescription().setVisible(true);
} }
@ -73,11 +91,12 @@ public class Battle {
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()); turnBelongTo.attack(e);
//TODO Animation of Death
if (e.health <= 0) enemies.remove(e);
windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName()); windowOfBattle.writeToOutput(turnBelongTo.getName() + " deal " + turnBelongTo.getLevel() * turnBelongTo.getStrength() + " to " + e.getName());
playerAttack = false; playerAttack = false;
turnBelongTo = null; turnBelongTo = null;
delay = 2; delay = 2;
} }
@ -89,19 +108,19 @@ public class Battle {
LinkedList<BattlePerson> turns = new LinkedList<>(enemies); LinkedList<BattlePerson> turns = new LinkedList<>(enemies);
turns.addAll(heroes); turns.addAll(heroes);
//TODO Check turns (maybe List)
Collections.shuffle(turns); Collections.shuffle(turns);
turnBelongTo = turns.getFirst(); turnBelongTo = turns.getFirst();
System.out.println(turnBelongTo.getClass().getName()); System.out.println(turnBelongTo.getClass().getName());
} }
private void attackByMonster() { private void attackByMonster(Hero e) {
turnCount++; turnCount++;
LinkedList list = getHeroes(); turnBelongTo.attack(e);
Random rand = new Random(); //TODO Animation of Death
Hero e = (Hero) list.get(rand.nextInt(list.size())); if (e.health <= 0) heroes.remove(e);
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());
delay = 2; delay = 2;
turnBelongTo = null; turnBelongTo = null;
@ -113,6 +132,12 @@ public class Battle {
return (Enemy) list.get(rand.nextInt(list.size())); 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() { public Timer getTimer() {
return timer; return timer;
} }

View File

@ -19,7 +19,7 @@ public class Hero extends BattlePerson {
@Override @Override
public void attack(BattlePerson enemy) { public void attack(BattlePerson enemy) {
enemy.setHealth(enemy.getHealth() - (strength)); enemy.setHealth(enemy.getHealth() - (strength * level));
} }
@Override @Override

View File

@ -41,18 +41,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
new EditorManager(); // new EditorManager();
setDefaultCloseOperation(EXIT_ON_CLOSE); setDefaultCloseOperation(EXIT_ON_CLOSE);
setUndecorated(true); setUndecorated(true);
@ -64,6 +64,8 @@ public class Game extends JFrame {
setExtendedState(MAXIMIZED_BOTH); setExtendedState(MAXIMIZED_BOTH);
} }
public Game game() {return this;} public Game game() {
return this;
}
} }

View File

@ -11,7 +11,7 @@ public class InfoManager {
private JScrollPane scrollInfo; private JScrollPane scrollInfo;
private JLayeredPane view; private JLayeredPane view;
LinkedList<JRadioButton> radioButtons; private LinkedList<JRadioButton> radioButtons;
public InfoManager(LevelEditor editor) { public InfoManager(LevelEditor editor) {
this.editor = editor; this.editor = editor;

View File

@ -2,6 +2,8 @@ package ruscz.game.gameOfProd.Windows;
import ruscz.game.gameOfProd.Battle.*; import ruscz.game.gameOfProd.Battle.*;
import ruscz.game.gameOfProd.Game; import ruscz.game.gameOfProd.Game;
import ruscz.game.gameOfProd.Save.LoadManager;
import ruscz.game.gameOfProd.Save.SaveManager;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -53,6 +55,8 @@ public class WindowOfBattle extends JPanel {
private Game game; private Game game;
private Battle battle; private Battle battle;
private LoadManager loadManager;
private SaveManager saveManager;
public WindowOfBattle(Game game) { public WindowOfBattle(Game game) {
this.game = game; this.game = game;
@ -91,11 +95,26 @@ 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 -> {
attack.setEnabled(false);
battle.setPlayerAttack(true); battle.setPlayerAttack(true);
battle.getTimer().start(); battle.getTimer().start();
}); });
attack.setLayout(null); JButton console = new JButton("Console");
console.addActionListener(e -> {
});
JButton inventory = new JButton("Items");
inventory.addActionListener(e -> {
});
JButton escape = new JButton("Run Away");
escape.addActionListener(e -> {
});
menuOfActions.add(attack); menuOfActions.add(attack);
menuOfActions.add(console);
menuOfActions.add(inventory);
menuOfActions.add(escape);
consoleInput = new JPanel(); consoleInput = new JPanel();
consoleInput.setSize(CELL_SIZE * (CANVAS_WIDTH - 4), CELL_SIZE); consoleInput.setSize(CELL_SIZE * (CANVAS_WIDTH - 4), CELL_SIZE);
@ -133,7 +152,10 @@ public class WindowOfBattle extends JPanel {
} }
public void startBattle() { public void startBattle() {
Hero hero = new Hero(100, 1, "Mark", 1, 100, 1); //saveManager.saveGame();
//TODO Enemies from Files
Hero hero = new Hero(100, 100, "Mark", 1, 100, 1);
Enemy enemy1 = new Enemy(10, 1, "123", 1, 1); Enemy enemy1 = new Enemy(10, 1, "123", 1, 1);
Enemy enemy2 = new Enemy(10, 1, "test", 1, 2); Enemy enemy2 = new Enemy(10, 1, "test", 1, 2);
@ -150,13 +172,13 @@ public class WindowOfBattle extends JPanel {
Timer t = new Timer(10, actionEvent -> { Timer t = new Timer(10, actionEvent -> {
repaint(); repaint();
canvas.repaint(); canvas.repaint();
if (situationText.isVisible()) {
situationText.repaint(); situationText.repaint();
}
}); });
t.start(); t.start();
battle.startBattle(); battle.startBattle();
//loadManager.loadSavedGame();
} }
public Canvas getCanvas() { public Canvas getCanvas() {

View File

@ -0,0 +1,65 @@
<map>
<mapName>tutorial</mapName>
<events>
<trigger>
<x>11</x>
<y>6</y>
<active>false</active>
</trigger>
</events>
</map>
<map>
<mapName>openspace</mapName>
<events>
<trigger>
<x>6</x>
<y>4</y>
<active>true</active>
</trigger>
<trigger>
<x>14</x>
<y>16</y>
<active>false</active>
</trigger>
<chest>
<x>15</x>
<y>7</y>
<active>true</active>
</chest>
</events>
</map>
<map>
<mapName>office</mapName>
<events>
<trigger>
<x>4</x>
<y>2</y>
<active>false</active>
</trigger>
<chest>
<x>1</x>
<y>9</y>
<active>false</active>
</chest>
</events>
</map>
<map>
<mapName>boss</mapName>
<events>
<trigger>
<x>4</x>
<y>18</y>
<active>true</active>
</trigger>
</events>
</map>
<map>
<mapName>treasure</mapName>
<events>
<trigger>
<x>6</x>
<y>1</y>
<active>true</active>
</trigger>
</events>
</map>

View File

@ -0,0 +1,6 @@
<item>
<name>blue-key-card</name>
<multiplicity>1</multiplicity>
<image>39</image>
<action>key</action>
</item>

View File

@ -0,0 +1,6 @@
<location>
<map>openspace</map>
<x>8</x>
<y>16</y>
<direction>3</direction>
</location>

View File

@ -0,0 +1 @@
TODO: Village