-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGDV5.java
252 lines (184 loc) · 5.41 KB
/
GDV5.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
import java.awt.Canvas;
import java.awt.Color;
/**
* @(#)GameDriverV4.java
*
*
* updates V4: jframe included, keylistener included, switch to render, game loop
* from tasktimer to thread - needs more testing sorry
* Updates V5: keyTyped, switched to update and render
*
* Possible Changes for V6: full-screen (win.scale)
*
* @version 5.0 9/13/2019
*/
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public abstract class GDV5 extends Canvas implements Runnable, KeyListener {
private int FramesPerSecond;
public static boolean[] KeysPressed;
public static boolean[] KeysReleased;
private static int MAX_WINDOW_X = 900;
private static int MAX_WINDOW_Y = 800;
private static int PADDING = 2;
// it is your responsibility to handle the release on keysTyped
public static boolean[] KeysTyped;
private JFrame frame;
private String title = "Breakout";
private boolean cleanCanvas = true;
public GDV5(int frames) {
// set up all variables related to the game
FramesPerSecond = frames;
this.addKeyListener(this);
// key setup
KeysPressed = new boolean[KeyEvent.KEY_LAST];
KeysReleased = new boolean[KeyEvent.KEY_LAST];
KeysTyped = new boolean[KeyEvent.KEY_LAST];
}
public GDV5() {
// default setting (60 frames per second)
this(60);
this.setBackground(Color.BLACK);
}
public void start() {
if (this.getWidth() == 0) {
this.setSize(MAX_WINDOW_X, MAX_WINDOW_Y); // CANVAS SIZE
}
frame = new JFrame();
frame.add(this);
frame.pack();
frame.setTitle(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
this.startThread();
}
private synchronized void startThread() {
Thread t1 = new Thread(this);
t1.start(); // calls run method after paint
this.setFocusable(true);
}
public void setFrames(int num) {
this.FramesPerSecond = num;
}
public abstract void update();
public abstract void draw(Graphics2D win);
private void render() {
BufferStrategy buffs = this.getBufferStrategy();
if (buffs == null) {
this.createBufferStrategy(3);
buffs = this.getBufferStrategy();
}
Graphics g = buffs.getDrawGraphics();
if (this.cleanCanvas) {
g.setColor(this.getBackground());
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}
draw((Graphics2D) g);
g.dispose();
buffs.show();
}
public void run() {
long lastTime = System.nanoTime(); // long 2^63
double nanoSecondConversion = 999998888.0 / this.FramesPerSecond; // 60 frames per second //1000000000
double changeInSeconds = 0;
while (true) {
long now = System.nanoTime();
changeInSeconds += (now - lastTime) / nanoSecondConversion;
while (changeInSeconds >= 1) {
update();
changeInSeconds--;
}
render();
lastTime = now;
}
}
public BufferedImage addImage(String name) {
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResourceAsStream(name));
} catch (IOException e) {
System.out.println(e);
}
return img;
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
KeysPressed[e.getKeyCode()] = true;
KeysReleased[e.getKeyCode()] = false;
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
KeysPressed[e.getKeyCode()] = false;
KeysReleased[e.getKeyCode()] = true;
KeysTyped[e.getKeyCode()] = true;
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
/*
* Returns the direction of collision (0 = right, 1 - top, 2 - left , 3 -
* bottom). stationary - the object we are colliding into projectile - the
* object that is moving dx = projectile's x displacement dy = projectile's y
* displacement
*/
public static int collisionDirection(Rectangle stationary, Rectangle projectile, int dx, int dy) {
// calculate previous location
int previousXPos = (int) projectile.getX() - dx;
int previousYPos = (int) projectile.getY() - dy;
int height = (int) projectile.getHeight();
int width = (int) projectile.getWidth();
int result = 0; // default intersects from right
if (previousYPos + height <= stationary.getY() && projectile.getMaxY() >= stationary.getY()) {
// intersects from top
result = 1;
} else if (previousXPos + width <= stationary.getX() && projectile.getX() + width >= stationary.getX()) {
// intersects from left
result = 2;
} else if (previousYPos >= stationary.getY() + stationary.height
&& projectile.getY() <= stationary.getY() + stationary.height) {
// intersects from bottom
result = 3;
}
return result;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public void setCleanCanvas(boolean option) {
this.cleanCanvas = option;
}
public static int getMaxWindowX() {
return MAX_WINDOW_X;
}
public static int getMaxWindowY() {
return MAX_WINDOW_Y;
}
public static void setMaxWindowX(int sizeX) {
MAX_WINDOW_X = sizeX;
}
public static void setMaxWindowY(int sizeY) {
MAX_WINDOW_Y = sizeY;
}
public static int getPadding() {
return PADDING;
}
public static void setPadding(int paddingVal) {
PADDING = paddingVal;
}
}