-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParatrooper.java
More file actions
152 lines (117 loc) · 4.63 KB
/
Paratrooper.java
File metadata and controls
152 lines (117 loc) · 4.63 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
//Justin Baldeosingh
//816021226
//COMP 3609 - Assignment 2
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Random;
public class Paratrooper {
//Sets the drawing panel to the global panel variable.
private JPanel panel;
//Declares the coordinate variables of the paratrooper.
private int x;
private int y;
//Declares the width and height dimensions of the paratrooper.
private int width;
private int height;
//Declares the image used for storing the missile paratrooper.
private Image paratrooperImage;
private int dx; // increment to move along x-axis
private int dy; // increment to move along y-axis
//Declares the rectangle for storing the paratrooper hitbox.
private Rectangle2D.Double paratrooper;
//Declares the city object for representing the player.
private City city;
//Declares the soundmanager for the single instance sound manager.
private SoundManager soundManager;
//Used to determine if the paratrooper is shown currently.
private boolean paratrooperShown;
public Paratrooper(JPanel p, City city) {
//Loads the main panel, to be used as the drawing context.
panel = p;
//Sets the dimensions of the paratrooper.
width = 75;
height = 75;
//Gets a handle on the main city representing the player's objective.
this.city = city;
//Loads the paratrooper image sprite.
paratrooperImage = ImageManager.loadImage ("images/paratrooper.png");
//Sets the single instance sound manager.
soundManager = SoundManager.getInstance();
//Hides the paratrooper at the start of the game.
hideParatrooper();
}
//Hides the paratrooper by moving it out of the game view and setting its motion to 0.
public void hideParatrooper() {
x = 0 - width;
y = 0 - height;
dx = 0;
dy = 0;
paratrooperShown = false;
}
//Shows the paratrooper by setting its starting position and downward movement.
public void showParatrooper(int x, int y) {
if(!paratrooperShown) {
this.x = x;
this.y = y;
dx = 0;
dy = 8;
paratrooperShown = true;
}
}
public void draw (Graphics2D g2) {
g2.drawImage(paratrooperImage, x, y, width, height, null);
}
//Moves the paratrooper
public void move() {
if (!panel.isVisible ())
return;
//Updates the x and y coordinates of the paratrooper as the paratrooper move.
x = x + dx;
y = y + dy;
//Ensures that the paratrooper does not leave the screen boundaries when spawned from the jet.
if(x <= 0){
x = 0;
} else if(x + width > panel.getWidth()){
x = panel.getWidth() - width;
}
//Determines if the paratrooper has collided with the city.
boolean collision = collidesWithCity();
//If the paratrooper has collided with the city, hide the paratrooper.
if (collision){
hideParatrooper();
}
}
//Determines if the user has clicked on the paratrooper.
public boolean shotDown (int x, int y) {
//Gets the hitbox of the paratrooper.
paratrooper = getBoundingRectangle();
//If the paratrooper has not been initialized, then return false.
if (paratrooper == null)
return false;
//Determines if the paratrooper hitbox contains the coordinates that correspond to the user's click.
boolean onParatrooper = paratrooper.contains(x, y);
//If the user has clicked on the paratrooper and the game is not over,
//play the 'Life Up' sound effect, add 100 to the user's score, and hide the paratrooper.
if (onParatrooper && !city.isGameOver()){
soundManager.playClip("lifeUp", false);
int score = city.getScore();
city.setScore(score + 100);
int lives = city.getLives();
city.setLives(lives + 1);
hideParatrooper();
}
//Returns the outcome of clicking on the paratrooper.
return onParatrooper;
}
//Returns the rectangular hit-box of the paratrooper.
public Rectangle2D.Double getBoundingRectangle() {
return new Rectangle2D.Double (x, y, width, height);
}
//Returns the outcome of the paratrooper colliding with the city.
public boolean collidesWithCity() {
Rectangle2D.Double paratrooperRect = getBoundingRectangle();
Rectangle2D.Double cityRect = city.getBoundingRectangle();
return paratrooperRect.intersects(cityRect);
}
}