2020-06-09 15:46:36 +02:00

360 lines
13 KiB
Java

package ruscz.game.gameOfProd.Windows;
import ruscz.game.gameOfProd.Cells.*;
import ruscz.game.gameOfProd.Dialog.DialogBar;
import ruscz.game.gameOfProd.Game;
import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapManager;
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.io.File;
import java.io.IOException;
import java.util.LinkedList;
public class WindowOfFreeMove extends JLayeredPane {
public static final int CELL_SIZE = 70;
public static int CANVAS_WIDTH = 50;
public static int CANVAS_HEIGHT = 50;
public static final int WINDOW_WIDTH = 13;
public static final int WINDOW_HEIGHT = 13;
private static boolean canPersonMove;
private static int nextMove;
private static boolean isReadyToPaint;
public static final int KEY_LEFT = 37;
public static final int KEY_UP = 38;
public static final int KEY_RIGHT = 39;
public static final int KEY_DOWN = 40;
public static final int KEY_EVENT = 69;
private static final int KEY_ESCAPE = 27;
public static String getCHAPTER() {
return CHAPTER;
}
public static void setCHAPTER(String CHAPTER) {
WindowOfFreeMove.CHAPTER = CHAPTER;
}
private static String CHAPTER = "0";
private Game game;
private Canvas canvas;
private DialogBar dialogBar;
private MapManager mapManager;
private Camera camera;
private Timer inputTimer;
private MusicManager musicManager;
private Foreground foreground;
private LinkedList<Map> mapLinkedList;
public WindowOfFreeMove windowOfFreeMove() { return this;}
public MapManager getMapManager() {
return mapManager;
}
public void repaintCanvas() {
mapManager.getActualMap().getPerson().move();
camera.moveCamera();
canvas.repaint();
foreground.repaint();
dialogBar.repaint();
}
public static void setCanPersonMove(boolean canPersonMove) {
WindowOfFreeMove.canPersonMove = canPersonMove;
}
public static boolean canPersonMove() {
return canPersonMove;
}
public void setMapManager(Map mapManager) {
this.mapManager.setMap(mapManager);
}
public void paintMapOnWindow() {
this.mapManager.createMap();
}
public static void setNextMove(int nextMove) {
WindowOfFreeMove.nextMove = nextMove;
}
public DialogBar getDialogBar() {
return dialogBar;
}
public LinkedList<Map> getMapLinkedList() {
return mapLinkedList;
}
public Camera getCamera() { return camera; }
public Foreground getForegroundPanel() { return foreground; }
public MusicManager getMusicManager() { return musicManager; }
public Game getGame() {
return game;
}
public WindowOfFreeMove(Game game) {
this.game = game;
isReadyToPaint = false;
canPersonMove = true;
canvas = new Canvas();
camera = new Camera(this, canvas);
canvas.setBackground(Color.BLACK);
canvas.setPreferredSize(new Dimension(
CELL_SIZE * CANVAS_WIDTH,
CELL_SIZE * CANVAS_HEIGHT));
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);
dialogBar.setOpaque(false);
dialogBar.setVisible(false);
dialogBar.setLayout(null);
foreground = new Foreground();
foreground.setSize(CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_HEIGHT);
foreground.setOpaque(false);
foreground.setVisible(false);
inputTimer = new Timer(10, actionEvent -> {
if (!mapManager.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) {
mapManager.getActualMap().getPerson().setDirectionMove(nextMove);
}
});
add(dialogBar, JLayeredPane.MODAL_LAYER);
add(foreground, JLayeredPane.PALETTE_LAYER);
add(canvas, JLayeredPane.PALETTE_LAYER);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KEY_ESCAPE){
game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING));
}
else {
if (canPersonMove) {
nextMove = e.getKeyCode();
inputTimer.start();
}
}
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (!mapManager.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) {
mapManager.getActualMap().getPerson().setDirectionMove(nextMove);
}
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
inputTimer.stop();
}
});
}
public void start(boolean newGame) {
centerContent();
mapLinkedList = new LinkedList<>();
mapManager = new MapManager(mapLinkedList, canvas, this);
if (newGame) {
mapManager.getLoadManager().clearSave();
mapManager.getLoadManager().loadNewGame();
Map firstMap = Map.findMapByName(Map.getMapWithSpawn(), this);
setMapManager(firstMap);
musicManager = new MusicManager("Town3.wav", -25.0f);
}
else {
mapManager.getLoadManager().loadSavedGame();
musicManager = new MusicManager("Town3.wav", -25.0f);
musicManager.playMusic();
musicManager.fadeIn(10);
}
mapManager.createMap();
isReadyToPaint = true;
Timer timer = new Timer(20, actionEvent -> {
mapManager.getActualMap().getPerson().move();
camera.moveCamera();
canvas.repaint();
foreground.repaint();
dialogBar.repaint();
});
timer.start();
}
public void loadNextChapter() {
CHAPTER = String.valueOf(Integer.parseInt(CHAPTER) + 1);
isReadyToPaint = false;
mapLinkedList = new LinkedList<>();
mapManager = new MapManager(mapLinkedList, canvas, this);
mapManager.getLoadManager().loadNewGame();
Map map = Map.findMapByName(Map.getMapWithSpawn(), this);
setMapManager(map);
mapManager.createMap();
isReadyToPaint = true;
}
public void startBattle(String name) {
setVisible(false);
WindowOfBattle battle = game.getBattle();
mapManager.getWindow().getGame().add(battle);
battle.start(name);
}
public class Canvas extends JPanel {
@Override
public void paint(Graphics g) {
if (isReadyToPaint && mapManager != null) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < mapManager.getActualMap().getCanvasSize()[1]; i++) {
for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[0]; j++) {
mapManager.getActualMap().getImageCell().paintBackground(g2D, j, i);
}
for (Cell cell : mapManager.getActualMap().getMoveToCells()) {
if (cell.getY() == i && isinView(cell)) cell.paint(g2D);
}
for (Block cell : mapManager.getActualMap().getBlocks()) {
if (cell.getY() == i && !cell.isPartial() && isinView(cell)) cell.paint(g2D);
}
if (mapManager.getActualMap().getPerson().getY() == i && mapManager.getActualMap().getPerson().getDirectionMoveLast() != KEY_UP) mapManager.getActualMap().getPerson().paint(g2D);
for (Cell cell : mapManager.getActualMap().getInteractionEventCells()) {
if (cell.getY() == i && isinView(cell)) cell.paint(g2D);
}
for (ChestCell cell : mapManager.getActualMap().getChestCells()) {
if (cell.getY() == i && cell.getPaint() && isinView(cell)) cell.paint(g2D);
}
if (mapManager.getActualMap().getPerson().getY() == (i-1) && mapManager.getActualMap().getPerson().getDirectionMoveLast() == KEY_UP) mapManager.getActualMap().getPerson().paint(g2D);
for (Block cell : mapManager.getActualMap().getBlocks()) {
if (cell.getY() == i && cell.isPartial() && isinView(cell)) cell.paint(g2D);
}
for (DoorCell cell : mapManager.getActualMap().getDoorCells()) {
if (cell.getY() == i && isinView(cell)) cell.paint(g2D);
}
for (Person person : mapManager.getActualMap().getNpcs()) {
if (person.getY() == i && isinView(person)) person.paint(g2D);
}
for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[0]; j++) {
if (isinView(j, i)) mapManager.getActualMap().getImageCell().paintForeground(g2D, j, i);
}
for (ChestCell cell : mapManager.getActualMap().getChestCells()) {
if (cell.getY() == i && cell.getShowItem() && isinView(cell)) cell.paint(g2D);
}
}
}
}
private boolean isinView(Cell cell) {
Boolean xValue = (CELL_SIZE * (cell.getX()+1) > -canvas.getLocation().x) && (CELL_SIZE * (cell.getX()-1) < (-canvas.getLocation().x + CELL_SIZE * WINDOW_WIDTH));
Boolean yValue = (CELL_SIZE * (cell.getY()+1) > -canvas.getLocation().y) && (CELL_SIZE * (cell.getY()-1) < (-canvas.getLocation().y + CELL_SIZE * WINDOW_HEIGHT));
return xValue && yValue;
}
private boolean isinView(int x, int y) {
Boolean xValue = (CELL_SIZE * (x+1) > -canvas.getLocation().x) && (CELL_SIZE * (x-1) < (-canvas.getLocation().x + CELL_SIZE * WINDOW_WIDTH));
Boolean yValue = (CELL_SIZE * (y+1) > -canvas.getLocation().y) && (CELL_SIZE * (y-1) < (-canvas.getLocation().y + CELL_SIZE * WINDOW_HEIGHT));
return xValue && yValue;
}
}
public class Foreground extends JPanel {
private Image imageForeground;
private int r, g, b, a;
private Timer fadeTimer;
public void setImageForeground(String name) {
if (name == null) imageForeground = null;
else {
try {
imageForeground = ImageIO.read(new File("src/main/resources/images/" + name + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void setColorForeground(int r, int g, int b, int a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public void fade(int startAlpha, int endAlpha, int delay) {
a = startAlpha;
fadeTimer = new Timer(delay, actionEvent -> {
if (a == endAlpha) stopTimer();
else if (startAlpha < endAlpha) {
a += 5;
}
else {
a -= 5;
}
});
fadeTimer.start();
}
private void stopTimer() {
fadeTimer.stop();
}
@Override
public void paint(Graphics gr) {
if (imageForeground != null)
gr.drawImage(imageForeground, 0, 0, CELL_SIZE * WINDOW_WIDTH,
CELL_SIZE * WINDOW_HEIGHT, this);
else {
gr.setColor(new Color(r, g, b, a));
gr.fillRect(0, 0, CELL_SIZE * WINDOW_WIDTH,
CELL_SIZE * WINDOW_HEIGHT);
}
}
}
public static void sleep(long tm) {
try {
Thread.sleep(tm);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void setCanvasSize(int[] size) {
CANVAS_WIDTH = size[0];
CANVAS_HEIGHT = size[1];
}
public void centerContent() {
game.getComponents()[0].setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - (WINDOW_WIDTH * CELL_SIZE)) / 2, (Toolkit.getDefaultToolkit().getScreenSize().height - (WINDOW_HEIGHT * CELL_SIZE)) / 2);
game.getComponents()[0].setSize(WINDOW_WIDTH * CELL_SIZE, WINDOW_HEIGHT * CELL_SIZE);
}
}