286 lines
9.0 KiB
Java
286 lines
9.0 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.MapPainter;
|
|
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;
|
|
|
|
public class WindowOfFreeMove extends JFrame {
|
|
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 = 11;
|
|
public static final int WINDOW_HEIGHT = 11;
|
|
|
|
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;
|
|
|
|
public static final int KEY_FULLSCREEN = 115;
|
|
public static final int KEY_ESCAPE = 27;
|
|
|
|
private Canvas canvas;
|
|
private DialogBar dialogBar;
|
|
private MapPainter mapPainter;
|
|
private Camera camera;
|
|
private Timer inputTimer;
|
|
private MusicManager musicManager;
|
|
|
|
private LinkedList<Map> mapLinkedList;
|
|
|
|
public MapPainter getMapPainter() {
|
|
return mapPainter;
|
|
}
|
|
|
|
public void repaintCanvas() {
|
|
canvas.repaint();
|
|
}
|
|
|
|
private WindowOfFreeMove windowOfFreeMove(){
|
|
return this;
|
|
}
|
|
|
|
public static void setCanPersonMove(boolean canPersonMove) {
|
|
WindowOfFreeMove.canPersonMove = canPersonMove;
|
|
}
|
|
|
|
public void setMapPainter(Map mapPainter) {
|
|
this.mapPainter.setMap(mapPainter);
|
|
}
|
|
|
|
public void paintMapOnWindow() {
|
|
this.mapPainter.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() {
|
|
isReadyToPaint = false;
|
|
canPersonMove = true;
|
|
JLayeredPane lp = getLayeredPane();
|
|
|
|
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.setBounds(0, 0, CELL_SIZE * CANVAS_HEIGHT, CELL_SIZE * CANVAS_WIDTH);
|
|
|
|
dialogBar = new DialogBar();
|
|
dialogBar.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT / 4);
|
|
dialogBar.setOpaque(false);
|
|
dialogBar.setVisible(false);
|
|
dialogBar.setLayout(null);
|
|
|
|
inputTimer = new Timer(10, actionEvent -> {
|
|
if (!mapPainter.getActualMap().getPerson().getIsMoving() && canPersonMove && nextMove != KEY_EVENT) {
|
|
mapPainter.getActualMap().getPerson().setDirectionMove(nextMove);
|
|
}
|
|
});
|
|
|
|
addKeyListener(new KeyAdapter() {
|
|
@Override
|
|
public void keyTyped(KeyEvent e) {
|
|
if (!mapPainter.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) {
|
|
mapPainter.getActualMap().getPerson().setDirectionMove(nextMove);
|
|
}
|
|
}
|
|
});
|
|
|
|
addKeyListener(new KeyAdapter() {
|
|
@Override
|
|
public void keyPressed(KeyEvent e) {
|
|
if (e.getKeyCode() == KEY_ESCAPE){
|
|
dispatchEvent(new WindowEvent(windowOfFreeMove(), WindowEvent.WINDOW_CLOSING));
|
|
}
|
|
|
|
if (e.getKeyCode() == KEY_FULLSCREEN){
|
|
if (getExtendedState() == MAXIMIZED_BOTH)
|
|
setExtendedState(NORMAL);
|
|
else
|
|
setExtendedState(MAXIMIZED_BOTH);
|
|
return;
|
|
}
|
|
|
|
if (canPersonMove) {
|
|
nextMove = e.getKeyCode();
|
|
inputTimer.start();
|
|
}
|
|
// if (canPersonMove && !map.getPerson().getIsMoving() && preventDoubleMove) {
|
|
// map.getPerson().setDirectionMove(e.getKeyCode());
|
|
// }
|
|
// preventDoubleMove = !map.getPerson().getIsMoving();
|
|
}
|
|
});
|
|
|
|
addKeyListener(new KeyAdapter() {
|
|
@Override
|
|
public void keyReleased(KeyEvent e) {
|
|
inputTimer.stop();
|
|
}
|
|
});
|
|
|
|
lp.add(canvas, JLayeredPane.PALETTE_LAYER);
|
|
lp.add(dialogBar, JLayeredPane.POPUP_LAYER);
|
|
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
setUndecorated(true);
|
|
setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH, CELL_SIZE * WINDOW_HEIGHT);
|
|
setLocationRelativeTo(null);
|
|
setResizable(false);
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
public void start() {
|
|
Rectangle[] rec = new Rectangle[12];
|
|
int count = 0;
|
|
for (int i = 0; i < rec.length / 4; i++) {
|
|
for (int j = 0; j < rec.length / 3; j++) {
|
|
rec[count] = new Rectangle(CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE, CELL_SIZE);
|
|
count++;
|
|
}
|
|
}
|
|
|
|
Map m1 = Map.loadMapFromFile("m1", this);
|
|
m1.getPerson().setImagePerson("main", rec);
|
|
m1.setName("m1");
|
|
|
|
Map m2 = Map.loadMapFromFile("m2", this);
|
|
m2.getPerson().setImagePerson("main", rec);
|
|
m2.setName("m2");
|
|
|
|
Map m3 = Map.loadMapFromFile("m3", this);
|
|
m3.getPerson().setImagePerson("main", rec);
|
|
m3.setName("m3");
|
|
|
|
Map m4 = Map.loadMapFromFile("m4", this);
|
|
m4.getPerson().setImagePerson("main", rec);
|
|
m4.setName("m4");
|
|
|
|
Map m5 = Map.loadMapFromFile("m5", this);
|
|
m5.getPerson().setImagePerson("main", rec);
|
|
m5.setName("m5");
|
|
|
|
Map m6 = Map.loadMapFromFile("m6", this);
|
|
m6.getPerson().setImagePerson("main", rec);
|
|
m6.setName("m6");
|
|
|
|
mapLinkedList = new LinkedList<>();
|
|
mapLinkedList.add(m1);
|
|
mapLinkedList.add(m2);
|
|
mapLinkedList.add(m3);
|
|
mapLinkedList.add(m4);
|
|
mapLinkedList.add(m5);
|
|
mapLinkedList.add(m6);
|
|
|
|
m1.linkMaps(m1, this);
|
|
m2.linkMaps(m2, this);
|
|
m3.linkMaps(m3, this);
|
|
m4.linkMaps(m4, this);
|
|
m5.linkMaps(m5, this);
|
|
m6.linkMaps(m6, this);
|
|
|
|
mapPainter = new MapPainter(mapLinkedList, canvas);
|
|
|
|
int startX = 1;
|
|
int startY = 1;
|
|
int endX = 8;
|
|
int endY = 8;
|
|
|
|
setMapPainter(m5);
|
|
|
|
camera.moveCamera();
|
|
mapPainter.createMap();
|
|
isReadyToPaint = true;
|
|
canvas.repaint();
|
|
|
|
Npc npc2 = new Npc(startX, startY, m6, mapPainter);
|
|
m6.setNpcs(npc2);
|
|
|
|
musicManager = new MusicManager("Town1.wav");
|
|
musicManager.playMusic();
|
|
Timer timer = new Timer(10, actionEvent -> {
|
|
mapPainter.getActualMap().getPerson().move();
|
|
camera.moveCamera();
|
|
canvas.repaint();
|
|
});
|
|
timer.start();
|
|
while (true) {
|
|
npc2.move(endX, endY);
|
|
npc2.move(startX, startY);
|
|
}
|
|
|
|
}
|
|
|
|
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 < mapPainter.getActualMap().getCanvasSize()[0]; i++) {
|
|
for (int j = 0; j < mapPainter.getActualMap().getCanvasSize()[1]; j++) {
|
|
mapPainter.getActualMap().getImageCell().paintBackground(g2D, i, j);
|
|
}
|
|
}
|
|
for (Cell cell : mapPainter.getActualMap().getMoveToCells()) {
|
|
cell.paint(g2D);
|
|
}
|
|
mapPainter.getActualMap().getPerson().paint(g2D);
|
|
for (Cell cell : mapPainter.getActualMap().getBlocks()) {
|
|
cell.paint(g2D);
|
|
}
|
|
for (Cell cell : mapPainter.getActualMap().getInteractionEventCells()) {
|
|
cell.paint(g2D);
|
|
}
|
|
for (Person person : mapPainter.getActualMap().getNpcs()) {
|
|
person.paint(g2D);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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];
|
|
}
|
|
|
|
} |