added menu bugged

This commit is contained in:
musilto9 2020-04-05 00:19:15 +02:00
parent aa07964b4c
commit 00c34e649d
7 changed files with 186 additions and 122 deletions

View File

@ -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; }
}

View File

@ -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 {

View File

@ -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; }
}

View File

@ -1,50 +0,0 @@
<map>
<mapName>m1</mapName>
<events>
</events>
</map>
<map>
<mapName>m2</mapName>
<events>
<event>
<x>7</x>
<y>2</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m3</mapName>
<events>
<event>
<x>2</x>
<y>5</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m4</mapName>
<events>
<event>
<x>3</x>
<y>3</y>
<active>true</active>
</event>
</events>
</map>
<map>
<mapName>m5</mapName>
<events>
</events>
</map>
<map>
<mapName>m6</mapName>
<events>
<event>
<x>11</x>
<y>11</y>
<active>true</active>
</event>
</events>
</map>

View File

@ -1 +0,0 @@
TODO: Inventory

View File

@ -1,5 +0,0 @@
<location>
<map>m5</map>
<x>4</x>
<y>3</y>
</location>

View File

@ -1 +0,0 @@
TODO: Village