-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManager.java
295 lines (268 loc) · 9.66 KB
/
GameManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import components.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* This class manages the current JFrame's panel. Essentially, this is the train station where all locations/menus
* connect in the program before being sent to their destination.
* @author Jacob Odenthal, Owen Talberg
* @version 8/9/24
*/
public class GameManager {
/**
* Game window to manage
*/
JFrame gameWindow;
/**
* Our hero; he rode a blazing saddle, he wore a shining star
*/
Player player;
/**
* Handles the game layout/graph
*/
GameLayout layout;
/**
* Handles the game locations
*/
LocationManager locater;
/**
* Game Over sprite image
*/
BufferedImage gameOver;
/**
* Game Won sprite image
*/
BufferedImage gameWon;
/**
* Title/Pause Menu display image
*/
BufferedImage titleImg;
/**
* Constructor that takes in the game window to manage
* @param gameWindow JFrame
*/
public GameManager(JFrame gameWindow) {
this.gameWindow = gameWindow;
this.player = new Player();
this.layout = new GameLayout(this.player);
this.locater = new LocationManager(this, layout);
try {
this.gameOver = ImageIO.read(new File("sprites/game_over.png"));
this.gameWon = ImageIO.read(new File("sprites/game_won.png"));
this.titleImg = ImageIO.read(new File("sprites/title_image.png"));
}
catch (IOException e) {
System.out.println("Could not load menu sprite");
}
}
/**
* Method that continually determines where user travels in the game until
* it is closed or finished
* @param pause False if initial launch
*/
public void startMenu(boolean pause) {
gameWindow.getContentPane().removeAll(); // TODO: May cause issues
gameWindow.add(loadMenu(pause));
gameWindow.revalidate();
}
/**
* Changes the location currently shown on the screen
* @param gameWindow JFrame to change
* @param location String location name
*/
public void changeLocation(String location) {
gameWindow.getContentPane().removeAll();
GamePanel newPanel = locater.getLocation(location, player);
gameWindow.add(newPanel);
layout.setCurLocation(location);
gameWindow.revalidate();
newPanel.requestFocusInWindow();
}
/**
* Hopefully you never see this. Shows the lose game menu if you lose
*/
public void loseGame() {
gameWindow.getContentPane().removeAll();
JPanel ggPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel ggLabel = new JLabel(new ImageIcon(gameOver));
c.fill = GridBagConstraints.PAGE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
ggPanel.add(ggLabel,c);
// restart Game Button:
// TODO: implement
/*
JButton restartBt = new JButton("Restart?");
c.fill = GridBagConstraints.PAGE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
c.ipady = 10;
// This format is how Buttons perform functions and other commands
restartBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ggPanel.setVisible(false);
player = new Player();
layout = new GameLayout(player);
locater = new LocationManager(manager, layout);
layout.newGame();
changeLocation("graveyard");
gameWindow.remove(ggPanel);
}
});
restartBt.setLocation(250, 400);
ggPanel.add(restartBt, c);
*/
gameWindow.add(ggPanel);
gameWindow.revalidate();
}
/**
* Extremely rewarding. Win game menu
*/
public void winGame() {
gameWindow.getContentPane().removeAll();
JPanel ggPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel ggLabel = new JLabel(new ImageIcon(gameWon));
c.fill = GridBagConstraints.PAGE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
ggPanel.add(ggLabel,c);
// restart Game Button:
// TODO: implement
/*
JButton restartBt = new JButton("Restart?");
c.fill = GridBagConstraints.PAGE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 1;
c.ipady = 10;
// This format is how Buttons perform functions and other commands
restartBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ggPanel.setVisible(false);
player = new Player();
layout = new GameLayout(player);
locater = new LocationManager(manager, layout);
layout.newGame();
changeLocation("graveyard");
gameWindow.remove(ggPanel);
}
});
ggPanel.add(restartBt, c);
*/
gameWindow.add(ggPanel);
gameWindow.revalidate();
}
// GAME PANELS ========================================================================================
/**
* Creates the main menu panel, can be called again later
* @param pause Boolean True if pausing the game, false if launching the main menu
*/
public JPanel loadMenu(boolean pause) {
// Menu panel
JPanel menu = new JPanel(new GridBagLayout());
menu.setLayout(null);
// Basic Game Title: can change to an image later if we get ambitious
BufferedImage logoImg;
try {
logoImg = ImageIO.read(new File("sprites/game_logo.png"));
JLabel title = new JLabel(new ImageIcon(logoImg));
title.setBounds(-10, 0, 600, 300);
menu.add(title);
}
catch (IOException e) {
System.out.println("Could not load title image");
}
// Chooses Continue Game or New Game whether it's a pause menu or not
if (!pause) {
// New Game Button:
JButton startBt = new JButton("New Game");
// This format is how Buttons perform functions and other commands
startBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
menu.setVisible(false);
layout.newGame();
changeLocation("graveyard");
}
});
startBt.setBounds(100, 440, 400, 50);
menu.add(startBt);
}
else {
// Continue Game Button:
JButton contBt = new JButton("Continue Game");
// Resumes the game
contBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
menu.setVisible(false);
changeLocation(layout.getCurLocation()); // Lame fix, restarts the current panel
}
});
contBt.setBounds(100, 440, 400, 50);
menu.add(contBt);
// Save Game Button:
JButton saveBt = new JButton("Save Game");
// This format is how Buttons perform functions and other commands
saveBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String result = (String)JOptionPane.showInputDialog(
menu,
"Please enter save name",
"Save Prompt",
JOptionPane.PLAIN_MESSAGE,
null,
null,
"testsave"
);
layout.saveGame(result + ".txt");
}
});
saveBt.setBounds(100, 500, 400, 50);
menu.add(saveBt);
}
// Load Game Button:
JButton loadBt = new JButton("Load Game");
// This format is how Buttons perform functions and other commands
loadBt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new FileNameExtensionFilter("Text Files", "txt"));
// invoke the showsSaveDialog function to show the save dialog
int r = chooser.showOpenDialog(null);
// if the user selects a file
if (r == JFileChooser.APPROVE_OPTION) {
// set the label to the path of the selected file
File savedGame = chooser.getSelectedFile();
layout.loadGame(savedGame);
System.out.println(layout.getStart());
changeLocation(layout.getCurLocation());
}
}
});
menu.setBackground(Color.decode("#090a6b"));
loadBt.setBounds(100, 500, 400, 50);
menu.add(loadBt);
JLabel titleBg = new JLabel(new ImageIcon(titleImg));
titleBg.setBounds(0, 0, 600, 600);
menu.add(titleBg);
return menu;
}
}