-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGamePanel.java
More file actions
326 lines (240 loc) · 9.97 KB
/
GamePanel.java
File metadata and controls
326 lines (240 loc) · 9.97 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
//Justin Baldeosingh
//816021226
//COMP 3609 - Assignment 2
import javax.swing.JPanel;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
/**
A component that displays all the game entities
*/
public class GamePanel extends JPanel {
// Game Panel Entity Constants
private static int NUM_STARS = 3;
private static int NUM_MISSILES = 4;
//Declares the main game entities.
private Missile[] missiles;
private City city;
private Cannon cannon;
private Paratrooper paratrooper;
private Jet jet;
//Declares the sound manager for playing sound clips.
private SoundManager soundManager;
//Declares the main game thread.
private GameThread gameThread;
//Declares image for the background and image context.
private BufferedImage image;
private Image backgroundImage;
//Declares the animations for the game.
private Animation[] missileDetonation;
private Animation fireAnimation;
//Declares the effects for the entities.
private RotateFX[] twinklingStars;
private DisintegrateFX[] missileDestroyed;
//Declares a global scoreboard variable for holding the main scoreboard.
private Scoreboard scoreboard;
//Sets the gameStarted status to false.
public boolean gameStarted = false;
public GamePanel (Scoreboard scoreboard) {
//Sets the sound manager to the single instance sound manager.
soundManager = SoundManager.getInstance();
//Sets the main scoreboard.
this.scoreboard = scoreboard;
//Loads the background image for hte game.
backgroundImage = ImageManager.loadImage ("images/Background.jpg");
//Initializes the main drawing context for double buffering rendering.
image = new BufferedImage (612, 408, BufferedImage.TYPE_INT_RGB);
//Declares arrays for missile detonation animations.
missileDetonation = new Animation[NUM_MISSILES];
//Declares arrays for rotating effects and disintegration effects used on stars and missiles.
twinklingStars = new RotateFX[NUM_STARS];
missileDestroyed = new DisintegrateFX[NUM_MISSILES];
//Declares an array for storing an array of missiles.
missiles = new Missile[NUM_MISSILES];
//Loads the animations from the stripfiles.
loadFireAnimation();
loadExplosionAnimation();
}
private void createGameEntities() {
//Creates the city entity.
city = new City(this, 0, 292, scoreboard);
//Creates the laser cannon entity.
cannon = new Cannon(this, 230, 250);
//Creates the jet entity.
jet = new Jet(this, 0, 0);
//Creates the paratrooper entity.
paratrooper = new Paratrooper(this, city);
//Creates the rotating stars effects.
for(int i = 0; i < NUM_STARS; i++){
twinklingStars[i] = new RotateFX(this);
}
//Creates the disintegration effects.
for(int i = 0; i < NUM_MISSILES; i++){
missileDestroyed[i] = new DisintegrateFX(this);
}
//Creates the missiles and assigns them their detonation and destroyed effects.
for(int i = 0; i < NUM_MISSILES; i++){
missiles[i] = new Missile(this, 50+i*100, 15+i*25, city, missileDetonation[i], missileDestroyed[i]);
}
//Places a fire animation on the city.
fireAnimation.start(100,270, true);
}
//Loads the fire animation from the stripfile and assigns it.
public void loadFireAnimation() {
Image stripImage = ImageManager.loadImage("images/fire.png");
int imageWidth = (int) stripImage.getWidth(null) / 10;
int imageHeight = stripImage.getHeight(null) / 6;
fireAnimation = new Animation(this);
for (int i=0; i<6; i++) {
for(int j=0; j<10; j++) {
BufferedImage frameImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) frameImage.getGraphics();
g.drawImage(stripImage,
0, 0, imageWidth, imageHeight,
j * imageWidth, i*imageHeight, (j * imageWidth) + imageWidth, (i*imageHeight)+imageHeight,
null);
fireAnimation.addFrame(frameImage, 50);
}
}
}
//Loads the explosion animation from the stripfile and assigns it.
public void loadExplosionAnimation() {
Image stripImage = ImageManager.loadImage("images/explosion.png");
int imageWidth = (int) stripImage.getWidth(null) / 5;
int imageHeight = stripImage.getHeight(null) / 4;
//Creates the animations for the missile detonations.
for(int i = 0; i < NUM_MISSILES; i++){
missileDetonation[i] = new Animation(this);
}
for (int i=0; i<4; i++) {
for(int j=0; j<5; j++) {
BufferedImage frameImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) frameImage.getGraphics();
g.drawImage(stripImage,
0, 0, imageWidth, imageHeight,
j * imageWidth, i*imageHeight, (j * imageWidth) + imageWidth, (i*imageHeight)+imageHeight,
null);
//Loads the frames for the explosion into the missile detonation animation objects.
for(int k = 0; k < NUM_MISSILES; k++){
missileDetonation[k].addFrame(frameImage, 50);
}
}
}
}
//Given an x and y coordinate, fires the cannon at the coordinates.
public boolean targetSky (int x, int y) {
boolean onMissile = false;
boolean hitParatrooper = false;
//Updates the cannon animation.
cannon.fireCannon(x, y);
//Determines if the paratrooper is shot down.
paratrooper.shotDown(x, y);
//Checks to see if any of the missiles have been clicked on.
for(int i = 0; i < NUM_MISSILES; i++){
boolean hitMissile = missiles[i].missileClicked(x, y);
if(hitMissile) {
onMissile = true;
}
}
//Returns whether any missile was clicked.
return onMissile;
}
public void startGame() { // initialise and start the game thread
Thread thread;
if (gameThread == null) {
soundManager.playClip ("background", true);
createGameEntities();
gameStarted = true;
gameThread = new GameThread (this);
thread = new Thread (gameThread);
thread.start();
}
}
public void restartGame() { // initialise and start a new game thread
Thread thread;
if (gameThread == null || !gameThread.isRunning()) {
soundManager.playClip ("background", true);
createGameEntities();
gameThread = new GameThread (this);
gameStarted = true;
thread = new Thread (gameThread);
thread.start();
}
}
public void pauseGame() { // pause the game (don't update game entities)
gameThread.pauseGame();
}
public void endGame() { // end the game thread
//If the game is running and the city object is initialized, display the game over screen and end the game once the game has ended.
if(gameThread != null) {
if(city != null)
city.gameOver();
gameThread.endGame();
gameStarted = false;
soundManager.stopClip("background");
}
}
//Updates the entities throughout the game.
public void gameUpdate () {
//Updates the stars.
for(int i = 0; i < NUM_STARS; i++){
twinklingStars[i].update();
}
//Updates the fire animations.
fireAnimation.update();
//Moves the jet
jet.move();
//Obtains 'checkpoints' to determine when the fire particular events.
int difficultyStage = scoreboard.getScore() / 1000;
int dispenseLife = scoreboard.getScore() % 800;
//Displays and moves the paratrooper every 800 score the user obtains.
if(dispenseLife == 0 && scoreboard.getScore() != 0) {
paratrooper.showParatrooper(jet.getJetX(), 0);
}
paratrooper.move();
//Updates the missile animations and the missile movement.
for (int i = 0; i< NUM_MISSILES; i++) {
missileDestroyed[i].update();
missiles[i].move();
missileDetonation[i].update();
missiles[i].updateDifficulty(difficultyStage);
}
}
public void gameRender () { // draw the game objects
Graphics2D imageContext = (Graphics2D) image.getGraphics();
imageContext.drawImage(backgroundImage, 0, 0, null); // draw the background image
if (city != null) {
city.draw(imageContext);
}
if(jet != null){
jet.draw(imageContext);
}
if (cannon != null) {
cannon.draw(imageContext);
}
for(int i = 0; i < NUM_STARS; i++){
twinklingStars[i].draw(imageContext);
}
for(int i = 0; i < NUM_MISSILES; i++){
missileDestroyed[i].draw(imageContext);
}
fireAnimation.draw(imageContext);
if (missiles != null) {
for (int i = 0; i< NUM_MISSILES; i++) {
missiles[i].draw(imageContext);
if (missileDetonation[i] != null)
missileDetonation[i].draw(imageContext);
}
}
if(paratrooper != null){
paratrooper.draw(imageContext);
}
Graphics2D g2 = (Graphics2D) getGraphics(); // get the graphics context for the panel
g2.drawImage(image, 0, 0, 612, 408, null);
imageContext.dispose();
g2.dispose();
//If the number of lives of the city is 0 or less, end the game.
if(city.getLives() <= 0)
endGame();
}
}