-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monster.java
272 lines (245 loc) · 6.34 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
package monster;
import map.Map;
import player.Player;
import game.GameConstants;
/**
* The Monster Base class.
* Monster will random walk on the whole map, until distance between the player smaller than ALERT_DISTANCE.
* Then the monster will follow the player, until get killed or be rid of, or collide with the player (
* in this case the player's HP will reduce by HP_LOSS_BY_MONSTER).
*
* @author Hang Chen
* @version 1.0
*/
public class Monster implements GameConstants {
boolean alive;
boolean alert; // whether the monster is in alert state
double velocity;
int oldDirection;
int direction;
int x;
int y;
int dyingCounter; // to implement dying effect
Brain brain;
Path path;
public int id;
/**
* Create the monster at random position
*/
public Monster(Map m) {
while (true) {
int i = (int)(CELL_NUM_X*Math.random());
int j = (int)(CELL_NUM_Y*Math.random());
// monster will not born on walls or the top-left 1/16 area
if (m.isAvailable(i, j) && i>CELL_NUM_X/4 && j>CELL_NUM_Y/4) {
this.x = i * CELL_WIDTH;
this.y = j * CELL_HEIGHT;
init();
break;
}
}
}
/**
* Create a monster at the given position (pixel coordinates)
*/
public Monster(int X, int Y) {
this.x = X;
this.y = Y;
init();
}
/**
* Set monster's properties
*/
void init() {
this.alive = true;
this.alert = false;
this.velocity = MONSTER_SPEED_LOW +
(MONSTER_SPEED_HIGH - MONSTER_SPEED_LOW) * Math.random();
this.direction = -1;
this.oldDirection = 0;
brain = new Brain(1);
path = new Path();
id = 0;
}
/**
* Generate the next direction
*/
int nextDirection(Player p, Map m) {
if (path.size() > 0) {
// compute direction
int dx = path.getNextX() - this.x;
int dy = path.getNextY() - this.y;
if (Math.abs(dx) < 2*this.velocity) {
this.x = path.getNextX();
dx = 0;
}
if (Math.abs(dy) < 2*this.velocity) {
this.y = path.getNextY();
dy = 0;
}
if (dx == 0 && dy == 0) {
path.removeFirst();
// replanning if the next position in the path is invalid or in explosion
if (path.size()>0 && !brain.isMovable(m, path.getNextI(), path.getNextJ()))
path.clear();
return DIRECTION_STOP;
}
if (dx > 0 && dy == 0) return DIRECTION_RIGHT;
if (dx < 0 && dy == 0) return DIRECTION_LEFT;
if (dx == 0 && dy > 0) return DIRECTION_DOWN;
if (dx == 0 && dy < 0) return DIRECTION_UP;
}
return DIRECTION_STOP;
}
/**
* Update monster's alert state
*/
void updateAlert(Player p) {
int mi = Math.round((float) x/CELL_WIDTH);
int mj = Math.round((float) y/CELL_HEIGHT);
int pi = Math.round((float) p.getX()/CELL_WIDTH);
int pj = Math.round((float) p.getY()/CELL_HEIGHT);
int dis = Math.abs(mi-pi) + Math.abs(mj-pj);
this.alert = (dis <= ALERT_DISTANCE);
}
void setDirection(int d) {
if (this.direction==DIRECTION_LEFT || this.direction==DIRECTION_RIGHT)
this.oldDirection = this.direction;
this.direction = d;
}
/**
* Get the image's index
*/
public int getImageIndex() {
if (isDying()) return 2;
else {
int d = this.direction == DIRECTION_LEFT || this.direction == DIRECTION_RIGHT ?
this.direction : this.oldDirection;
switch (d) {
case DIRECTION_LEFT: return 0;
case DIRECTION_RIGHT: return 1;
}
return 0;
}
}
public void setVelocity(double v) {
this.velocity = v;
}
public double getVelocity() {
return this.velocity;
}
public void setX(int X) {
this.x = X;
}
public void setY(int Y) {
this.y = Y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setLocation(int X, int Y) {
this.x = X;
this.y = Y;
}
/**
* Move a step and update path
*/
void moveStep(Player p, Map m) {
switch (this.direction) {
case DIRECTION_UP:
this.y -= this.velocity;
break;
case DIRECTION_DOWN:
this.y += this.velocity;
break;
case DIRECTION_LEFT:
this.x -= this.velocity;
break;
case DIRECTION_RIGHT:
this.x += this.velocity;
break;
case DIRECTION_STOP:
int mi = Math.round((float) x/CELL_WIDTH);
int mj = Math.round((float) y/CELL_HEIGHT);
if (this.alert) {
int pi = Math.round((float) p.getX()/CELL_WIDTH);
int pj = Math.round((float) p.getY()/CELL_HEIGHT);
brain.findPath(m, path, mi, mj, pi, pj);
}
else brain.randomPath(m, path, mi, mj);
break;
}
}
/**
* Take some actions in a game loop
*/
public void monsterMove(Player p, Map m) {
if (this.alive) {
moveStep(p, m); // move a step
if (p.getActiveItem() != null) { // killed by bullet
if (isCollided(p.getActiveItem().getX(), p.getActiveItem().getY(), CELL_WIDTH, CELL_HEIGHT)) {
eliminate();
p.getActiveItem().setState(false);
p.setIsUsingBulletFlag(0);
p.setActiveItem(null);
}
} else if (isCollided(p.getX(), p.getY(), CELL_WIDTH, CELL_HEIGHT)) { // collide with player
eliminate();
p.getHurt(HP_LOSS_BY_MONSTER);
} else if (isBlownOff(m)) { // killed by bomb
eliminate();
} else { // still alive
setDirection(nextDirection(p, m));
updateAlert(p);
}
}
else if (isDying()) {
this.dyingCounter--;
}
}
/**
* Check whether the monster is collided with a blown region
*/
boolean isBlownOff(Map m) {
int mi = Math.floorDiv(x, CELL_WIDTH);
int mj = Math.floorDiv(y, CELL_HEIGHT);
for (int i=mi; i<Math.min(mi+2, CELL_NUM_X); ++i)
for (int j=mj; j<Math.min(mj+2, CELL_NUM_Y); ++j)
if (m.isAtExplosion(i, j) &&
isCollided(i*CELL_WIDTH, j*CELL_HEIGHT, CELL_WIDTH, CELL_HEIGHT))
return true;
return false;
}
/**
* Check whether the monster is collided with a rectangle [x,y,w,h]
*/
boolean isCollided(int x, int y, int w, int h) {
int x1 = Math.max(x, this.x);
int x2 = Math.min(x + w, this.x + MONSTER_WIDTH);
int y1 = Math.max(y, this.y);
int y2 = Math.min(y + h, this.y + MONSTER_HEIGHT);
return (x2 > x1) && (y2 > y1);
}
public boolean isAlive() {
return this.alive;
}
/**
* If this function return false, then the monster will not be painted
*/
public boolean isVisible() {
return this.alive || isDying();
}
public boolean isDying() {
return this.dyingCounter > 0;
}
/**
* Remove itself from the map after killed.
*/
public void eliminate() {
this.alive = false;
this.dyingCounter = 30; // take 0.9 seconds to die thoroughly
}
}