-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameWindow.java
More file actions
529 lines (424 loc) · 20.1 KB
/
Copy pathGameWindow.java
File metadata and controls
529 lines (424 loc) · 20.1 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.net.URL;
//Handles drawing the game window and all of the graphics inside of it
//Also has the code to capture key presses/mouse clicks by the player
//Which ultimately are retrieved and reacted to by Abstract/Basic/CreativeGame classes
//************************************************************************************
//* *
//* *
//* YOU ARE NOT ALLOWED TO MODIFY THIS CLASS *
//* YOU ALSO DON'T NEED TO READ/TRACE/CALL ANY METHODS IN HERE *
//* (But feel free to take a look if you're feeling extra curious!) *
//* *
//* *
//************************************************************************************
public class GameWindow extends JComponent implements KeyListener, MouseListener{
//Constants related to game timing
//target frames rendered per second
private static final int TARGET_FPS = 60;
//number of nano seconds in 1 second
private static final long NANOS_IN_SECOND = 1000000000;
//Ideal time (in nano secs) to wait between rendering frames per the target FPS
private static final long TARGET_FRAMETIME = NANOS_IN_SECOND / TARGET_FPS;
//The default game speed, represented as a percentage (100 = 100% speed)
private static final int DEFAULT_GAME_SPEED = 100;
//Manages the color and thickness of hitboxes when drawn (if debug mode enabled)
private static final int BORDER_THICKNESS = 2;
private static final Color HITBOX_COLOR = Color.MAGENTA;
//Related to the color, size, and positioning of debug text, drawn when debug enabled.
//private static final int DEBUG_FONT_SIZE = 16;
//private static final String DEBUG_FONT_FILE = "debug_font.ttf";
private static final int DEBUG_X_PADDING = 5;
private static final int DEBUG_Y_PADDING = 20;
//Text drawn before any user specified debug text
private static final String DEBUG_PREFIX = "[DEBUG ENABLED] ";
//Window object containing game content
private static JFrame window;
//Collection of all entities to be drawn in the window on each refresh
//Linked to the Collection of the associated GameEngine object
private Collection<Entity> displayList;
//A HashSet containing the four keys pertaining to player movement
//Used in handle keypress to allow for smooth, continuous movement
private static HashSet<Integer> movementKeys;
//A HashSet maintaining the key codes of all keys currently pressed
//In order to ensure smooth movement, movement keys remain in the set until they are released
//Non-movement keys are removed from the set after each call to getKeysPressed()
private HashSet<Integer> keysCurrentlyPressed;
//used by debug text to display last keyboard key pressed
private KeyEvent debugLastKeyPressed = null;
//Current game speed, as percentage (ex: 150 = 150% speed, ie 50% faster than normal)
private int gameSpeed = DEFAULT_GAME_SPEED;
//Hashmap of images loaded into memory
//Key = image file name, value = Image object to be rendered
private HashMap<String, Image> loadedImages;
//The filenames and Image objects of the currently drawn background/splash images
private String bgImageFile, splashImageFile;
private Image backgroundImage, splashImage;
//Tracks the last mouse click
private MouseEvent lastMouseEvent, debugLastMouseEvent;
//Tracks the System.nanoTime() of the last completed render, used in the game timing
private long lastPaintTime;
//Label used to display debug text
private JLabel debugLabel;
//tracks if debug text is currently enabled or not
private boolean isDebugEnabled;
//background color of the window
private Color bgColor;
//tracks if the window is currently painting, forcing the game logic to wait
private static boolean isPainting;
//tracks if the window is currently painting, forcing the game logic to wait
private static boolean isFetchingKeys;
public GameWindow(int windowWidth, int windowHeight, Collection<Entity> displayList){
this.displayList = displayList; //Link this displayList to the respective GameEngine
initLogic();
initWindow(windowWidth, windowHeight);
initDebugText();
adjustFrameTiming();
}
//Initialize Logical components of this GameWindow
private void initLogic(){
this.loadedImages = new HashMap<String, Image>();
movementKeys = new HashSet<Integer>();
//Create a set of all the movement keys
for (int key : GameEngine.MOVEMENT_KEYS)
movementKeys.add(key);
keysCurrentlyPressed = new HashSet<Integer>();
lastPaintTime = System.nanoTime();
isPainting = true;
isFetchingKeys = false;
//No background or splash image to start with
bgImageFile = null;
splashImageFile = null;
}
//Initializes the game window
private void initWindow(int windowWidth, int windowHeight){
window = new JFrame("Super Scrolling Game!");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(windowWidth, windowHeight);
window.add(this);
//Fix Windows listener bug(?)
this.setFocusable(true);
//window.requestFocus();
this.requestFocusInWindow();
this.bgColor = Color.BLACK;
this.setOpaque(true);
window.setVisible(true);
window.setResizable(false);
//Lets the window know that the methods to react to keyboard/mouse actions
//are implemented in this class
addKeyListener(this);
addMouseListener(this);
}
//Initialize the debug text (only drawn when Debug Mode enabled)
private void initDebugText(){
debugLabel = new JLabel(DEBUG_PREFIX);
debugLabel.setBounds(DEBUG_X_PADDING, 0, this.getWidth(), DEBUG_Y_PADDING);
debugLabel.setVerticalAlignment(SwingConstants.BOTTOM);
debugLabel.setForeground(Color.MAGENTA);
Font debugFont = null;
/* Possible font stutter fix??!
try{
debugFont = Font.createFont(Font.TRUETYPE_FONT, new File(getClass().getResource(DEBUG_FONT_FILE).getFile()));
}
catch (FontFormatException | IOException e){
// System.err.println("Could not open debug font file at: " + DEBUG_FONT_FILE);
}
*/
debugLabel.setFont(debugFont);
debugLabel.setText("");
debugLabel.setVisible(true);
this.isDebugEnabled = false;
add(debugLabel);
}
//Called by GameEngine to redraw the window and all of the Entities in it
public void refresh(){
this.repaint();
this.adjustFrameTiming();
}
//called once the game ends
public void endGame(){
//Keep repainting the window to show any last changes in splash screen/etc
while(true)
this.refresh();
}
//Called everytime the game window is "repainted"
public void paintComponent(Graphics g) {
//set isPainting to true to ensure logic stops updating until painting is complete
isPainting = true;
super.paintComponent(g);
//Make sure all the graphics that need to be drawn are loaded into memory
ensureImagesLoaded();
drawBackground(g);//first draw the background
drawEntities(g);//then the entities ontop of that
if (isDebugEnabled()){ //if debug mode is on, draw hitboxes and update debug text
drawHitboxes(g);
updateDebugText();
}
drawSplash(g);//then the splash screen if enabled
//now we're done repainting and the game logic can update!
isPainting = false;
}
//Draws all the Entities in the display list to the game window
private void drawEntities(Graphics g){
try{
for (Entity e : displayList){
Image img = loadedImages.get(e.getImageName());
//Draw each entity per its image name, coordinates, and dimensions
g.drawImage(img, e.getX(), e.getY(), e.getWidth(), e.getHeight(), null);
}
}
catch(RuntimeException re){
//just in case... since students will be modifying the displayList
//should be accounted for via the isPainting flag/adjustFrameTiming()
}
}
//Ensures that the images for every Entity in the display list
//have been loaded into memory
private void ensureImagesLoaded(){
try{
for (Entity e : displayList){
String imageName = e.getImageName();
if (imageName != null && !loadedImages.containsKey(imageName))
loadedImages.put(imageName, GameWindow.readImage(imageName));
}
}
catch(RuntimeException re){
//just in case... since students will be modifying the displayList
//should be accounted for via the isPainting flag/adjustFrameTiming()
}
}
//Can be used to preemptively load a collection of image files
public void preloadImages(ArrayList<String> imageFileNames){
try{
for (int i = 0; i < imageFileNames.size(); i++){
String fname = imageFileNames.get(i);
loadedImages.put(fname, GameWindow.readImage(fname));
}
}
catch(RuntimeException re){
//just in case... since students will be modifying the displayList
//should be accounted for via the isPainting flag/adjustFrameTiming()
}
}
//Draws the current background image underneath everything else in the game window
private void drawBackground(Graphics g){
//fill the window to draw the background color
g.setColor(bgColor);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(getForeground());
//draw the background image if there's one set
if (backgroundImage != null)
g.drawImage(backgroundImage, 0, 0, this.getWidth(), this.getHeight(),null);
}
//Draws the current splash image ontop of everything else in the game window
private void drawSplash(Graphics g){
//checks if there's currently a splash image set
if (splashImage != null)
g.drawImage(splashImage, 0, 0, this.getWidth(), this.getHeight(),null);
}
//Draws the hitbox around all of the images currently in the game window
//Only occurs if debug mode is enabled
private void drawHitboxes(Graphics g){
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(BORDER_THICKNESS));
g2D.setPaint(HITBOX_COLOR);
try{
for (Entity e : displayList){
//Draw hitboxes per each entity's current coordinates/dimensions
RectangularShape hitbox = new Rectangle2D.Double(e.getX(), e.getY(), e.getWidth(), e.getHeight());
g2D.draw(hitbox);
}
}
catch(RuntimeException re){
//just in case... since students will be modifying the arraylist
}
}
//Updates the debug text displayed when debug mode is enabled
public void updateDebugText(){
StringBuilder debugText = new StringBuilder(DEBUG_PREFIX + "DisplayList Size: " + displayList.size());
debugText.append(", Game Speed: " + this.gameSpeed + "%");
debugText.append(", Last Key Press: ");
if (debugLastKeyPressed == null)
debugText.append(" null ");
else
debugText.append(KeyEvent.getKeyText(debugLastKeyPressed.getKeyCode()));
debugText.append(", Last Mouse Click: ");
if (debugLastMouseEvent == null)
debugText.append("null");
else{
debugText.append("(x: " + debugLastMouseEvent.getX() + " y:" + debugLastMouseEvent.getY() + ")");
}
debugLabel.setText(debugText.toString());
}
//Adjusts the game timing to ensure that it doesn't run too fast or slow
//Makes sure the game maintains its target frames per second (FPS)
//1 frame = 1 "redraw" of the game window
public void adjustFrameTiming(){
//Wait for the game to finish painting
while(isPainting){
sleep(100);
}
//Figure out how much longer we need to wait to hit our target frametime
//ex: if we want 60 fps, we want to make sure there's a 1/60 second delay
//between each frame painted
long target = (TARGET_FRAMETIME * 100 / gameSpeed);
long delta = System.nanoTime() - lastPaintTime;
long delayNeeded = (target - delta);
sleep(delayNeeded);
//log the current system time for the next frame's timing
lastPaintTime = System.nanoTime();
}
//Forces the execution of the program to stop and wait
//For the specified number of nano seconds
private void sleep(long nanosToSleep){
if (nanosToSleep <= 0)
return;
long start = System.nanoTime();
while (System.nanoTime() - start < nanosToSleep){
try {
Thread.sleep(0, 5000);
}
catch(Exception e) {
//shouldn't ever reach here, but try/catch is necessary due to
//Java's implementation of Thread.sleep function
}
}
}
//Toggles the current state of Debug mode
public void setDebugMode(boolean isDebugEnabled){
this.isDebugEnabled = isDebugEnabled;
//Debug label is always visible and being drawn (to prevent rendering stutter),
//so when debug mode is disabled, this text is just set to an empty String.
if (!this.isDebugEnabled)
this.debugLabel.setText("");
}
//The game speed is represented as integer indicating a percentage.
//a value of 100 indicates the game is running at 100% speed, ie the default game speed.
//150 and 50 would indicate game running 50% faster and 50% slower, respectively.
public boolean isDebugEnabled(){
return this.isDebugEnabled;
}
//Gets and Sets the current game speed
//The game speed is represented as integer indicating a percentage.
//a value of 100 indicates the game is running at 100% speed, ie the default game speed.
//150 and 50 would indicate game running 50% faster and 50% slower, respectively.
public int getGameSpeed(){
return gameSpeed;
}
public void setGameSpeed(int newGameSpeed){
this.gameSpeed = newGameSpeed;
}
//Sets and retrieves the current background Color
//This background color won't be seen if there is a bg image (unless it has transparency)
public void setBackgroundColor(Color c){
this.bgColor = c;
}
public Color getBackgroundColor(){
return this.bgColor;
}
//Called automatically whenever the mouse is clicked inside the game window
//Updates the data of the last mouse event
public void mousePressed(MouseEvent event) {
lastMouseEvent = event;
//seperate variable used for debug text
debugLastMouseEvent = event;
}
//Returns the data of the last mouse click,
//or null if the mouse wasn't clicked since last check
public MouseEvent getLastMousePress(){
MouseEvent toReturn = lastMouseEvent;
//Set lastMouseEvent to null for subsequent calls to getLastMousePress
//to ensure that a mouse click isn't registered multiple times.
lastMouseEvent = null;
return toReturn;
}
//Returns a collection of all keys currently being pressed down
//Non-Movement keys are removed so they aren't re-registered on subsequent checks
//Movement keys only removed from keysPressed Set when they are released on the keyboard
public Collection<Integer> getKeysPressed(){
isFetchingKeys = true;
try{
ArrayList<Integer> toReturn = new ArrayList<Integer>(keysCurrentlyPressed);
HashSet<Integer> onlyMovementKeys = new HashSet<Integer>();
//create a copy of what's currently pressed to avoid concurrent modification
HashSet<Integer> currentPressCopy = new HashSet<Integer>(keysCurrentlyPressed);
isFetchingKeys = false;
//Remove any non-movement keys from the Set of keys currently pushed down
//so they are not registered on subsequent checks by the game
//Movement-specific keys remain in the Set to ensure smooth/reactive movement
for (Integer key : currentPressCopy){
if (movementKeys.contains(key))
onlyMovementKeys.add(key);
}
keysCurrentlyPressed = onlyMovementKeys;
return toReturn;
}
catch(RuntimeException re){
//just in case we get a one-in-a-million race condition between retrieving/fetching keys
isFetchingKeys=false;
return new ArrayList<Integer>();
}
}
//Called automatically whenever a key on the keyboard is pushed down/released
public void keyPressed(KeyEvent event) {
//Wait for the game to finish retrieving keys pressed
while(isFetchingKeys){
sleep(100);
}
keysCurrentlyPressed.add(event.getKeyCode());
debugLastKeyPressed = event;
int keyCode = event.getKeyCode();
if (keyCode == GameEngine.KEY_QUIT_GAME)
System.exit(0);
else if (keyCode == GameEngine.KEY_TOGGLE_DEBUG)
this.setDebugMode(!this.isDebugEnabled());
}
public void keyReleased(KeyEvent event) {
keysCurrentlyPressed.remove(event.getKeyCode());
}
//Get and set current background image
//bgImageFile is null if no background image is set
public void setBackgroundImage(String filename){
bgImageFile = filename;
backgroundImage = GameWindow.readImage(bgImageFile);
}
public String getBackgroundImage(){
return bgImageFile;
}
//Get and set current splash image
//splashImageFile is null if no splash image is set
public void setSplashImage(String filename){
splashImage = GameWindow.readImage(filename);
splashImageFile = filename;
}
public String getSplashImage(){
return splashImageFile;
}
//Loads an image into memory -- returns the Image object
public static Image readImage(String filename){
if (filename == null)
return null;
//need to use URL to allow for animated gifs to animate properly
URL imageResource = GameWindow.class.getResource(filename);
if (imageResource == null){
System.err.println("Unable to read image file: " + filename);
return null;
}
return new ImageIcon(imageResource).getImage();
}
//Sets the title text on the top of the game window
public void setTitle(String text){
window.setTitle(text);
}
//These functions are required by various interfaces, but are not used
public void mouseReleased(MouseEvent event) { }
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mouseMoved(MouseEvent event) { }
public void keyTyped(KeyEvent event) {}
}