diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java index c596a33..d233459 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java @@ -1,4 +1,88 @@ 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(); + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/BattlePerson.java b/src/main/java/ruscz/game/gameOfProd/Battle/BattlePerson.java index b5de7fa..0081048 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/BattlePerson.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/BattlePerson.java @@ -1,4 +1,43 @@ package ruscz.game.gameOfProd.Battle; +import ruscz.game.gameOfProd.Windows.WindowOfBattle; + +import java.awt.*; + public class BattlePerson { + private int health; + private int level; + private String name; + private int strength; + + public BattlePerson(int hp, int lvl, String name, int str){ + health = hp; + level = lvl; + this.name = name; + strength = str; + } + + public void paint(Graphics g, int x, int y) { + g.fillOval(x,y, WindowOfBattle.CELL_SIZE, WindowOfBattle.CELL_SIZE); + } + + public int getHealth() { + return health; + } + + public void setHealth(int health) { + this.health = health; + } + + public int getLevel() { + return level; + } + + public String getName() { + return name; + } + + public int getStrength() { + return strength; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/Enemy.java b/src/main/java/ruscz/game/gameOfProd/Battle/Enemy.java index 4f3895f..449419e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/Enemy.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/Enemy.java @@ -1,4 +1,14 @@ package ruscz.game.gameOfProd.Battle; public class Enemy extends BattlePerson { + private int id; + + public Enemy(int hp, int lvl, String name, int str, int id) { + super(hp, lvl, name, str); + this.id = id; + } + + public int getId() { + return id; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/Hero.java b/src/main/java/ruscz/game/gameOfProd/Battle/Hero.java index 5a295e4..05d1d8d 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/Hero.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/Hero.java @@ -1,4 +1,10 @@ package ruscz.game.gameOfProd.Battle; public class Hero extends BattlePerson { + private int mana; + + public Hero(int hp, int lvl, String name, int str, int mana){ + super(hp, lvl, name, str); + this.mana = mana; + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Game.java b/src/main/java/ruscz/game/gameOfProd/Game.java index 7860b89..1e824d0 100644 --- a/src/main/java/ruscz/game/gameOfProd/Game.java +++ b/src/main/java/ruscz/game/gameOfProd/Game.java @@ -37,8 +37,10 @@ public class Game extends JFrame { menu = new WindowOfMenu(this); battle = new WindowOfBattle(this); + add(battle); + battle.startBattle(); //tohle spustí na začátku menu - add(menu); +// add(menu); //tohle spustí na začátku hru // add(freeMove); diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java index f809d3f..3db6ce8 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java @@ -1,23 +1,200 @@ package ruscz.game.gameOfProd.Windows; +import ruscz.game.gameOfProd.Battle.*; 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; -public class WindowOfBattle { +public class WindowOfBattle extends JPanel { + public static final int CELL_SIZE = 50; + public static int CANVAS_WIDTH = 16; + public static int CANVAS_HEIGHT = 13; + + private static final int KEY_ESCAPE = 27; + + private static boolean isReadyToPaint; + + public JPanel getSituationText() { + return situationText; + } + + public JPanel getEnemyDescription() { + return enemyDescription; + } + + public JPanel getMenuOfActions() { + return menuOfActions; + } + + public JPanel getConsoleInput() { + return consoleInput; + } + + public JPanel getCommandList() { + return commandList; + } + + public JPanel getItemsInventory() { + return itemsInventory; + } + private Canvas canvas; + private JPanel situationText; + private JPanel enemyDescription; + private JPanel menuOfActions; + private JPanel consoleInput; + private JPanel commandList; + private JPanel itemsInventory; + + private Game game; + private Battle battle; public WindowOfBattle(Game game) { - canvas = new Canvas(); + this.game = game; + isReadyToPaint = false; + JLayeredPane lp = new JLayeredPane(); + lp.setPreferredSize(new Dimension( + CELL_SIZE * CANVAS_WIDTH, + CELL_SIZE * CANVAS_HEIGHT)); + lp.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT); + + setBackground(Color.BLACK); + + canvas = new Canvas(); + canvas.setPreferredSize(new Dimension( + CELL_SIZE * CANVAS_WIDTH, + CELL_SIZE * CANVAS_HEIGHT)); + canvas.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT); + canvas.setBackground(Color.RED); + + situationText = new JPanel(); + situationText.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT / 4); + situationText.setLocation(0, CELL_SIZE * CANVAS_HEIGHT * 3 / 4); + situationText.setBackground(Color.BLUE); + + enemyDescription = new JPanel(); + enemyDescription.setSize(CELL_SIZE * CANVAS_WIDTH * 2 / 3, CELL_SIZE * CANVAS_HEIGHT / 4); + enemyDescription.setLocation(0, CELL_SIZE * CANVAS_HEIGHT * 3 / 4); + enemyDescription.setBackground(Color.cyan); + enemyDescription.setVisible(false); + + menuOfActions = new JPanel(); + menuOfActions.setSize(CELL_SIZE * CANVAS_WIDTH / 3, CELL_SIZE * CANVAS_HEIGHT / 4); + menuOfActions.setLocation(CELL_SIZE * CANVAS_WIDTH * 2 / 3, CELL_SIZE * CANVAS_HEIGHT * 3 / 4); + menuOfActions.setBackground(Color.green); + 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()))); + }); + attack.setLayout(null); + menuOfActions.add(attack); + + consoleInput = new JPanel(); + consoleInput.setSize(CELL_SIZE * (CANVAS_WIDTH - 4), CELL_SIZE); + consoleInput.setLocation(CELL_SIZE * 4, CELL_SIZE * CANVAS_HEIGHT * 3 / 4 - CELL_SIZE); + consoleInput.setVisible(false); + + commandList = new JPanel(); + commandList.setSize(CELL_SIZE * 8, CELL_SIZE * 6); + commandList.setLocation(CELL_SIZE * 4, CELL_SIZE * 2); + commandList.setVisible(false); + + itemsInventory = new JPanel(); + itemsInventory.setSize(CELL_SIZE * 8, CELL_SIZE * 6); + itemsInventory.setLocation(CELL_SIZE * 4, CELL_SIZE * 2); + itemsInventory.setBackground(Color.yellow); + itemsInventory.setVisible(false); + + add(lp); + lp.add(canvas, JLayeredPane.DEFAULT_LAYER); + lp.add(situationText, JLayeredPane.PALETTE_LAYER); + lp.add(enemyDescription, JLayeredPane.MODAL_LAYER); + lp.add(menuOfActions, JLayeredPane.MODAL_LAYER); + lp.add(consoleInput, JLayeredPane.PALETTE_LAYER); + lp.add(commandList, JLayeredPane.PALETTE_LAYER); + lp.add(itemsInventory, JLayeredPane.MODAL_LAYER); + + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KEY_ESCAPE) { + game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING)); + } + } + }); + } + + public void startBattle(){ + Hero hero = new Hero(100, 1, "Mark", 1, 100); + Enemy enemy1 = new Enemy(10, 1, "123", 1, 1); + Enemy enemy2 = new Enemy(10, 1, "test", 1, 2); + + LinkedList a = new LinkedList<>(); + a.add(hero); + + LinkedList b = new LinkedList<>(); + b.add(enemy1); + b.add(enemy2); + + battle = new Battle(a, b, this); + + isReadyToPaint = true; + Thread repainting = new Thread(() -> { + Timer t = new Timer(10, actionEvent -> { + canvas.repaint(); + if (situationText.isVisible()){ + situationText.repaint(); + } + }); + t.start(); + }); + repainting.start(); + + battle.startBattle(); + } + + public Canvas getCanvas() { + return canvas; + } + + public void writeToOutput(String 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(); + } } class Canvas extends JPanel { @Override public void paint(Graphics g) { - + super.paint(g); + if (isReadyToPaint){ + for (Enemy battlePerson: battle.getEnemies()){ + battlePerson.paint(g, CELL_SIZE * 2, CELL_SIZE *3 * battlePerson.getId()); + } + for (BattlePerson battlePerson: battle.getHeroes()){ + battlePerson.paint(g, CELL_SIZE * 12, CELL_SIZE * 4); + } + } } } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index af7ac8b..cfaa059 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -93,7 +93,7 @@ public class WindowOfFreeMove extends JLayeredPane { canvas.setPreferredSize(new Dimension( CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT)); - canvas.setSize(CELL_SIZE * CANVAS_HEIGHT, CELL_SIZE * CANVAS_WIDTH); + canvas.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT); dialogBar = new DialogBar(); dialogBar.setBounds(0, CELL_SIZE * WINDOW_HEIGHT * 3/4, CELL_SIZE * WINDOW_WIDTH, CELL_SIZE * WINDOW_HEIGHT / 4); @@ -115,7 +115,6 @@ public class WindowOfFreeMove extends JLayeredPane { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KEY_ESCAPE){ game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING)); - return; } else { if (canPersonMove) {