From 325a929c3fffb47e1613e34efbbea0a088fab57b Mon Sep 17 00:00:00 2001 From: iryupnik Date: Sat, 6 Jun 2020 14:01:17 +0200 Subject: [PATCH] Battle continues..... --- .../ruscz/game/gameOfProd/Battle/Battle.java | 13 +++- .../game/gameOfProd/BattleUI/CommandList.java | 11 +++ .../gameOfProd/BattleUI/ConsoleInput.java | 6 +- .../gameOfProd/BattleUI/ItemsInventory.java | 70 +++++++++++++++++++ .../gameOfProd/BattleUI/MenuOfActions.java | 1 + .../java/ruscz/game/gameOfProd/Cells/Npc.java | 14 +--- src/main/java/ruscz/game/gameOfProd/Game.java | 2 +- .../ruscz/game/gameOfProd/Inventory/Item.java | 8 ++- .../game/gameOfProd/Inventory/ItemPanel.java | 21 ++++++ .../gameOfProd/Windows/WindowOfBattle.java | 45 +++++++----- .../resources/battles/storageroombattle.txt | 2 +- src/main/resources/save/savedGame/events.txt | 2 +- .../resources/save/savedGame/inventory.txt | 6 ++ src/main/resources/save/savedGame/person.txt | 6 +- 14 files changed, 163 insertions(+), 44 deletions(-) create mode 100644 src/main/java/ruscz/game/gameOfProd/Inventory/ItemPanel.java diff --git a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java index 72d06bd..7351bd8 100644 --- a/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java +++ b/src/main/java/ruscz/game/gameOfProd/Battle/Battle.java @@ -9,10 +9,15 @@ import java.util.LinkedList; import java.util.Random; public class Battle { - private LinkedList enemies; - private LinkedList heroes; - private WindowOfBattle windowOfBattle; + private final LinkedList enemies; + private final LinkedList heroes; + private final WindowOfBattle windowOfBattle; private Timer timer; + + public void setTurnBelongTo(BattlePerson turnBelongTo) { + this.turnBelongTo = turnBelongTo; + } + private BattlePerson turnBelongTo; private LinkedList turnOrder; @@ -22,6 +27,8 @@ public class Battle { private boolean playerAttack; + public void setDelay(int delay) { this.delay = delay; } + public boolean isTimerStopped() { return timerStopped; } diff --git a/src/main/java/ruscz/game/gameOfProd/BattleUI/CommandList.java b/src/main/java/ruscz/game/gameOfProd/BattleUI/CommandList.java index 583931b..6df4068 100644 --- a/src/main/java/ruscz/game/gameOfProd/BattleUI/CommandList.java +++ b/src/main/java/ruscz/game/gameOfProd/BattleUI/CommandList.java @@ -4,6 +4,8 @@ import ruscz.game.gameOfProd.Windows.WindowOfBattle; import javax.swing.*; +import java.awt.*; + import static ruscz.game.gameOfProd.Windows.WindowOfBattle.CELL_SIZE; public class CommandList extends JPanel { @@ -11,5 +13,14 @@ public class CommandList extends JPanel { setSize(CELL_SIZE * 8, CELL_SIZE * 6); setLocation(CELL_SIZE * 4, CELL_SIZE * 2); setVisible(false); + + JLabel text = new JLabel("It's not a situation to use command line, idiot!"); + JButton jButton = new JButton("Hide this field"); + jButton.addActionListener(e -> { + this.setVisible(false); + }); + + add(text); + add(jButton); } } diff --git a/src/main/java/ruscz/game/gameOfProd/BattleUI/ConsoleInput.java b/src/main/java/ruscz/game/gameOfProd/BattleUI/ConsoleInput.java index b62c4e9..3ee6326 100644 --- a/src/main/java/ruscz/game/gameOfProd/BattleUI/ConsoleInput.java +++ b/src/main/java/ruscz/game/gameOfProd/BattleUI/ConsoleInput.java @@ -31,9 +31,9 @@ public class ConsoleInput extends JPanel { }); list.addActionListener(e -> { - submit.setEnabled(false); - list.setEnabled(false); - exit.setEnabled(false); +// submit.setEnabled(false); +// list.setEnabled(false); +// exit.setEnabled(false); window.getCommandList().setVisible(true); }); diff --git a/src/main/java/ruscz/game/gameOfProd/BattleUI/ItemsInventory.java b/src/main/java/ruscz/game/gameOfProd/BattleUI/ItemsInventory.java index 3554a6f..ffa531a 100644 --- a/src/main/java/ruscz/game/gameOfProd/BattleUI/ItemsInventory.java +++ b/src/main/java/ruscz/game/gameOfProd/BattleUI/ItemsInventory.java @@ -1,5 +1,10 @@ package ruscz.game.gameOfProd.BattleUI; +import ruscz.game.gameOfProd.Image.ImageCell; +import ruscz.game.gameOfProd.Inventory.InventoryManager; +import ruscz.game.gameOfProd.Inventory.Item; +import ruscz.game.gameOfProd.Inventory.ItemAction; +import ruscz.game.gameOfProd.Inventory.ItemPanel; import ruscz.game.gameOfProd.Windows.WindowOfBattle; import javax.swing.*; @@ -8,10 +13,75 @@ import java.awt.*; import static ruscz.game.gameOfProd.Windows.WindowOfBattle.CELL_SIZE; public class ItemsInventory extends JPanel { + private final JPanel items; + private final ImageCell imageCell; + private final WindowOfBattle windowOfBattle; + public ItemsInventory(WindowOfBattle window){ + imageCell = new ImageCell(); + imageCell.imageItem("TransparentIcons.png"); + windowOfBattle = window; + setSize(CELL_SIZE * 8, CELL_SIZE * 6); setLocation(CELL_SIZE * 4, CELL_SIZE * 2); setBackground(Color.yellow); setVisible(false); + + items = new JPanel(); + + JButton returnToMenu = new JButton("x"); + returnToMenu.addActionListener(e -> { + window.getMenuOfActions().getAttack().setEnabled(true); + window.getMenuOfActions().getConsole().setEnabled(true); + window.getMenuOfActions().getInventory().setEnabled(true); + window.getMenuOfActions().getEscape().setEnabled(true); + + setVisible(false); + }); + add(items); + add(returnToMenu); } + + public void refreshItemsInField() { + for (Component c: items.getComponents()) + items.remove(c); + + System.out.println(InventoryManager.getInventory()); + + for (Item item: InventoryManager.getInventory()){ + JPanel jPanel = new JPanel(); + jPanel.setLayout(new GridLayout(1, 5)); + jPanel.setSize(CELL_SIZE * 8, 32); + ItemPanel itemPanel = new ItemPanel(imageCell, item.getImage()); + itemPanel.setSize(32, 32); + + jPanel.add(itemPanel); + jPanel.add(new JLabel(item.getName())); + jPanel.add(new JLabel(String.valueOf(item.getMultiplicity()))); + jPanel.add(new JLabel(String.valueOf(item.getItemAction()))); + + if (item.getItemAction().equals(ItemAction.HEAL)){ + JButton use = new JButton("Use"); + use.addActionListener(e -> { + setVisible(false); + windowOfBattle.getBattle().getHeroes().getFirst().heal(100); + windowOfBattle.getMenuOfActions().setVisible(false); + windowOfBattle.getEnemyDescription().setVisible(false); + windowOfBattle.writeToOutput(windowOfBattle.getBattle().getHeroes().getFirst().getName() + " heals 100 HP"); + InventoryManager.removeItem(item, 1); + windowOfBattle.getBattle().setTurnBelongTo(null); + windowOfBattle.getBattle().setDelay(2); + windowOfBattle.getBattle().getTimer().start(); + }); + jPanel.add(use); + } + + items.add(jPanel); + } + + items.setVisible(true); + if (items.getComponents().length == 0) items.setVisible(false); + } + + } diff --git a/src/main/java/ruscz/game/gameOfProd/BattleUI/MenuOfActions.java b/src/main/java/ruscz/game/gameOfProd/BattleUI/MenuOfActions.java index b3e827e..efccb6a 100644 --- a/src/main/java/ruscz/game/gameOfProd/BattleUI/MenuOfActions.java +++ b/src/main/java/ruscz/game/gameOfProd/BattleUI/MenuOfActions.java @@ -51,6 +51,7 @@ public class MenuOfActions extends JPanel { inventory.setEnabled(false); escape.setEnabled(false); + window.getItemsInventory().refreshItemsInField(); window.getItemsInventory().setVisible(true); }); diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 2bb0491..8d20419 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -16,7 +16,7 @@ import java.util.LinkedList; import java.util.Scanner; public class Npc extends Person { - private MapManager mapManager; + private final MapManager mapManager; private String name; private int direction; private int width; @@ -36,14 +36,6 @@ public class Npc extends Person { loadNpc(); } - public Npc(int x, int y, String name, String battle, Map map, MapManager mapManager) { - super(x, y, map); - this.name = name; - this.battle = battle; - this.mapManager = mapManager; - loadNpc(); - } - public Npc(int x, int y, Map map, MapManager mapManager) { super(x, y, map); this.mapManager = mapManager; @@ -57,7 +49,7 @@ public class Npc extends Person { } final int[] i = {0}; timer = new Timer(500, actionEvent -> { - if (i[0] == directions.size()) stopTimer(endX, endY); + if (i[0] == directions.size()) stopNPCMove(endX, endY); else { switch (directions.get(i[0])) { case UP: @@ -83,7 +75,7 @@ public class Npc extends Person { timer.start(); } - private void stopTimer(int endX, int endY) { + private void stopNPCMove(int endX, int endY) { timer.stop(); if (battle != null && endX == mapManager.getActualMap().getPerson().getX() && endY == mapManager.getActualMap().getPerson().getY()) { mapManager.getWindow().startBattle(battle); diff --git a/src/main/java/ruscz/game/gameOfProd/Game.java b/src/main/java/ruscz/game/gameOfProd/Game.java index 82d8a34..7e1effe 100644 --- a/src/main/java/ruscz/game/gameOfProd/Game.java +++ b/src/main/java/ruscz/game/gameOfProd/Game.java @@ -48,7 +48,7 @@ public class Game extends JFrame { //tohle spustí na začátku bitvu // add(battle); -// battle.startBattle(); +// battle.startBattle("storageroombattle"); //Tohle spustí na začátku tileset overview //new LevelEditor(); diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java index 0d27cc8..3739435 100644 --- a/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/Item.java @@ -1,12 +1,16 @@ package ruscz.game.gameOfProd.Inventory; +import ruscz.game.gameOfProd.Image.ImageCell; + +import javax.swing.*; import java.util.Objects; public class Item { private ItemAction itemAction; - private String name; + private final String name; private int multiplicity; - private int image; + private final int image; + public Item(String name, int multiplicity, int image, String itemAction) { this.name = parseName(name); diff --git a/src/main/java/ruscz/game/gameOfProd/Inventory/ItemPanel.java b/src/main/java/ruscz/game/gameOfProd/Inventory/ItemPanel.java new file mode 100644 index 0000000..496289e --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Inventory/ItemPanel.java @@ -0,0 +1,21 @@ +package ruscz.game.gameOfProd.Inventory; + +import ruscz.game.gameOfProd.Image.ImageCell; + +import javax.swing.*; +import java.awt.*; + +public class ItemPanel extends JPanel { + ImageCell imageCell; + int item; + + public ItemPanel(ImageCell imageCell, int item){ + this.imageCell = imageCell; + this.item = item; + } + + @Override + public void paint(Graphics g) { + g.drawImage(imageCell.getImagesOfItems().get(item), 0, 0, 32,32, null); + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java index 060e9d8..ce0fa0e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfBattle.java @@ -6,6 +6,9 @@ import ruscz.game.gameOfProd.Battle.Enemy; import ruscz.game.gameOfProd.Battle.Hero; import ruscz.game.gameOfProd.BattleUI.*; import ruscz.game.gameOfProd.Game; +import ruscz.game.gameOfProd.Image.ImageCell; +import ruscz.game.gameOfProd.Inventory.InventoryManager; +import ruscz.game.gameOfProd.Inventory.Item; import ruscz.game.gameOfProd.Save.SaveManager; import ruscz.game.gameOfProd.Sounds.MusicManager; @@ -21,7 +24,7 @@ import java.util.Scanner; public class WindowOfBattle extends JPanel { - public static final int CELL_SIZE = 50; + public static final int CELL_SIZE = 60; public static int CANVAS_WIDTH = 16; public static int CANVAS_HEIGHT = 13; @@ -52,15 +55,15 @@ public class WindowOfBattle extends JPanel { return itemsInventory; } - private Canvas canvas; - private SituationText situationText; - private EnemyDescription enemyDescription; - private MenuOfActions menuOfActions; - private ConsoleInput consoleInput; - private CommandList commandList; - private ItemsInventory itemsInventory; + private final Canvas canvas; + private final SituationText situationText; + private final EnemyDescription enemyDescription; + private final MenuOfActions menuOfActions; + private final ConsoleInput consoleInput; + private final CommandList commandList; + private final ItemsInventory itemsInventory; - private Game game; + private final Game game; private Battle battle; private MusicManager musicManager; @@ -80,6 +83,7 @@ public class WindowOfBattle extends JPanel { lp.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT); setBackground(Color.BLACK); + //setLayout(new GridBagLayout()); canvas = new Canvas(); situationText = new SituationText(this); @@ -109,8 +113,10 @@ public class WindowOfBattle extends JPanel { } public void startBattle(String name) { - SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager(); - saveManager.saveGame(); + if (game.getFreeMove() != null) { + SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager(); + saveManager.saveGame(); + } LinkedList heroes = new LinkedList<>(); LinkedList enemies = new LinkedList<>(); @@ -118,19 +124,17 @@ public class WindowOfBattle extends JPanel { try { File battleFile = new File("src/main/resources/battles/" + name + ".txt"); Scanner scanner = new Scanner(battleFile); - while(scanner.hasNextLine()) { + while (scanner.hasNextLine()) { String[] data = scanner.nextLine().split(" "); if (data[0].equals("")) { Hero hero = new Hero(Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[3], Integer.parseInt(data[4]), Integer.parseInt(data[5]), Integer.parseInt(data[6])); heroes.add(hero); - } - else if (data[0].equals("")) { + } else if (data[0].equals("")) { Enemy enemy = new Enemy(Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[3], Integer.parseInt(data[4]), Integer.parseInt(data[5])); enemies.add(enemy); - } - else if (data[0].equals("")) { + } else if (data[0].equals("")) { musicManager = new MusicManager(data[1], Float.parseFloat(data[2])); } } @@ -138,7 +142,6 @@ public class WindowOfBattle extends JPanel { e.printStackTrace(); } - MusicManager musicManager = new MusicManager("Fight_Music.wav", -15.0f); battle = new Battle(heroes, enemies, this); isReadyToPaint = true; @@ -151,7 +154,7 @@ public class WindowOfBattle extends JPanel { battle.startBattle(); - if (musicManager != null){ + if (musicManager != null) { musicManager.playMusic(); musicManager.setVolumeToMaxVolume(); } @@ -178,8 +181,12 @@ public class WindowOfBattle extends JPanel { return game; } + public MusicManager getMusicManager() { + return musicManager; + } + class Canvas extends JPanel { - Canvas(){ + Canvas() { setPreferredSize(new Dimension( CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT)); diff --git a/src/main/resources/battles/storageroombattle.txt b/src/main/resources/battles/storageroombattle.txt index 546413f..258280f 100644 --- a/src/main/resources/battles/storageroombattle.txt +++ b/src/main/resources/battles/storageroombattle.txt @@ -1,3 +1,3 @@ 100 10 Mark 1 10 1 100 10 Karen 10 1 - Fight_Music.wav -10.0f \ No newline at end of file + Fight_Music.wav -20.0f \ No newline at end of file diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt index b09722c..eef5d1f 100644 --- a/src/main/resources/save/savedGame/events.txt +++ b/src/main/resources/save/savedGame/events.txt @@ -44,7 +44,7 @@ 1 9 - true + false diff --git a/src/main/resources/save/savedGame/inventory.txt b/src/main/resources/save/savedGame/inventory.txt index e69de29..ad81884 100644 --- a/src/main/resources/save/savedGame/inventory.txt +++ b/src/main/resources/save/savedGame/inventory.txt @@ -0,0 +1,6 @@ + + blue-key-card + 1 + 39 + key + diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt index ec0fcae..85956e7 100644 --- a/src/main/resources/save/savedGame/person.txt +++ b/src/main/resources/save/savedGame/person.txt @@ -1,6 +1,6 @@ storageroom - 6 - 2 - 0 + 2 + 7 + 3 \ No newline at end of file