-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.pde
440 lines (388 loc) · 10.7 KB
/
Enemy.pde
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
class Enemy
{
PShape Hive;
PShape Guard;
PShape Patriot;
PShape Boss;
int type, p, strtpos, lives;
float x, y, LBulpos, RBulpos;
float dir = 2;
boolean check = false, dead, shot;
color boss = color(90, 180, 235);
void CreateHive() //Create Hive
{
Hive = createShape(GROUP);
PShape body = createShape();
body.beginShape();
body.fill(130, 30, 120);
body.vertex(0, -15); //-100 -25
body.vertex(35, -15);
body.vertex(35, 5);
body.vertex(5, 25);
body.vertex(-15, 25);
body.vertex(-35, 5);
body.vertex(-35, -15);
body.endShape(CLOSE);
PShape leftwing = createShape();
leftwing.beginShape();
leftwing.fill(130, 30, 120);
leftwing.vertex(-25, -25);
leftwing.vertex(-45, -25);
leftwing.vertex(-60, 0);
leftwing.vertex(-35, 35);
leftwing.vertex(-25, 35);
leftwing.endShape(CLOSE);
PShape rightwing = createShape();
rightwing.beginShape();
rightwing.fill(130, 30, 120);
rightwing.vertex(15, -25);
rightwing.vertex(35, -25);
rightwing.vertex(50, 0);
rightwing.vertex(25, 35);
rightwing.vertex(15, 35);
rightwing.endShape(CLOSE);
fill(255);
PShape cockpit = createShape();
cockpit.beginShape();
cockpit.vertex(-20, 5);
cockpit.vertex(10, 5);
cockpit.vertex(10, 10);
cockpit.vertex(0, 20);
cockpit.vertex(-10, 20);
cockpit.vertex(-20, 10);
cockpit.endShape(CLOSE);
Hive.addChild(body);
Hive.addChild(leftwing);
Hive.addChild(rightwing);
Hive.addChild(cockpit);
}
void CreatePatriot() //Create Patriot
{
Patriot = createShape(GROUP);
PShape body = createShape();
body.beginShape();
body.fill(130, 30, 120);
body.vertex(0, -40); //-100, -40
body.vertex(-20, -20);
body.vertex(-20, 20);
body.vertex(0, 40);
body.vertex(20, 20);
body.vertex(20, -20);
body.endShape(CLOSE);
PShape leftwing = createShape();
leftwing.beginShape();
leftwing.fill(130, 30, 120);
leftwing.vertex(-20, -10);
leftwing.vertex(-45, -10);
leftwing.vertex(-50, -20);
leftwing.vertex(-50, 15);
leftwing.vertex(-20, 15);
leftwing.endShape(CLOSE);
PShape rightwing = createShape();
rightwing.beginShape();
rightwing.fill(130, 30, 120);
rightwing.vertex(20, -10);
rightwing.vertex(45, -10);
rightwing.vertex(50, -20);
rightwing.vertex(50, 15);
rightwing.vertex(20, 15);
rightwing.endShape(CLOSE);
PShape leftgun = createShape();
leftgun.beginShape();
leftgun.fill(100);
leftgun.vertex(-35, 15);
leftgun.vertex(-35, 25);
leftgun.vertex(-42, 25);
leftgun.vertex(-42, 15);
leftgun.endShape(CLOSE);
PShape rightgun = createShape();
rightgun.beginShape();
rightgun.fill(100);
rightgun.vertex(35, 15);
rightgun.vertex(35, 25);
rightgun.vertex(43, 25);
rightgun.vertex(43, 15);
rightgun.endShape(CLOSE);
fill(255);
PShape cockpit = createShape(ELLIPSE, 0, 10, 10, 30);
Patriot.addChild(body);
Patriot.addChild(leftwing);
Patriot.addChild(rightwing);
Patriot.addChild(leftgun);
Patriot.addChild(rightgun);
Patriot.addChild(cockpit);
}
void CreateGuard() //Create Guard
{
Guard = createShape(GROUP);
fill(130, 30, 120);
PShape body = createShape(ELLIPSE, 0, 0, 60, 60);
PShape rightweapon = createShape(ARC, 18, 25, 30, 30, 15, 25, HALF_PI);
PShape leftweapon = createShape(ARC, -18, 25, 30, 30, 15, 25, HALF_PI);
fill(255);
PShape cockpit = createShape(ELLIPSE, 0, 15, 25, 20);
PShape leftengine = createShape();
leftengine.beginShape();
leftengine.fill(100);
leftengine.vertex(-15, -10);
leftengine.vertex(-25, -10);
leftengine.vertex(-35, -35);
leftengine.vertex(-20, -35);
leftengine.endShape(CLOSE);
PShape rightengine = createShape();
rightengine.beginShape();
rightengine.fill(100);
rightengine.vertex(15, -10);
rightengine.vertex(25, -10);
rightengine.vertex(35, -35);
rightengine.vertex(20, -35);
rightengine.endShape(CLOSE);
PShape leftgun = createShape();
leftgun.beginShape();
leftgun.fill(100);
leftgun.vertex(-25, 20);
leftgun.vertex(-40, 40);
leftgun.vertex(-32, 45);
leftgun.vertex(-17, 26);
leftgun.endShape();
PShape rightgun = createShape();
rightgun.beginShape();
rightgun.fill(100);
rightgun.vertex(25, 20);
rightgun.vertex(40, 40);
rightgun.vertex(32, 45);
rightgun.vertex(17, 26);
rightgun.endShape();
Guard.addChild(leftengine);
Guard.addChild(rightengine);
Guard.addChild(leftgun);
Guard.addChild(rightgun);
Guard.addChild(rightweapon);
Guard.addChild(leftweapon);
Guard.addChild(body);
Guard.addChild(cockpit);
}
void CreateBoss()
{
Boss = createShape(GROUP);
stroke(0);
fill(113, 66, 66);
PShape body = createShape(ELLIPSE, 0, 0, 150, 150);
fill(255);
PShape eye = createShape(ELLIPSE, 0, 20, 90, 70);
fill(boss);
PShape pupil = createShape(ELLIPSE, 0, 20, 30, 30);
Boss.addChild(body);
Boss.addChild(eye);
Boss.addChild(pupil);
}
void Render()
{
if (type == 1 && dead == false) //if dead, do not render
{
shape(Hive, x, y);
}
if (type == 2 && dead == false)
{
shape(Patriot, x, y);
}
if (type == 3 && dead == false)
{
shape(Guard, x, y);
}
if (type == 4 && dead == false)
{
shape(Boss, x, y);
}
}
void Update()
{
if (type == 1 && dead == false) //If Hive enemy and enemy not dead
{
switch(p) //P dictates what pattern the enemy will run
{
case 1:
y = TopDown(y);
break;
case 2:
y = ZigZag(x, y, check);
break;
case 4:
y = Corner(x, y);
break;
default:
break;
}
if (y > 820 || lives <= 12)
{ //If enemy goes off the screen, or lives drop below
Enemies.remove(this); //Remove enemy from Enemy array
dead = true; //Make dead true to prevent enemy from being rendered
if (shot == true) //If enemy is shot
{ //Score incremented
score += 100;
}
}
}
else if (type == 2 && dead == false) //If enemy is Patriot and enemy not dead
{
switch(p)//P dictates the pattern the enemy will run
{
case 1:
y = TopDown(y);
break;
case 2:
y = ZigZag(x, y, check);
break;
case 4:
pos = random(50, 270);
y = Corner(x, y);
break;
default:
break;
}
y = Bullet(x, y); //Bullet method called
if ( y > 820 || lives <= 0)
{ //If enemy goes off screen or lives drops to 0
Enemies.remove(this); //Enemy is removed from enemy array
dead = true; //Make dead true so enemy is not rendered
if (shot == true) //If enemy is shot
{ //Score is incremented
score += 200;
}
}
}
else if (type == 3 && dead == false) //If Guard enemy and enemy not dead
{
switch(p) //P dictates which pattern enemy will run
{
case 3:
Guarding(x, strtpos);
break;
default:
break;
}
if ( (x < -500 || x > 1000) || lives <= 0 )
{ //If enemy goes off screen or lives drop to 0
Enemies.remove(this); //Enemies is removed from enemy array list
dead = true; //Make dead true so enemy is not rendered
if (shot == true) //If enemy is shot
{ //Score is incremented
score += 50;
}
}
}
else if (type == 4 && dead == false) //If boss enemy and enemy is not dead
{
boss = Boss(x, boss);
if (lives <= 0)
{ //If lives drops to 0
Enemies.remove(this); //Remove enemy from Enemy array list
dead = true; //Make dead true so enemy is not rendered
score += 1000; //Score is incremented
win = true; //Win is made true so winning title appears
gameOver = true; //Game over is made true so the game ends
}
BLife = lives; //Keeps track of Boss's life to render health bar
}
}
float TopDown(float ypos)
{
ypos = ypos + 3; //Enemy moves from top to the bottom of the screen
return(ypos);
}
float ZigZag(float xpos, float ypos, boolean c)
{
if (x > 700 && c == false) //Initial check to determine initial direction of enemy
{
dir = -dir; //Changes direction of initial zig zag
c = true; //Prevents if statement from being run again
}
if (x < 50 || x > 590) //If enemy hits edge of the screen
{
dir = -dir; //Change directions
}
x = xpos + dir; //Enemy moves from side to side
ypos = ypos + 3; //Enemy moves from top to the bottom of the screen
return(ypos);
}
void Guarding(float xpos, float start)
{
if (start > 310) //If enemy starts on the right side of the screen
{
x = xpos - dir; //Enemy moves from right to left
}
else //If enemy starts on the left side of the screen
{
x = xpos + dir; //Enemy moves from left to right
}
}
float Corner(float xpos, float ypos)
{
if (xpos > 310) //If enemy starts on right hand of screen
{
if (xpos == 440)
{
ypos = ypos + 3;
}
else
{
x = xpos - 2;
ypos = ypos + 3;
}
}
else
{
if (xpos == 190)
{
ypos = ypos + 3;
}
else
{
x = xpos + 2;
ypos = ypos + 3;
}
}
return(ypos);
}
color Boss(float xpos, color c)
{
if (xpos < 70 || xpos > 550 )
{
dir = -dir;
}
if (frameCount % 240 == 0)
{
c = color(255, 0, 0);
check = true;
laser = true;
}
else if (frameCount % 60 == 0)
{
check = false;
laser = false;
}
else if (check == false)
{
c = color(90, 130, 235);
x = xpos + dir;
laspos = x - 10;
}
CreateBoss();
return(c);
}
float Bullet(float xpos, float ypos)
{
float ybulpos;
int rate;
LBulpos = xpos - 38.5;
RBulpos = xpos + 38.5;
ybulpos = ypos;
rate = 100;
if ( frameCount % rate == 0 )
{
EnemyBullet EB = new EnemyBullet(LBulpos, RBulpos, ybulpos);
EBullets.add(EB);
}
return(ypos);
}
}