-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSJGL.java
More file actions
583 lines (499 loc) · 14.2 KB
/
SJGL.java
File metadata and controls
583 lines (499 loc) · 14.2 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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import java.awt.*;
import javax.swing.*;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
/**
* Simple Java Game Library based on:
* https://www3.ntu.edu.sg/home/ehchua/programming/java/J4b_CustomGraphics.html
*/
public class SJGL extends JFrame
{
// Define window attributes
private int canvasWidth = 1024;
private int canvasHeight = 768;
private int updateInterval = 20; // milliseconds
private int fps = 50;
private String title = "SJGL Game by @TokyoEdtech";
// dt (delta time) to keep framerates consistent
private double dt = 0.02;
private DrawCanvas canvas; // the drawing canvas (using an inner class extends JPanel)
// Sprites
private ArrayList<Sprite> sprites = new ArrayList<Sprite>();
// Background Sprites
private ArrayList<Sprite> backgroundSprites = new ArrayList<Sprite>();
// Labels
private ArrayList<Label> labels = new ArrayList<Label>();
// Currently pressed keys
private Set<Integer> keys = new HashSet<Integer>();
// Mouse presses
private boolean isMousePressed = false;
private int mouseX = 0;
private int mouseY = 0;
// Camera
private Camera camera;;
// Attributes
private Color backgroundColor = Color.BLACK;
private int gravity = 0;
// Controls repaint and game updates
private Timer timer;
// Used to prevent updates while rendering
public boolean isRendering = false;
/**
* Class constructor.
*/
public SJGL()
{
this.run();
}
/**
* Class constructor specifying the tile displayed in the window.
* Default canvas size is 1024 x 768
* @param title the title to be displayed
*/
public SJGL(String title)
{
this.title = title;
this.run();
}
/**
* Class constructor specifying the tile displayed in the window and canvas width and height.
* @param title the title to be displayed
* @param canvasWidth width of the camvas in pixels
* @param canvasHeight height of the canvas in pixels
*/
public SJGL(String title, int canvasWidth, int canvasHeight)
{
this.title = title;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
this.run();
}
// Setup the GUI components and event handlers
public void run()
{
canvas = new DrawCanvas();
canvas.setDoubleBuffered(true);
canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
canvas.setFocusable(true);
canvas.requestFocusInWindow();
this.setContentPane(canvas);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.pack();
this.setTitle(this.title);
this.setVisible(true);
// Add KeyListener to canvas
canvas.addKeyListener(new KeyListener()
{
@Override
public void keyPressed(KeyEvent e){
registerKeyPress(e.getKeyCode());
}
@Override
public void keyTyped(KeyEvent e)
{
}
@Override
public void keyReleased(KeyEvent e)
{
registerKeyRelease(e.getKeyCode());
}
});
// MouseListener to canvas
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
isMousePressed = true;
}
@Override
public void mouseReleased(MouseEvent e) {
isMousePressed = false;
}
});
// Create a timer to repaint, and update sprites
timer = new Timer(updateInterval, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
if(!isRendering)
{
// Update sprites
for(int i=sprites.size()-1;i>-1;i--)
{
// Add gravity
Sprite sprite = sprites.get(i);
sprite.setDY(sprite.getDY() + gravity);
sprite.update(canvasWidth, canvasHeight, dt);
}
}
if(camera != null)
{
camera.update();
}
// Refresh mouse coordinates
Point point = MouseInfo.getPointerInfo().getLocation();
Point topLeft = canvas.getLocationOnScreen();
mouseX = point.x - topLeft.x;
mouseY = point.y - topLeft.y;
// System.out.println(mouseX + " " + mouseY);
}
});
timer.start();
}
// Define Inner class DrawCanvas, which is a JPanel used for custom drawing
class DrawCanvas extends JPanel {
final SJGL sjgl = SJGL.this;
@Override
public void paintComponent(Graphics g) {
try
{
if(!sjgl.isRendering)
{
super.paintComponent(g); // paint parent's background
}
}
catch(Exception e)
{
}
// Render game sprites
if(!sjgl.isRendering)
{
// System.out.print(" RENDER ");
render(g);
}
// This avoids random slowdowns on Linux
Toolkit.getDefaultToolkit().sync();
}
private void render(Graphics g)
{
// System.out.print("T");
sjgl.isRendering = true;
setBackground(backgroundColor);
// Render Background Sprites
// Render in order (background in back)
// Background sprites do not follow the camera
for(int i=0;i<backgroundSprites.size();i++)
{
Sprite backgroundSprite = backgroundSprites.get(i);
backgroundSprite.render(g, null, canvasWidth, canvasHeight);
}
// Render Sprites
// Render in reverse (player in front)
for(int i=sprites.size()-1;i>-1;i--)
{
Sprite sprite = sprites.get(i);
sprite.render(g, camera, canvasWidth, canvasHeight);
}
// Render Text
// Render in order
for(int i=0;i<labels.size();i++)
{
Label label = labels.get(i);
label.render(g);
}
sjgl.isRendering = false;
// System.out.print("F");
}
}
// Public methods
/**
* Add a sprite to ArrayList sprites.
* @param sprite the sprite to be added
* @see Sprite
*/
public void addSprite(Sprite sprite)
{
while(isRendering)
{
sleep(1);
}
sprites.add(sprite);
}
/**
* Add a background sprite to ArrayList backgroundSprites.
* @param sprite the sprite to be added
* @see Sprite
*/
public void addBackgroundSprite(Sprite sprite)
{
while(isRendering)
{
sleep(1);
}
backgroundSprites.add(sprite);
}
/**
* Add a label to ArrayList labels.
* @param label the label to be added
* @see Label
*/
public void addLabel(Label label)
{
while(isRendering)
{
sleep(1);
}
labels.add(label);
}
/**
* Remove a sprite from ArrayList sprites.
* @param sprite the sprite to be removed
* @see Sprite
*/
public void removeSprite(Sprite sprite)
{
while(isRendering)
{
sleep(1);
}
sprites.remove(sprite);
}
/**
* Remove a background sprite from ArrayList backgroundSprites.
* @param sprite the sprite to be removed
* @see Sprite
*/
public void removeBackgroundSprite(Sprite sprite)
{
while(isRendering)
{
sleep(1);
}
backgroundSprites.remove(sprite);
}
/**
* Remove a label from ArrayList labels.
* @param label the label to be removed
* @see Label
*/
public void removeLabel(Label label)
{
while(isRendering)
{
sleep(1);
}
labels.remove(label);
}
/**
* Set the background color.
* @param color the color value
* @see java.awt.Color;
*/
public void setBackgroundColor(Color color)
{
this.backgroundColor = color;
}
/**
* Set global gravity value.
* @param gravity the gravity value
*/
public void setGravity(int gravity)
{
this.gravity = gravity;
}
/**
* Set the game camera.
* @param camera the camera
* @see Camera
*/
public void setCamera(Camera camera)
{
this.camera = camera;
}
/**
* Set the target of the camera.
* @param target the sprite for the camera to follow
* @see Sprite
*/
public void setCameraTarget(Sprite target)
{
if(this.camera == null)
{
this.camera = new Camera(target);
}
else
{
this.camera.setTarget(target);
}
}
/**
* Set the camera to an x/y location on the screen.
* @param x the x coordinate
* @param y the y coordinate
*/
public void setCameraTarget(int x, int y)
{
if(this.camera == null)
{
this.camera = new Camera(new Sprite(x, y));
}
else
{
this.camera.setTarget(x, y);
}
}
/**
* Returns the width of the canvas.
* @return the canvas width in pixels
*/
public int getCanvasWidth()
{
return this.canvasWidth;
}
/**
* Returns the height of the canvas.
* @return the canvas height in pixels
*/
public int getCanvasHeight()
{
return this.canvasHeight;
}
/**
* Returns the current delta time.
* @return delta time
*/
public double getDT()
{
return this.dt;
}
// Keyboard
/**
* Adds key pressed to Set keys.
* @param key that is pressed
* @see java.awt.event.KeyEvent
*/
private void registerKeyPress(int key)
{
keys.add(key);
}
/**
* Removes key pressed from Set keys.
* @param key that is released
* @see java.awt.event.KeyEvent
*/
private void registerKeyRelease(int key)
{
keys.remove(key);
}
/**
* Indicates if a particular key is currently pressed.
* @return true if pressed / false if not currently pressed
*/
public boolean isKeyPressed(int key)
{
if(keys.contains(key))
{
return true;
}
return false;
}
/**
* Indicates if the mouse pointer is currently over a particular sprite.
* @return true if mouse is over / false if not over
*/
public boolean isMouseOverSprite(Sprite sprite)
{
boolean xRange = (sprite.getX() < mouseX) && (mouseX < (sprite.getX() + sprite.getWidth()));
boolean yRange = (sprite.getY() < mouseY) && (mouseY < (sprite.getY() + sprite.getHeight()));
return xRange && yRange;
}
// Mouse
/**
* Indicates if the mouse is currently pressed.
* @return true if pressed / false if not currently pressed
*/
public boolean getIsMousePressed()
{
return isMousePressed;
}
/**
* Returns the x coordinate of the mouse.
* @return x coordinate of the mouse
*/
public int getMouseX()
{
return mouseX;
}
/**
* Returns the y coordinate of the mouse.
* @return y coordinate of the mouse
*/
public int getMouseY()
{
return mouseY;
}
/**
* Returns the game camera.
* @return the game camera
* @see Camera
*/
public Camera getCamera()
{
return camera;
}
/**
* Returns the ArrayList sprites.
* @return an ArrayList containing all game sprites
* @see Sprite
*/
public ArrayList<Sprite> getSprites()
{
// Return copy to avoid concurrentModificationException
return new ArrayList<Sprite>(sprites);
}
/**
* Returns the ArrayList backgroundSprites.
* @return an ArrayList containing all game background sprites
* @see Sprite
*/
public ArrayList<Sprite> getBackgroundSprites()
{
// Return copy to avoid concurrentModificationException
return new ArrayList<Sprite>(backgroundSprites);
}
/**
* Returns the ArrayList labels.
* @return an ArrayList containing all game labels
* @see Label
*/
public ArrayList<Label> getLabels()
{
// Return copy to avoid concurrentModificationException
return new ArrayList<Label>(labels);
}
/**
* Sets the frames per second of the game.
* @param fps the target frames per second
*/
// Set FPS (also updates updateInterval and dt
public void setFPS(int fps)
{
this.fps = fps;
this.updateInterval = (int)(1000.0 / fps);
this.dt = updateInterval / 1000.0;
this.timer.setDelay(this.updateInterval);
}
/**
* Helper method to pause changes to ArrayLists while rendering.
*/
private void sleep(int milliseconds)
{
// System.out.print("* ");
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
}
}
}