-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.java
More file actions
330 lines (299 loc) · 6.4 KB
/
Entity.java
File metadata and controls
330 lines (299 loc) · 6.4 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
*
* @author elan
*
*/
public class Entity
{
/**
* these variable are all instance variable because they will be different for every instance
*/
protected double xPos, yPos, angle;
protected int index, HP;
protected boolean dead = false;
/**
* Constructor for my Entity
* @param x x co-ordinate where it will be created
* @param y y co-ordinate where it will be created
* @param a angle it will start with
*/
public Entity(double x, double y, double a)
{
xPos = x;
yPos = y;
angle = a;
}
/**
* Constructor for my Entity
* @param x x co-ordinate where it will be created
* @param y y co-ordinate where it will be created
* @param a angle it will start with
* @param i unique identifier
*/
public Entity(double x, double y, double a, int i)
{
xPos = x;
yPos = y;
angle = a;
index = i;
}
/**
* Constructor for my Entity
* @param x x co-ordinate where it will be created
* @param y y co-ordinate where it will be created
* @param a angle it will start with
* @param i unique identifier
* @param HP hit points it will start with
*/
public Entity(double x, double y, double a, int i, int HP)
{
xPos = x;
yPos = y;
angle = a;
index = i;
this.HP = HP;
}
/**
* Draws the picture on the screen
* @param filename Name of the image to be drawn
*/
public void render(String filename)
{
StdDraw.picture(xPos, yPos, filename, States.SHOOTER_SIZE, States.SHOOTER_SIZE, angle);
}
/**
* @return Returns the X co-ordinate of the entity
*/
public double getX()
{
return xPos;
}
/**
* @return Returns the X co-ordinate of the entity
*/
public double getY()
{
return yPos;
}
/**
* @return Returns the angle of the entity
*/
public double getAngle()
{
return angle;
}
/**
* @return Returns the hit points of the entity
*/
public int getHP()
{
return HP;
}
/**
* @param speed moves the entity Left by this amount
*/
public void moveLeft(double speed)
{
if(canMoveLeft())
{
xPos -= speed;
}
}
/**
* @param speed moves the entity Right by this amount
*/
public void moveRight(double speed)
{
if(canMoveRight())
{
xPos += speed;
}
}
/**
* @param speed moves the entity Up by this amount
*/
public void moveUp(double speed)
{
if(canMoveUp())
{
yPos += speed;
}
}
/**
* @param speed moves the entity Down by this amount
*/
public void moveDown(double speed)
{
if(canMoveDown())
{
yPos -= speed;
}
}
/**
* @param speed rotates the entity clockwise by this amount
*/
public void rotateClockWise(double speed)
{
if(canRotateClockWise())
{
angle -= speed;
}
}
/**
* @param speed rotates the entity anti-clockwise by this amount
*/
public void rotateAntiClockWise(double speed)
{
if(canRotateAntiClockWise())
{
angle += speed;
}
}
/**
* @return true if entity can move left
*/
protected boolean canMoveLeft()
{
if(xPos <= (States.SHOOTER_SIZE * 0.5 + States.SHOOTER_MOVE_SPEED * 1.1))
{
return false;
}
return true;
}
/**
* @return true if entity can move right
*/
protected boolean canMoveRight()
{
if(xPos >= (States.WINDOW_RESOLUTION - (States.SHOOTER_SIZE * 0.5 + States.SHOOTER_MOVE_SPEED * 1.1)))
{
return false;
}
return true;
}
/**
* @return true if entity can move up
*/
protected boolean canMoveUp()
{
if(yPos >= States.WINDOW_RESOLUTION)
{
return false;
}
return true;
}
/**
* @return true if entity can move down
*/
protected boolean canMoveDown()
{
if(yPos <= 0)
{
return false;
}
return true;
}
/**
* @return true if entity can rotate anti-clockwise
*/
private boolean canRotateAntiClockWise()
{
if(angle >= 90 - States.SHOOTER_ROTATE_SPEED)
{
return false;
}
return true;
}
/**
* @return true if entity can rotate clockwise
*/
private boolean canRotateClockWise()
{
if(angle <= -90 + States.SHOOTER_ROTATE_SPEED)
{
return false;
}
return true;
}
/**
* @return the entity's unique identifier
*/
public int getIndex()
{
return index;
}
/**
* @param i sets the entity's unique identifier to this
*/
public void updateIndex(int i)
{
index = i;
}
/**
* This method check whether these two entities are touching
* @param entity1
* @param entity2
* @return true if they are in contact
*/
protected boolean inRadius(Entity entity1, Entity entity2)
{
try
{
if(entity1 instanceof Shooter || entity2 instanceof Shooter)
if(Math.abs(entity1.getX() - entity2.getX()) < States.SHOOTER_SIZE*0.5 && Math.abs(entity1.getY() - entity2.getY()) < States.SHOOTER_SIZE*0.5)
return true;
if(entity1 instanceof Enemy || entity2 instanceof Enemy)
if(Math.abs(entity1.getX() - entity2.getX()) < States.enemySize*0.5 && Math.abs(entity1.getY() - entity2.getY()) < States.enemySize*0.5)
return true;
if(entity1 instanceof PowerUpBox || entity2 instanceof PowerUpBox)
if(Math.abs(entity1.getX() - entity2.getX()) < States.SHOOTER_SIZE*0.5 && Math.abs(entity1.getY() - entity2.getY()) < States.SHOOTER_SIZE*0.5)
return true;
if(entity1 instanceof Bunker && entity1 instanceof Bunker)
if(Math.abs(entity1.getX() - entity2.getX()) < (((Bunker) entity1).getWidth() + ((Bunker) entity2).getWidth())/(double)2)
return true;
if(entity2 instanceof Bunker)
if(Math.abs(entity1.getX() - entity2.getX()) < ((Bunker) entity2).getWidth()/(double)2 && Math.abs(entity1.getY() - entity2.getY()) < States.BUNKER_HEIGHT)
return true;
if(entity1 instanceof Boss || entity2 instanceof Boss)
if(Math.abs(entity1.getX() - entity2.getX()) < States.BOSS_SIZE*0.5 && Math.abs(entity1.getY() - entity2.getY()) < States.BOSS_SIZE*0.5)
return true;
}
catch(Exception e) {e.printStackTrace();}
return false;
}
/**
* Decreases the hit points of the entity
* @param bullet the damage of this is subtracted from hit points
* @return result of checHP
*/
public boolean damage(Bullet bullet)
{
HP -= bullet.getDamage();
return checkHP();
}
/**
* @return true if entity's hit points is 0 or less
*/
private boolean checkHP()
{
if(HP <= 0)
{
if(this instanceof Enemy)
{
Score.increaseScore(Controller.player, (Enemy) this);
return true;
}
else if(this instanceof Shooter)
if(((Shooter)this).getLives() < 1)
return true;
else
{
((Shooter)this).loseLife();
Controller.player = new Shooter();
}
else if(this instanceof Bunker)
return true;
}
return false;
}
}