-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScientist.java
204 lines (183 loc) · 7.79 KB
/
Scientist.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
/*
* Scientist.java
* Ingrid and Isabel Crant
* Harmless side characters mainly used for aesthetic purposes in the Jetpack Joyride game. They faint when they touch an obstacle or if Barry passes by them. Some crouch to avoid Barry and dumber ones keep walking.
*/
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
public class Scientist extends Rectangle {
public static final int LEFT = 0, RIGHT = 1;
private final int BLANK = 0x00000000;
private BufferedImage scientistWalking1 = JetpackJoyridePanel.loadBuffImg("scientist_stationary.png");
private BufferedImage scientistWalking2 = JetpackJoyridePanel.loadBuffImg("scientist_moving.png");
private BufferedImage scientistCrouching = JetpackJoyridePanel.loadBuffImg("scientist_crouching.png");
private BufferedImage scientistFainting = JetpackJoyridePanel.loadBuffImg("scientist_fainting.png");
private BufferedImage scientistFaintingUpsideDown = JetpackJoyridePanel.loadBuffImg("scientist_fainting_upsidedown.png");
private int x, y;
private int dir;
private static final int speed = 5;
private int width, height;
private static boolean isMoving = true;
private boolean walking, crouching, fainting; // the scientist's different phases: walking, crouching, fainting
private boolean flipped = false; // if the scientist's image needs to be flipped upside down
private boolean isAbleToCrouch = false;
private boolean canPlayFaintingSound = true;
private static int maxWalkingPoseCount = 4; // tells when to reset walking pose count back to 0
private int walkingPoseCount = 0; // tells when to switch barry's legs when walking (0, 1 - one leg forward, 2, 3 - other leg forward)
public Scientist(int ddir, int ccrouch) {
super();
walking = true;
crouching = false;
fainting = false;
dir = ddir;
// only allows some of the scientist to crouch:
if(ccrouch == 0) {
isAbleToCrouch = true;
}
if(dir == RIGHT) {
// flips the images to face right:
scientistWalking1 = JetpackJoyridePanel.flipImage(scientistWalking1);
scientistWalking2 = JetpackJoyridePanel.flipImage(scientistWalking2);
scientistCrouching = JetpackJoyridePanel.flipImage(scientistCrouching);
scientistFainting = JetpackJoyridePanel.flipImage(scientistFainting);
scientistFaintingUpsideDown = JetpackJoyridePanel.flipImage(scientistFaintingUpsideDown);
}
width = scientistWalking1.getWidth(null);
height = scientistWalking1.getHeight(null);
x = JetpackJoyridePanel.WIDTH;
y = JetpackJoyridePanel.HEIGHT - height - 100;
setBounds(x, y, width, height);
}
// Getter and setter methods:
public int getHitByLaserFallingDirection() { // if the scientist hits a laser, they fall in the opposite direction
if(dir == RIGHT) {
return LEFT;
}
else {
return RIGHT;
}
}
public int getDir() {
return dir;
}
protected Rectangle getCollision(Rectangle rect2) {
Area a1 = new Area(this);
Area a2 = new Area(rect2);
a1.intersect(a2);
return a1.getBounds();
}
public void crouch() { // makes scientist crouch
walking = false; crouching = true; fainting = false;
}
public void walk() { // makes scientist walk
walking = true; crouching = false; fainting = false;
}
public void faint(int direction) { // makes scientist faint
if(dir == direction) {
flipped = true;
}
walking = false; crouching = false; fainting = true;
playFaintingSound();
}
public BufferedImage getImage() {
if(walking) {
if(walkingPoseCount > maxWalkingPoseCount/2) { // one leg forward
return scientistWalking1;
}
else { // other leg forward
return scientistWalking2;
}
} else if (crouching) {
return scientistCrouching;
} else {
if(flipped) {
return scientistFaintingUpsideDown;
}
else {
return scientistFainting;
}
}
}
public boolean isFainted() {
return fainting;
}
public boolean canCrouch() {
return isAbleToCrouch;
}
public static void stopMoving() {
isMoving = false;
}
// plays the sound of the scientist fainting:
public void playFaintingSound() {
if(canPlayFaintingSound) {
SoundPlayer.playSoundEffect(SoundPlayer.scientistFainting, 0);
canPlayFaintingSound = false;
}
}
public boolean collidesWith(Zapper zapper) {
if (intersects(zapper)) { // checks if the boundaries intersect
Rectangle intersectBounds = getCollision(zapper); // calculates the collision overlay
if (!intersectBounds.isEmpty()) {
// Check all the pixels in the collision overlay to determine
// if there are any non-alpha pixel collisions...
for (int x = intersectBounds.x; x < intersectBounds.x + intersectBounds.width; x++) {
for (int y = intersectBounds.y; y < intersectBounds.y + intersectBounds.height; y++) {
int scientistPixel = getImage().getRGB(x - (int) getX(), y - (int) getY());
int zapperPixel = zapper.getImage().getRGB(x - (int)zapper.getX(), y - (int)zapper.getY());
// 255 is completely transparent, you might consider using something
// a little less absolute, like 225, to give you a sligtly
// higher hit right, for example...
if (scientistPixel != BLANK && zapperPixel != BLANK) {
return true;
}
}
}
}
}
return false;
}
public void move() {
int dx;
if(walking) { // if the scientist is walking
if(dir == LEFT) { // if the scientist is facing left
dx = JetpackJoyridePanel.speedX-speed; // the scientist moves left
}
else { // if the scientist is facing right
dx = JetpackJoyridePanel.speedX+speed; // the scientist moves right
}
}
else { // if the scientist isn't walking
dx = JetpackJoyridePanel.speedX; // the scientist moves with the background (looks like they are stationary)
}
translate(dx, 0);
x += dx;
}
public void draw(Graphics g) {
if(walking) {
if(isMoving) {
walkingPoseCount++;
}
if(walkingPoseCount > maxWalkingPoseCount/2) {
g.drawImage(scientistWalking1, x, y, null);
}
else {
g.drawImage(scientistWalking2, x, y, null);
}
if(walkingPoseCount > maxWalkingPoseCount) {
walkingPoseCount = 0;
}
}
else if(crouching) {
g.drawImage(scientistCrouching, x, y, null);
}
else if(fainting) {
if(flipped) {
g.drawImage(scientistFaintingUpsideDown, x, y, null);
}
else {
g.drawImage(scientistFainting, x, y, null);
}
}
}
}