257 lines
8.6 KiB
Java
257 lines
8.6 KiB
Java
package ruscz.game.gameOfProd.Windows;
|
|
|
|
import ruscz.game.gameOfProd.Battle.Battle;
|
|
import ruscz.game.gameOfProd.Battle.BattlePerson;
|
|
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.Sounds.MusicManager;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.KeyAdapter;
|
|
import java.awt.event.KeyEvent;
|
|
import java.awt.event.WindowEvent;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.LinkedList;
|
|
import java.util.Scanner;
|
|
|
|
|
|
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 SituationText getSituationText() {
|
|
return situationText;
|
|
}
|
|
|
|
public EnemyDescription getEnemyDescription() {
|
|
return enemyDescription;
|
|
}
|
|
|
|
public MenuOfActions getMenuOfActions() {
|
|
return menuOfActions;
|
|
}
|
|
|
|
public ConsoleInput getConsoleInput() {
|
|
return consoleInput;
|
|
}
|
|
|
|
public CommandList getCommandList() {
|
|
return commandList;
|
|
}
|
|
|
|
public ItemsInventory getItemsInventory() {
|
|
return itemsInventory;
|
|
}
|
|
|
|
private Canvas canvas;
|
|
private SituationText situationText;
|
|
private EnemyDescription enemyDescription;
|
|
private MenuOfActions menuOfActions;
|
|
private ConsoleInput consoleInput;
|
|
private CommandList commandList;
|
|
private ItemsInventory itemsInventory;
|
|
|
|
private Game game;
|
|
private Battle battle;
|
|
|
|
private MusicManager musicManager;
|
|
|
|
public Battle getBattle() {
|
|
return battle;
|
|
}
|
|
|
|
public WindowOfBattle(Game game) {
|
|
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();
|
|
situationText = new SituationText(this);
|
|
enemyDescription = new EnemyDescription(this);
|
|
menuOfActions = new MenuOfActions(this);
|
|
consoleInput = new ConsoleInput(this);
|
|
commandList = new CommandList(this);
|
|
itemsInventory = new ItemsInventory(this);
|
|
|
|
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(String name) {
|
|
// SaveManager saveManager = game.getFreeMove().getMapManager().getSaveManager();
|
|
// saveManager.saveGame();
|
|
|
|
LinkedList<Hero> heroes = new LinkedList<>();
|
|
LinkedList<Enemy> enemies = new LinkedList<>();
|
|
|
|
try {
|
|
File battleFile = new File("src/main/resources/battles/" + name + ".txt");
|
|
Scanner scanner = new Scanner(battleFile);
|
|
while(scanner.hasNextLine()) {
|
|
String[] data = scanner.nextLine().split(" ");
|
|
if (data[0].equals("<hero>")) {
|
|
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]), data[7]);
|
|
if (data.length > 8) hero.setImageEffect(data[8]);
|
|
heroes.add(hero);
|
|
}
|
|
else if (data[0].equals("<enemy>")) {
|
|
Enemy enemy = new Enemy(Integer.parseInt(data[1]), Integer.parseInt(data[2]), data[3],
|
|
Integer.parseInt(data[4]), Integer.parseInt(data[5]), data[6]);
|
|
if (data.length > 7) enemy.setImageEffect(data[7]);
|
|
enemies.add(enemy);
|
|
}
|
|
else if (data[0].equals("<music>")) {
|
|
musicManager = new MusicManager(data[1], Float.parseFloat(data[2]));
|
|
}
|
|
else if (data[0].equals("<image>")) {
|
|
if (data[1].equals("flicker")) canvas.flicker(Integer.parseInt(data[2]));
|
|
else canvas.addImage(data[1]);
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
MusicManager musicManager = new MusicManager("Fight_Music.wav", -15.0f);
|
|
battle = new Battle(heroes, enemies, this);
|
|
|
|
isReadyToPaint = true;
|
|
Timer t = new Timer(10, actionEvent -> {
|
|
repaint();
|
|
canvas.repaint();
|
|
situationText.repaint();
|
|
});
|
|
t.start();
|
|
|
|
battle.startBattle();
|
|
|
|
if (musicManager != null){
|
|
musicManager.playMusic();
|
|
musicManager.setVolumeToMaxVolume();
|
|
}
|
|
}
|
|
|
|
public Canvas getCanvas() {
|
|
return canvas;
|
|
}
|
|
|
|
public void writeToOutput(String s) {
|
|
System.out.println(s);
|
|
Font f = new Font(Font.SERIF, Font.BOLD, 20);
|
|
if (situationText.getComponents().length == 0) {
|
|
JLabel text = new JLabel();
|
|
situationText.add(text);
|
|
}
|
|
JLabel jLabel = (JLabel) situationText.getComponents()[0];
|
|
jLabel.setText(s);
|
|
jLabel.setFont(f);
|
|
repaint();
|
|
}
|
|
|
|
public Game getGame() {
|
|
return game;
|
|
}
|
|
|
|
class Canvas extends JPanel {
|
|
private LinkedList<BufferedImage> images;
|
|
private BufferedImage image;
|
|
private Timer timer;
|
|
|
|
public void addImage(String name) {
|
|
try {
|
|
images.add(ImageIO.read(new File("src/main/resources/foregrounds/" + name + ".png")));
|
|
image = images.getLast();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void setImage(String name) {
|
|
try {
|
|
image = ImageIO.read(new File("src/main/resources/foregrounds/" + name + ".png"));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void flicker(int delay) {
|
|
timer = new Timer(delay, actionEvent -> {
|
|
image = images.get((int) (Math.random() * images.size()));
|
|
});
|
|
timer.start();
|
|
}
|
|
|
|
Canvas(){
|
|
setPreferredSize(new Dimension(
|
|
CELL_SIZE * CANVAS_WIDTH,
|
|
CELL_SIZE * CANVAS_HEIGHT));
|
|
setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT);
|
|
setBackground(Color.RED);
|
|
images = new LinkedList<>();
|
|
}
|
|
|
|
@Override
|
|
public void paint(Graphics g) {
|
|
super.paint(g);
|
|
if (isReadyToPaint) {
|
|
if (image != null) {
|
|
g.drawImage(image, 0, 0, CELL_SIZE * CANVAS_WIDTH,
|
|
CELL_SIZE * CANVAS_HEIGHT, this);
|
|
}
|
|
for (Enemy battlePerson : battle.getEnemies()) {
|
|
g.drawImage(
|
|
battlePerson.getImage(),
|
|
CELL_SIZE * 2 + battlePerson.getEffectedCoords()[0],
|
|
CELL_SIZE * 4 * battlePerson.getId() + battlePerson.getEffectedCoords()[1],
|
|
battlePerson.getImage().getWidth() * 2,
|
|
battlePerson.getImage().getHeight() * 2,
|
|
this);
|
|
}
|
|
for (BattlePerson battlePerson : battle.getHeroes()) {
|
|
g.drawImage(
|
|
battlePerson.getImage(),
|
|
CELL_SIZE * 12 + battlePerson.getEffectedCoords()[0],
|
|
CELL_SIZE * 4 + battlePerson.getEffectedCoords()[1],
|
|
battlePerson.getImage().getWidth() * 2,
|
|
battlePerson.getImage().getHeight() * 2,
|
|
this);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|