-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monster.java
286 lines (267 loc) · 7.39 KB
/
Monster.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
import java.awt.Image;
import javax.swing.ImageIcon;
import java.util.*;
public class Monster {
private int color; //1=red 2=yellow 3=pink 4=blue
private int direction; //1=right 2=left 3=up 4=down
private int xMonstCor;
private int yMonstCor;
protected int arrayRow;
protected int arrayCol;
private Image monster;
private Random rand = new Random();
protected boolean dead;
public void monsterReset(int color){
dead = false;
this.color = color;
if (color == 1){
monster = new ImageIcon(this.getClass().getResource("images/monsterRed.png")).getImage();
xMonstCor = 10;
yMonstCor = 570;
arrayRow = 28;
arrayCol = 0;
direction = 1;
}
if (color == 2){
monster = new ImageIcon(this.getClass().getResource("images/monsterYellow.png")).getImage();
xMonstCor = 10;
yMonstCor = 10;
arrayRow = 0;
arrayCol = 0;
direction = 4;
}
if (color == 3){
monster = new ImageIcon(this.getClass().getResource("images/monsterPink.png")).getImage();
xMonstCor = 370;
yMonstCor = 10;
arrayRow = 0;
arrayCol = 18;
direction = 2;
}
if (color == 4){
monster = new ImageIcon(this.getClass().getResource("images/monsterBlue.png")).getImage();
xMonstCor = 370;
yMonstCor = 570;
arrayRow = 28;
arrayCol = 18;
direction = 3;
}
}
public Monster(int color){
this.monsterReset(color);
}
public int getX(){return xMonstCor;}
public int getY(){return yMonstCor;}
public Image getImage(){return monster;}
public boolean detectWall(int[][] lvlData, int direction){ // detects if there is a maze wall in front
if (dead)
return false;
if (arrayCol<18){
if (direction == 1 && lvlData[arrayRow][arrayCol+1] == 2){
return true;
}
}
if (arrayCol>0){
if (direction == 2 && lvlData[arrayRow][arrayCol-1] == 2){
return true;
}
}
if (arrayRow>0){
if (direction == 3 && lvlData[arrayRow-1][arrayCol] == 2){
return true;
}
}
if (arrayRow<28){
if (direction == 4 && lvlData[arrayRow+1][arrayCol] == 2){
return true;
}
}
return false;
}
public boolean detectEdge(int direction){ // detects if it is at the edge
if (xMonstCor>=370 && direction == 1){
xMonstCor = 370;
return true;
}
if (xMonstCor<=10 && direction == 2){
xMonstCor = 10;
return true;
}
if (yMonstCor<=10 && direction == 3){
yMonstCor = 10;
return true;
}
if (yMonstCor>=570 && direction == 4){
yMonstCor = 570;
return true;
}
return false;
}
public boolean canMoverForward(int[][] lvlData, int direction){ // combines detect Edge and Wall
if (detectEdge(direction) || detectWall(lvlData, direction))
return false;
return true;
}
public void moveForward(int[][] lvlData, int direction){ // generic move forward in the specified direction
if (direction==1){
xMonstCor+=20;
arrayCol+=1;
}
if (direction==2){
xMonstCor-=20;
arrayCol-=1;
}
if (direction==3){
yMonstCor-=20;
arrayRow-=1;
}
if (direction==4){
yMonstCor+=20;
arrayRow+=1;
}
}
public void moveRandom(int[][] lvlData){
boolean moved = false;
if (canMoverForward(lvlData, this.direction)){
moveForward(lvlData, this.direction);
moved = true;
}
else{
int newDirection;
while(!moved){
newDirection = rand.nextInt(4)+1;
if (canMoverForward(lvlData, newDirection)){
moveForward(lvlData, newDirection);
moved = true;
this.direction = newDirection;
}
}
}
}
public boolean obstructedView(int[][] lvlData, Pacman player, Monster ghost, int orientation){
int bigger;
int smaller;
if (orientation==1){ //column
if (player.arrayRow > ghost.arrayRow){
bigger = player.arrayRow;
smaller = ghost.arrayRow;
} else{
bigger = ghost.arrayRow;
smaller = player.arrayRow;
}
for (int i=smaller+1;i<bigger;i++){
if(lvlData[i][ghost.arrayCol] == 2)
return true;
}
}
else{ //row
if (player.arrayCol > ghost.arrayCol){
bigger = player.arrayCol;
smaller = ghost.arrayCol;
} else{
bigger = ghost.arrayCol;
smaller = player.arrayCol;
}
for (int i=smaller+1;i<bigger;i++){
if(lvlData[ghost.arrayRow][i] == 2)
return true;
}
}
return false;
}
public void changeColor(Pacman player){
if(player.getPower()){
monster = new ImageIcon(this.getClass().getResource("images/monsterWeak.png")).getImage();
}
else if(color == 1){
monster = new ImageIcon(this.getClass().getResource("images/monsterRed.png")).getImage();
}
else if(color == 2){
monster = new ImageIcon(this.getClass().getResource("images/monsterYellow.png")).getImage();
}
else if(color == 3){
monster = new ImageIcon(this.getClass().getResource("images/monsterPink.png")).getImage();
}
else if(color == 4){
monster = new ImageIcon(this.getClass().getResource("images/monsterBlue.png")).getImage();
}
}
public void moveSearch(int[][]lvlData, Pacman player){
if (!player.powerUp && dead){
dead = false;
}
changeColor(player);
if (!die(player)) {
if(!player.getPower()){
int chance = rand.nextInt(10)+1;
if (chance>6)
moveRandom(lvlData);
else{
if (player.arrayCol == this.arrayCol && !obstructedView(lvlData,player,this,1)){
if (player.arrayRow > this.arrayRow)
this.direction = 4;
if (player.arrayRow < this.arrayRow)
this.direction = 3;
if (canMoverForward(lvlData, this.direction))
this.moveForward(lvlData, this.direction);
}
else if (player.arrayRow == this.arrayRow && !obstructedView(lvlData,player,this,2)){
if (player.arrayCol < this.arrayCol)
this.direction = 2;
if (player.arrayCol > this.arrayCol)
this.direction = 1;
if (canMoverForward(lvlData, this.direction))
this.moveForward(lvlData, this.direction);
}
else
moveRandom(lvlData);
}
}
else {
int chance = rand.nextInt(10)+1;
if (chance>6)
moveRandom(lvlData);
else{
if (player.arrayCol == this.arrayCol && !obstructedView(lvlData,player,this,1)){
if (player.arrayRow > this.arrayRow)
this.direction = 3;
if (player.arrayRow < this.arrayRow)
this.direction = 4;
if (canMoverForward(lvlData, this.direction))
this.moveForward(lvlData, this.direction);
}
else if (player.arrayRow == this.arrayRow && !obstructedView(lvlData,player,this,2)){
if (player.arrayCol < this.arrayCol)
this.direction = 1;
if (player.arrayCol > this.arrayCol)
this.direction = 2;
if (canMoverForward(lvlData, this.direction))
this.moveForward(lvlData, this.direction);
}
else
moveRandom(lvlData);
}
}
}
}
public boolean kill(Pacman player){
// if player is within 21 pixels and they are at the same location on the board they die
int xDiff = Math.abs(this.xMonstCor-player.getX());
int yDiff = Math.abs(this.yMonstCor-player.getY());
if (!player.getPower() && (xDiff < 21 && yDiff < 21) && (this.arrayCol == player.arrayCol || this.arrayRow == player.arrayRow))
return true;
return false;
}
public boolean die(Pacman player){
// if player is within 21 pixels and they are at the same location on they eat the monster
int xDiff = Math.abs(this.xMonstCor-player.getX());
int yDiff = Math.abs(this.yMonstCor-player.getY());
if (player.getPower() && (xDiff < 21 && yDiff< 21) && (this.arrayCol == player.arrayCol || this.arrayRow == player.arrayRow)){
if (!dead)
player.score += 50;
dead = true;
return true;
}
return false;
}
}