-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTruck.pde
363 lines (314 loc) · 10.4 KB
/
Truck.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
// https://www.openprocessing.org/sketch/141841
class Truck {
//declaration of floats, booleans, and pVectors
PVector myLocation;
PVector frontWheel;
PVector backWheel;
float myHeading, mySpeed, myBaseMaxSpeed, myMaxSpeed, mySteerAngle, myMaxSteerAngle, myWheelBase, myMinWheelBase, myMaxWheelBase, myHP, maxHP, carSize;
boolean plus, minus, up, down, left, right, steerLock, exploding, exploded, timerStarted;
//timer for how long the explosion of the truck lasts
Timer explosionTimer = new Timer(8);
//truck object(what all it's variables mean
Truck(float maxSpeed) {
up = down = left = right = steerLock = false;
myLocation = new PVector(860, 540);
myHeading = PI;
mySpeed = 0;
myBaseMaxSpeed = maxSpeed;
myMaxSpeed = myBaseMaxSpeed;
mySteerAngle = 0;
myWheelBase = 150;
myMaxSteerAngle = PI/4;
maxHP = 1;
myHP = maxHP;
carSize = 8;
}
//return function for speed
float getSpeed() {
return mySpeed;
}
//location of vehicle on x and y plane
void setX (float x) {
myLocation.x = x;
}
void setY (float y) {
myLocation.y = y;
}
//return function for truck x and y locations
float getX() {
return myLocation.x;
}
float getY() {
return myLocation.y;
}
//return functions for gun x and y locations
float gunX() {
return myLocation.x;
}
float gunY() {
return myLocation.y;
}
void setHeading(float heading) {
myHeading = heading;
}
void setSteerAngle(float steerAngle) {
mySteerAngle = steerAngle;
}
void setSpeed(float speed) {
mySpeed = speed;
}
void setWheelBase(float wb) {
myWheelBase = wb;
}
void setLeft (boolean l) {
left = l;
}
void setRight(boolean r) {
right = r;
}
void setUp(boolean u) {
up = u;
}
void setDown(boolean d) {
down = d;
}
void reduceHP(float dmg) {
myHP-=dmg;
}
void setHP (float h) {
myHP = h;
if (myHP > 1) myHP = 1;
}
void setMaxSpeed() {
myMaxSpeed = myHP * myBaseMaxSpeed;
}
void resetMaxSpeed() {
myMaxSpeed = 6;
}
//this is the function for the damage on the car, it determines what HP the vehicle is at as well as what level it is on(using instanceof), once this is decided it implements the proper images onto the trucks location
void carDamage() {
if (myHP > 0) {
if (myHP != maxHP) {
pushMatrix();
imageMode(CENTER);
translate(myLocation.x, myLocation.y);
rotate(-myHeading);
for (Level l : levels) {
if (l instanceof LevelVietnam) {
if (myHP < 0.25) {
image(carDamage[2], 0, 0);
image(vietCarDamage[2], 0, 0);
} else if (myHP < 0.5) {
image(carDamage[1], 0, 0);
image(vietCarDamage[1], 0, 0);
} else if (myHP < 0.75) {
image(carDamage[0], 0, 0);
image(vietCarDamage[0], 0, 0);
}
} else if (l instanceof LevelAfghan) {
if (myHP < 0.25) {
image(carDamage[2], 0, 0);
image(afghanCarDamage[2], 0, 0);
} else if (myHP < 0.5) {
image(carDamage[1], 0, 0);
image(afghanCarDamage[1], 0, 0);
} else if (myHP < 0.75) {
image(carDamage[0], 0, 0);
image(afghanCarDamage[0], 0, 0);
}
} else {
if (myHP < 0.25) {
image(carDamage[2], 0, 0);
image(carDamage[5], 0, 0);
} else if (myHP < 0.5) {
image(carDamage[1], 0, 0);
image(carDamage[4], 0, 0);
} else if (myHP < 0.75) {
image(carDamage[0], 0, 0);
image(carDamage[3], 0, 0);
}
}
}
popMatrix();
}
}
}
//continuously centered health bar that goes down and changes color depending on the amount of health left
void healthBar() {
if (myHP > 0) {
if (myHP != maxHP) {
if (myHP > (maxHP*0.75)) {
fill(0, 255, 0);
} else if (myHP > (maxHP * 0.25)) {
fill(255, 255, 0);
} else {
fill(255, 0, 0);
}
rectMode(CENTER);
rect(myLocation.x, myLocation.y - 50, (200)*(myHP/maxHP), 10);
rectMode(CENTER);
}
}
}
//checks if the vehicle is exploding by check if the HP is less than or equal to zero, clears the gun and spawns the explosion image at the trucks location, emus in location have their HP reduced, after explosion timer is up gameOver becomes true
void explode() {
if (!exploding) {
if (!exploded) {
if (myHP <= 0) {
exploded = true;
explosions.add(new Explosion(myLocation.x, myLocation.y, 300));
explosionTimer.setSeconds(8);
exploding = true;
guns.clear();
for (Emu e : emus) {
if (e.getX() > myLocation.x - 300 && e.getX() < myLocation.x + 300 && e.getY() > myLocation.y - 300 && e.getY() < myLocation.y + 300) {
e.reduceHP(500);
}
}
}
}
} else {
explosionTimer.update();
if (explosionTimer.isDone()) {
exploding = false;
for (Level l : levels) {
if (l instanceof LevelFour) {
if (l.getBossDead()) {
l.setEndTimerState(true);
myHeart.rewind();
myHeart.play();
} else {
gameOver = true;
}
} else {
gameOver = true;
}
}
}
}
}
//if your speed is greater than 2 and you hit an emu it kills them and reduces the driver's HP, if the emu is not the boss emu. If it is the boss emu, hitting it stops and damages the car.
void hitEmu() {
for (Emu e : emus) {
if (e instanceof BossEmu) {
if (e.getX() > myLocation.x - 100 && e.getX() < myLocation.x + 100 && e.getY() > myLocation.y - 100 && e.getY() < myLocation.y + 100) {
reduceHP(0.005);
}
} else {
if (e.getX() > myLocation.x - 100 && e.getX() < myLocation.x + 100 && e.getY() > myLocation.y - 100 && e.getY() < myLocation.y + 100) {
if (mySpeed > 2) {
e.reduceHP(50);
reduceHP(0.005);
}
}
}
}
}
float getHP() {
return myHP;
}
void update() {
if (myLocation.x<0) myLocation.x=0;
if (myLocation.x>width) myLocation.x=width;
if (myLocation.y<0) myLocation.y=0;
if (myLocation.y>height) myLocation.y=height;
frontWheel = new PVector(myLocation.x+(myWheelBase/2)*sin(myHeading), myLocation.y+(myWheelBase/2)*cos(myHeading));
backWheel = new PVector(myLocation.x-(myWheelBase/2)*sin(myHeading), myLocation.y-(myWheelBase/2)*cos(myHeading));
// front axle
pushMatrix();
translate(frontWheel.x, frontWheel.y);
rotate(-myHeading);
strokeWeight(2);
line (-myWheelBase/7, 0, myWheelBase/7, 0);
strokeWeight(1);
popMatrix();
// end: front axle
// front left wheel
pushMatrix();
translate(frontWheel.x+sin(myHeading+PI/2)*myWheelBase/2, frontWheel.y+cos(myHeading+PI/2)*myWheelBase/2);
// sin(myHeading+PI/2) and cos(myHeading+PI/2) helps to place wheel to correct position shifted to side by (myWheelBase/7)
// otherwise it would circle around the front regarding to a heading
rotate(-mySteerAngle-myHeading);
rectMode(CENTER);
fill(0);
rect(0, 0, myWheelBase/7, myWheelBase/3, myWheelBase/(myWheelBase/10));
popMatrix();
// end: front left wheel
// front right wheel
pushMatrix();
translate(frontWheel.x-sin(myHeading+PI/2)*myWheelBase/2, frontWheel.y-cos(myHeading+PI/2)*myWheelBase/2);
rotate(-mySteerAngle-myHeading);
rectMode(CENTER);
fill(0);
rect(0, 0, myWheelBase/7, myWheelBase/3, myWheelBase/(myWheelBase/10));
popMatrix();
// end: front right wheel
// back wheels
pushMatrix();
translate(backWheel.x, backWheel.y);
rotate(-myHeading);
rectMode(CENTER);
fill(0);
strokeWeight(2);
line(-myWheelBase/7, 0, myWheelBase/7, 0);
strokeWeight(1);
rect(-myWheelBase/3, 0, myWheelBase/2, myWheelBase/3, myWheelBase/(myWheelBase/10));
rect(myWheelBase/3, 0, myWheelBase/2, myWheelBase/3, myWheelBase/(myWheelBase/10));
popMatrix();
frontWheel.add(mySpeed*sin(myHeading+mySteerAngle), mySpeed*cos(myHeading+mySteerAngle), 0);
backWheel.add(mySpeed*sin(myHeading), mySpeed*cos(myHeading), 0);
myLocation.set(frontWheel.x+backWheel.x, frontWheel.y+backWheel.y, 0) ;
myLocation.div(2);
// end: back wheels
pushMatrix();
translate(myLocation.x, myLocation.y);
rotate(-myHeading);
rectMode(CENTER);
fill(150);
rect(0, 0, myWheelBase, myWheelBase+myWheelBase/1.5 + 30, 3, 3, myWheelBase/(myWheelBase/20), myWheelBase/(myWheelBase/20));
popMatrix();
myHeading = atan2(frontWheel.x - backWheel.x, frontWheel.y - backWheel.y );
//locks the tires in place when you steer too far to the left and right
if (left) {
if (mySteerAngle < myMaxSteerAngle) mySteerAngle += 0.08;
if (mySteerAngle>myMaxSteerAngle) mySteerAngle = myMaxSteerAngle;
} else {
if (!steerLock) {
if (mySteerAngle > 0) mySteerAngle -= 0.08;
}
}
// Right
if (right) {
if (mySteerAngle > -myMaxSteerAngle) mySteerAngle -= 0.08;
if (mySteerAngle<-myMaxSteerAngle) mySteerAngle = -myMaxSteerAngle;
} else {
if (!steerLock) {
if (mySteerAngle < 0) mySteerAngle += 0.08;
}
}
/* check with Dakota */
// UP
if (up)
{
if (mySpeed<myMaxSpeed) mySpeed += 0.08;
} else {
}
// DOWN
if (down) {
if (mySpeed>0) mySpeed -= 0.15; //brake
else
if (abs(mySpeed)<myMaxSpeed) mySpeed -= 0.05; // reverse
} else {
}
if (abs(mySteerAngle)<0.08) mySteerAngle = 0;
if (mySpeed>0) mySpeed -= 0.01; //friction for forward
if (mySpeed<0) mySpeed += 0.01; //friction for backward
if ((!up && !down) && (abs(mySpeed)<0.01)) mySpeed=0;
//calling of major functions into the update function
carDamage();
setMaxSpeed();
healthBar();
hitEmu();
explode();
}
}