2020-04-23 00:00:02 +02:00

271 lines
9.7 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.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 = 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 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 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 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_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 openspace = Map.findMapByName("tutorial", this);
setMapManager(openspace);
}
else {
mapManager.getLoadManager().loadSavedGame();
}
mapManager.createMap();
isReadyToPaint = true;
musicManager = new MusicManager("Town3.wav", -25.0f);
// musicManager.playMusic();
Timer timer = new Timer(10, actionEvent -> {
mapManager.getActualMap().getPerson().move();
camera.moveCamera();
canvas.repaint();
foreground.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()[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 (Block cell : mapManager.getActualMap().getBlocks()) {
if (cell.getY() == i && !cell.isPartial()) 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);
}
for (ChestCell cell : mapManager.getActualMap().getChestCells()) {
if (cell.getY() == i && cell.getPaint()) 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()) cell.paint(g2D);
}
for (DoorCell 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);
}
for (ChestCell cell : mapManager.getActualMap().getChestCells()) {
if (cell.getY() == i && cell.getShowItem()) cell.paint(g2D);
}
}
}
}
}
public class Foreground extends JPanel {
Image imageForeground;
public void setImageForeground(String name) {
try {
imageForeground = ImageIO.read(new File("src/main/resources/" + name + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void paint(Graphics g) {
g.drawImage(imageForeground, 0, 0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_WIDTH,
WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT, this);
}
}
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);
}
}