-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ghost.java
293 lines (253 loc) · 8.79 KB
/
Ghost.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
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
package PacmanPack;
import java.awt.image.BufferedImage;
import static PacmanPack.Pacman.Type.WALL;
import static PacmanPack.Pacman.pacmanIsDying;
import static PacmanPack.Pacman.standStill;
import static PacmanPack.SpriteMap.getSprite;
public abstract class Ghost extends Sprite {
protected Cell homeCell1;
protected Cell homeCell2;
protected Cell homeCell;
protected Cell ghostHouse;
protected int frightenedStage = 0;
protected boolean isFrightened = false;
protected int danceStance;
protected boolean isEaten = false;
protected Cell randomCell = null;
protected boolean isBlinking;
protected abstract Cell chaseStrategy();
protected abstract Cell scatterStrategy();
public void setEaten(boolean eaten) {
isEaten = eaten;
}
@Override
public Cell move(boolean chase) {
locateSpriteCell();
checkIfHomeCell();
boolean canMoveBack = false;
Cell targetCell = randomCell;
Cell nextCell = null;
if (dancingInGhostHouse && isFrightened) {
startFrightenedAnimation();
targetCell = spriteCell;
} else if (dancingInGhostHouse) {
if (spriteFacing == Pacman.Facing.UP) {
startAnimation(normalDanceAnimation[2]);
} else if (spriteFacing == Pacman.Facing.DOWN) {
startAnimation(normalDanceAnimation[3]);
}
targetCell = spriteCell;
} else if (standStill || pacmanIsDying) {
targetCell = spriteCell;
} else if (isEaten) {
targetCell = ghostHouse;
canMoveBack = true;
} else if (isFrightened) {
if (spriteCell != prevCell || targetCell == null) {
targetCell = moveToRandom();
randomCell = targetCell;
canMoveBack = false;
}
} else if (!chase) {
targetCell = scatterStrategy();
} else {
targetCell = chaseStrategy();
}
if (spriteCell != prevCell) {
Pathfinder.findPath(spriteCell, prevCell, targetCell, spriteLevel, canMoveBack);
prevCell = spriteCell;
}
nextCell = findNextCell(targetCell, spriteLevel);
setDance(nextCell);
moveToNextCell(nextCell);
return targetCell;
}
protected Cell moveToRandom() {
Cell nextCell = spriteCell;
do {
int rand4 = (int) (Math.random() * ((3) + 1));
if (rand4 == 0) nextCell = spriteCell.getLeft();
if (rand4 == 1) nextCell = spriteCell.getRight();
if (rand4 == 2) nextCell = spriteCell.getUp();
if (rand4 == 3) nextCell = spriteCell.getDown();
} while (nextCell.getType() == WALL && nextCell != prevCell);
return nextCell;
}
protected void startFrightenedAnimation() {
setNormalOrEatenAnimation(0);
}
public Ghost(Cell cell, int spriteLevel) {
super(cell, spriteLevel);
loadFrightenedBlue();
loadFrightenedBlinking();
loadEatenEyes();
}
protected Cell findNextCell(Cell targetCell, int l) {
while (targetCell.getParent(l) != null && targetCell.getParent(l).getParent(l) != null) {
targetCell = targetCell.getParent(l);
}
return targetCell;
}
protected void checkIfHomeCell() {
if (spriteCell == homeCell1) {
homeCell = homeCell2;
} else if (spriteCell == homeCell2) {
homeCell = homeCell1;
}
}
public void setBlinking(boolean blinking) {
isBlinking = blinking;
}
public int getFrightenedStage() {
return frightenedStage;
}
public void setFrightenedStage(int frightenedStage) {
this.frightenedStage = frightenedStage;
}
protected Cell cellsAhead(Cell targetCell, int ahead) {
Pacman.Facing facing = Pacman.pacman.getFacing();
if (facing == Pacman.Facing.LEFT) {
while (notHitWall(ahead, targetCell.getLeft())) {
targetCell = targetCell.getLeft();
if (targetCell == spriteCell) {
return Pacman.pacman.getCell();
}
--ahead;
}
} else if (facing == Pacman.Facing.RIGHT) {
while (notHitWall(ahead, targetCell.getRight())) {
targetCell = targetCell.getRight();
if (targetCell == spriteCell) {
return Pacman.pacman.getCell();
}
--ahead;
}
} else if (facing == Pacman.Facing.UP) {
while (notHitWall(ahead, targetCell.getUp())) {
targetCell = targetCell.getUp();
if (targetCell == spriteCell) {
return Pacman.pacman.getCell();
}
--ahead;
}
} else if (facing == Pacman.Facing.DOWN) {
while (notHitWall(ahead, targetCell.getDown())) {
targetCell = targetCell.getDown();
if (targetCell == spriteCell) {
return Pacman.pacman.getCell();
}
--ahead;
}
}
return targetCell;
}
private boolean notHitWall(int ahead, Cell cell) {
return (cell.getType() != WALL) && (ahead > 0);
}
protected void setDance(Cell nextCell) {
if (isBlinking) {
startAnimation(blinkAnimation[0]);
} else if (isFrightened && !isEaten) {
startAnimation(blueAnimation[0]);
} else if (spriteCell.getLeft() == nextCell) {
spriteFacing = Pacman.Facing.LEFT;
setNormalOrEatenAnimation(0);
} else if (spriteCell.getRight() == nextCell) {
spriteFacing = Pacman.Facing.RIGHT;
setNormalOrEatenAnimation(1);
} else if (spriteCell.getUp() == nextCell) {
spriteFacing = Pacman.Facing.UP;
setNormalOrEatenAnimation(2);
} else if (spriteCell.getDown() == nextCell) {
spriteFacing = Pacman.Facing.DOWN;
setNormalOrEatenAnimation(3);
}
}
private void setNormalOrEatenAnimation(int i) {
if (isEaten) {
startAnimation(eatenAnimation[i]);
} else {
startAnimation(normalDanceAnimation[i]);
}
}
public void setIsFrightened(boolean isFrightened) {
this.isFrightened = isFrightened;
prevCell = null;
}
public void loadNormalDanceStances() {
isEaten = false;
loadDanceStances(danceStance);
}
protected void setSpeed(double speed) {
spriteSpeed = speed;
}
public boolean isInGhostHouse() {
return spriteCell == ghostHouse;
}
public void setPrevCell(Cell prevCell) {
this.prevCell = prevCell;
}
protected void loadDanceStances(int y) {
danceRight = new BufferedImage[]{
getSprite(0, y),
getSprite(1, y),
};
danceLeft = new BufferedImage[]{
getSprite(2, y),
getSprite(3, y),
};
danceUp = new BufferedImage[]{
getSprite(4, y),
getSprite(5, y),
};
danceDown = new BufferedImage[]{
getSprite(6, y),
getSprite(7, y),
};
normalDanceAnimation = new Animation[]{
new Animation(danceLeft, 2),
new Animation(danceRight, 2),
new Animation(danceUp, 2),
new Animation(danceDown, 2)};
}
protected void loadFrightenedBlue() {
danceBlue = new BufferedImage[]{
getSprite(8, 4),
getSprite(9, 4),
};
blueAnimation = new Animation[]{
new Animation(danceBlue, 2),
};
}
protected void loadFrightenedBlinking() {
BufferedImage blue1 = getSprite(8, 4);
BufferedImage blue2 = getSprite(9, 4);
BufferedImage white1 = getSprite(10, 4);
BufferedImage white2 = getSprite(11, 4);
danceBlink = new BufferedImage[]{
blue1,
white1,
blue2,
white2,
};
blinkAnimation = new Animation[]{
new Animation(danceBlink, 4),
};
}
protected void loadEatenEyes() {
eatenRight = new BufferedImage[]{
getSprite(8, 5)};
eatenLeft = new BufferedImage[]{
getSprite(9, 5)};
eatenUp = new BufferedImage[]{
getSprite(10, 5)};
eatenDown = new BufferedImage[]{
getSprite(11, 5)};
eatenAnimation = new Animation[]{
new Animation(eatenLeft, 4),
new Animation(eatenRight, 4),
new Animation(eatenUp, 4),
new Animation(eatenDown, 4),
};
}
}