2020-03-20 20:25:43 +01:00

259 lines
8.1 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 javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
public class WindowOfFreeMove extends JFrame {
public static final int CELL_SIZE = 40;
public static final int CANVAS_WIDTH = 36;
public static final int CANVAS_HEIGHT = 36;
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;
private Canvas canvas;
private DialogBar dialogBar;
private MapPainter mapPainter;
private Timer inputTimer;
private LinkedList<Map> mapLinkedList;
public MapPainter getMapPainter() {
return mapPainter;
}
public void repaintCanvas() {
canvas.repaint();
}
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 WindowOfFreeMove() {
isReadyToPaint = false;
canPersonMove = true;
JLayeredPane lp = getLayeredPane();
canvas = new 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.setBackground(Color.BLACK);
dialogBar.setVisible(false);
dialogBar.setLayout(null);
inputTimer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent 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 (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);
setTitle("Ahoj");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH + 15, CELL_SIZE * WINDOW_HEIGHT + 35);
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(40 * i, 40 * j, 40, 40);
count++;
}
}
Map m1 = Map.loadMapFromFile("m1", this);
m1.getPerson().setImagePerson("mainpng.png", rec);
m1.setName("m1");
Map m2 = Map.loadMapFromFile("m2", this);
m2.getPerson().setImagePerson("mainpng.png", rec);
m2.setName("m2");
Map m3 = Map.loadMapFromFile("m3", this);
m3.getPerson().setImagePerson("mainpng.png", rec);
m3.setName("m3");
Map m4 = Map.loadMapFromFile("m4", this);
m4.getPerson().setImagePerson("mainpng.png", rec);
m4.setName("m4");
Map m5 = Map.loadMapFromFile("m5", this);
m5.getPerson().setImagePerson("mainpng.png", rec);
m5.setName("m5");
Map m6 = Map.loadMapFromFile("m6", this);
m6.getPerson().setImagePerson("mainpng.png", 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);
Npc npc1 = new Npc(1,1, m5, mapPainter);
m5.setNpcs(npc1);
setMapPainter(m5);
mapPainter.moveCamera();
mapPainter.createMap();
isReadyToPaint = true;
canvas.repaint();
Npc npc2 = new Npc(1,1, m1, mapPainter);
m1.setNpcs(npc2);
Timer timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
mapPainter.getActualMap().getPerson().move();
mapPainter.moveCamera();
canvas.repaint();
}
});
timer.start();
while (true){
npc2.move(6,1);
npc2.move(1, 6);
}
}
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 < CANVAS_WIDTH; i++) {
for (int j = 0; j < CANVAS_HEIGHT; 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();
}
}
}