From 00c34e649d743e3d042d98948ca4ad6150671634 Mon Sep 17 00:00:00 2001 From: musilto9 Date: Sun, 5 Apr 2020 00:19:15 +0200 Subject: [PATCH] added menu bugged --- src/main/java/Game.java | 79 ++++++++++++++- .../gameOfProd/Windows/WindowOfFreeMove.java | 95 +++++++------------ .../game/gameOfProd/Windows/WindowOfMenu.java | 77 +++++++++++++++ src/main/resources/save/savedGame/events.txt | 50 ---------- .../resources/save/savedGame/inventory.txt | 1 - src/main/resources/save/savedGame/person.txt | 5 - src/main/resources/save/savedGame/village.txt | 1 - 7 files changed, 186 insertions(+), 122 deletions(-) create mode 100644 src/main/java/ruscz/game/gameOfProd/Windows/WindowOfMenu.java diff --git a/src/main/java/Game.java b/src/main/java/Game.java index d2c396f..1cc0c9d 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,10 +1,83 @@ import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; +import ruscz.game.gameOfProd.Windows.WindowOfMenu; -public class Game { +import javax.swing.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.WindowEvent; + +public class Game extends JFrame { + public static final int WINDOW_WIDTH = 11; + public static final int WINDOW_HEIGHT = 11; + public static final int CELL_SIZE = 50; + + private static final int KEY_FULLSCREEN = 115; + private static final int KEY_ESCAPE = 27; + + private WindowOfMenu menu; + private WindowOfFreeMove freeMove; public static void main(String[] args) { - WindowOfFreeMove window = new WindowOfFreeMove(); - window.start(); + new Game(); } + + public Game() { + freeMove = new WindowOfFreeMove(this); + menu = new WindowOfMenu(this, freeMove); + + //tohle spustí na začátku menu, ale nejsem schopen přinutit to menu, aby se vzdalo Listeneru a mohl ho využít Game + //add(menu); + + //tohle spustí na začátku hru, vše funguje tak, jak má + add(freeMove); + freeMove.start(true); + + setDefaultCloseOperation(EXIT_ON_CLOSE); + setUndecorated(true); + setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH, CELL_SIZE * WINDOW_HEIGHT); + setLocationRelativeTo(null); + setResizable(false); + setVisible(true); + + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() == KEY_ESCAPE){ + dispatchEvent(new WindowEvent(game(), WindowEvent.WINDOW_CLOSING)); + return; + } + + if (e.getKeyCode() == KEY_FULLSCREEN){ + + if (getExtendedState() == MAXIMIZED_BOTH) + setExtendedState(NORMAL); + else + setExtendedState(MAXIMIZED_BOTH); + return; + } + + else { + freeMove.keyPressed(e); + } + } + }); + + addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + freeMove.keyTyped(e); + } + }); + + addKeyListener(new KeyAdapter() { + @Override + public void keyReleased(KeyEvent e) { + freeMove.keyReleased(e); + } + }); + + } + + public Game game() {return this; } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index 589d656..73cf445 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -13,7 +13,10 @@ import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.util.LinkedList; -public class WindowOfFreeMove extends JFrame { +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; @@ -33,6 +36,7 @@ public class WindowOfFreeMove extends JFrame { private static final int KEY_FULLSCREEN = 115; private static final int KEY_ESCAPE = 27; + private JFrame game; private Canvas canvas; private DialogBar dialogBar; private MapManager mapManager; @@ -80,10 +84,11 @@ public class WindowOfFreeMove extends JFrame { public Camera getCamera() { return camera; } - public WindowOfFreeMove() { + public WindowOfFreeMove(JFrame game) { + this.game = game; isReadyToPaint = false; canPersonMove = true; - JLayeredPane lp = getLayeredPane(); + JPanel background = new JPanel(); background.setBounds(-WINDOW_WIDTH/2, -WINDOW_HEIGHT/2, (CANVAS_WIDTH + WINDOW_WIDTH)*CELL_SIZE, (CANVAS_HEIGHT + WINDOW_HEIGHT)*CELL_SIZE); background.setBackground(Color.BLACK); @@ -108,72 +113,26 @@ public class WindowOfFreeMove extends JFrame { } }); - 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 keyPressed(KeyEvent e) { - if (e.getKeyCode() == KEY_ESCAPE){ - dispatchEvent(new WindowEvent(windowOfFreeMove(), WindowEvent.WINDOW_CLOSING)); - return; - } - - if (e.getKeyCode() == KEY_FULLSCREEN){ - - if (getExtendedState() == MAXIMIZED_BOTH) - setExtendedState(NORMAL); - else - setExtendedState(MAXIMIZED_BOTH); - return; - } - - if (canPersonMove) { - nextMove = e.getKeyCode(); - inputTimer.start(); - } - } - }); - - addKeyListener(new KeyAdapter() { - @Override - public void keyReleased(KeyEvent e) { - inputTimer.stop(); - } - }); - - lp.add(dialogBar, JLayeredPane.MODAL_LAYER); - lp.add(canvas, JLayeredPane.PALETTE_LAYER); - lp.add(background, JLayeredPane.DEFAULT_LAYER); - - setDefaultCloseOperation(EXIT_ON_CLOSE); - setUndecorated(true); - setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH, CELL_SIZE * WINDOW_HEIGHT); - setLocationRelativeTo(null); - setResizable(false); - setVisible(true); + add(dialogBar, JLayeredPane.MODAL_LAYER); + add(canvas, JLayeredPane.PALETTE_LAYER); + add(background, JLayeredPane.DEFAULT_LAYER); } - public void start() { + public void start(boolean newGame) { mapLinkedList = new LinkedList<>(); mapManager = new MapManager(mapLinkedList, canvas, this); - // to load a new game + if (newGame) { mapManager.getLoadManager().clearSave(); mapManager.getLoadManager().loadNewGame(); Map m5 = Map.findMapByName("m5", this); setMapManager(m5); - - //to load a saved game - //mapManager.getLoadManager().loadSavedGame(); + } + else { + mapManager.getLoadManager().loadSavedGame(); + } Map m6 = Map.findMapByName("m6", this); @@ -199,11 +158,23 @@ public class WindowOfFreeMove extends JFrame { dialogBar.repaint(); }); timer.start(); - while (true) { - npc2.move(endX, endY); - npc2.move(startX, startY); - } + } + public void keyTyped(KeyEvent e) { + if (!mapManager.getActualMap().getPerson().getIsMoving() && canPersonMove || nextMove == KEY_EVENT) { + mapManager.getActualMap().getPerson().setDirectionMove(nextMove); + } + } + + public void keyPressed(KeyEvent e) { + if (canPersonMove) { + nextMove = e.getKeyCode(); + inputTimer.start(); + } + } + + public void keyReleased(KeyEvent e) { + inputTimer.stop(); } public class Canvas extends JPanel { diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfMenu.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfMenu.java new file mode 100644 index 0000000..eb6093c --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfMenu.java @@ -0,0 +1,77 @@ +package ruscz.game.gameOfProd.Windows; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowEvent; + +public class WindowOfMenu extends JLayeredPane { + public static final int WINDOW_WIDTH = 11; + public static final int WINDOW_HEIGHT = 11; + public static final int CELL_SIZE = 50; + + private JFrame game; + private WindowOfFreeMove freeMove; + private JButton startNewGame; + private JButton loadSavedGame; + private JButton exitGame; + + public WindowOfMenu(JFrame game, WindowOfFreeMove freeMove) { + this.game = game; + this.freeMove = freeMove; + + JPanel background = new JPanel(); + background.setBounds(0, 0, WINDOW_WIDTH*CELL_SIZE, WINDOW_HEIGHT*CELL_SIZE); + background.setBackground(Color.BLACK); + + startNewGame = new JButton("START NEW GAME"); + startNewGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 -70,200,30); + startNewGame.setActionCommand("startNewGame"); + startNewGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + cleanUp(); + game.add(freeMove); + freeMove.start(true); + } + }); + + loadSavedGame = new JButton("LOAD SAVED GAME"); + loadSavedGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2,200,30); + loadSavedGame.setActionCommand("loadSavedGame"); + loadSavedGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + cleanUp(); + game.add(freeMove); + freeMove.start(false); + } + }); + + exitGame = new JButton("EXIT GAME"); + exitGame.setBounds((WINDOW_WIDTH*CELL_SIZE - 200) / 2,(WINDOW_HEIGHT*CELL_SIZE - 30) / 2 + 70,200,30); + exitGame.setActionCommand("exitGame"); + exitGame.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent actionEvent) { + game.dispatchEvent(new WindowEvent(game, WindowEvent.WINDOW_CLOSING)); + } + }); + + add(startNewGame); + add(loadSavedGame); + add(exitGame); + add(background); + } + + private void cleanUp() { + setVisible(false); + game.remove(this); +// startNewGame.removeAll(); +// loadSavedGame.removeAll(); +// exitGame.removeAll(); + } + + public WindowOfMenu menu() { return this; } +} diff --git a/src/main/resources/save/savedGame/events.txt b/src/main/resources/save/savedGame/events.txt index 5807f8a..e69de29 100644 --- a/src/main/resources/save/savedGame/events.txt +++ b/src/main/resources/save/savedGame/events.txt @@ -1,50 +0,0 @@ - - m1 - - - - - m2 - - - 7 - 2 - true - - - - - m3 - - - 2 - 5 - true - - - - - m4 - - - 3 - 3 - true - - - - - m5 - - - - - m6 - - - 11 - 11 - true - - - diff --git a/src/main/resources/save/savedGame/inventory.txt b/src/main/resources/save/savedGame/inventory.txt index 7082420..e69de29 100644 --- a/src/main/resources/save/savedGame/inventory.txt +++ b/src/main/resources/save/savedGame/inventory.txt @@ -1 +0,0 @@ -TODO: Inventory \ No newline at end of file diff --git a/src/main/resources/save/savedGame/person.txt b/src/main/resources/save/savedGame/person.txt index 5249531..e69de29 100644 --- a/src/main/resources/save/savedGame/person.txt +++ b/src/main/resources/save/savedGame/person.txt @@ -1,5 +0,0 @@ - - m5 - 4 - 3 - \ No newline at end of file diff --git a/src/main/resources/save/savedGame/village.txt b/src/main/resources/save/savedGame/village.txt index 1b4bc7b..e69de29 100644 --- a/src/main/resources/save/savedGame/village.txt +++ b/src/main/resources/save/savedGame/village.txt @@ -1 +0,0 @@ -TODO: Village \ No newline at end of file