2020-04-12 17:11:30 +02:00

273 lines
9.9 KiB
Java

package ruscz.game.gameOfProd.Windows;
import ruscz.game.gameOfProd.Cells.*;
import ruscz.game.gameOfProd.Dialog.DialogBar;
import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapManager;
import ruscz.game.gameOfProd.Sounds.MusicManager;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.util.LinkedList;
import static java.awt.Frame.MAXIMIZED_BOTH;
import static java.awt.Frame.NORMAL;
public class WindowOfFreeMove extends JLayeredPane {
public static final int CELL_SIZE = 50;
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;
private JFrame game;
private Canvas canvas;
private DialogBar dialogBar;
private MapManager mapManager;
private Camera camera;
private Timer inputTimer;
private MusicManager musicManager;
private LinkedList<Map> mapLinkedList;
public WindowOfFreeMove windowOfFreeMove() { return this;}
public MapManager getMapManager() {
return mapManager;
}
public void repaintCanvas() {
canvas.repaint();
}
public static void setCanPersonMove(boolean canPersonMove) {
WindowOfFreeMove.canPersonMove = 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 WindowOfFreeMove(JFrame 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_HEIGHT, CELL_SIZE * CANVAS_WIDTH);
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);
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(canvas, JLayeredPane.PALETTE_LAYER);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KEY_ESCAPE){
System.out.println("escape");
game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING));
return;
}
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 m5 = Map.findMapByName("m5", this);
setMapManager(m5);
}
else {
mapManager.getLoadManager().loadSavedGame();
}
Map boss = Map.findMapByName("boss", this);
int startX = 4;
int startY = 2;
mapManager.createMap();
isReadyToPaint = true;
Npc npc2 = new Npc(startX, startY, boss, mapManager);
boss.setNpcs(npc2);
musicManager = new MusicManager("Town1.wav");
// musicManager.playMusic();
Timer timer = new Timer(10, actionEvent -> {
mapManager.getActualMap().getPerson().move();
camera.moveCamera();
canvas.repaint();
dialogBar.repaint();
});
timer.start();
}
public class Canvas extends JPanel {
@Override
// public void paint(Graphics g) {
// if (isReadyToPaint) {
// super.paint(g);
// Graphics2D g2D = (Graphics2D) g;
// g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
// for (int i = 0; i < mapManager.getActualMap().getCanvasSize()[0]; i++) {
// for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[1]; j++) {
// mapManager.getActualMap().getImageCell().paintBackground(g2D, i, j);
// }
// }
// for (Cell cell : mapManager.getActualMap().getMoveToCells()) {
// cell.paint(g2D);
// }
// for (Cell cell : mapManager.getActualMap().getBlocks()) {
// cell.paint(g2D);
// }
// for (Cell cell : mapManager.getActualMap().getInteractionEventCells()) {
// cell.paint(g2D);
// }
// for (Cell cell : mapManager.getActualMap().getDoorCells()) {
// cell.paint(g2D);
// }
// for (Person person : mapManager.getActualMap().getNpcs()) {
// person.paint(g2D);
// }
// for (int i = 0; i < mapManager.getActualMap().getCanvasSize()[0]; i++) {
// for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[1]; j++) {
// mapManager.getActualMap().getImageCell().paintForeground(g2D, i, j);
// }
// }
// mapManager.getActualMap().getPerson().paint(g2D);
// }
// }
public void paint(Graphics g) {
if (isReadyToPaint) {
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) cell.paint(g2D);
}
for (Cell cell : mapManager.getActualMap().getBlocks()) {
if (cell.getY() == i) 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) cell.paint(g2D);
}
if (mapManager.getActualMap().getPerson().getY() == (i-1) && mapManager.getActualMap().getPerson().getDirectionMoveLast() == KEY_UP) mapManager.getActualMap().getPerson().paint(g2D);
for (Cell cell : mapManager.getActualMap().getDoorCells()) {
if (cell.getY() == i) cell.paint(g2D);
}
for (Person person : mapManager.getActualMap().getNpcs()) {
if (person.getY() == i) person.paint(g2D);
}
for (int j = 0; j < mapManager.getActualMap().getCanvasSize()[1]; j++) {
mapManager.getActualMap().getImageCell().paintForeground(g2D, j, i);
}
}
}
}
}
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);
}
}