-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGun.pde
125 lines (107 loc) · 2.65 KB
/
Gun.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
class Gun {
float myX, myY, myTheta, myMaxAmmo, myAmmo, myDamage;
int myID;
int myRateOfFire, numShots;
boolean reloading, flashing;
PImage gunImage;
Gun () {
myX = truck.gunX();
myY = truck.gunY();
myTheta = 0;
myID = 0;
myDamage = 0;
numShots = 0;
}
int getRateOfFire() {
return myRateOfFire;
}
int getID() {
return myID;
}
float getDamage() {
return myDamage;
}
int getNumShots() {
return numShots;
}
float getTheta() {
return myTheta;
}
// plays gunshot sound and reduces gun ammo on shooting, also causes emus to begin grouping and shows gun flash
void shoot() {
gunshot.rewind();
gunshot.play();
myAmmo--;
if (!group) {
group = true;
}
flashing = true;
//track = true;
}
float getAmmo() {
return myAmmo;
}
float getMaxAmmo() {
return myMaxAmmo;
}
void setAmmo(float ammo) {
myAmmo = ammo;
}
// reload function to increase ammo. Sets ammo to 0 and reloads bullets individually every 2 frames
void reload() {
if (!reloading) {
myAmmo = 0;
reloading = true;
}
if (reloading) {
if (myAmmo < myMaxAmmo) {
if (frameCount%2 == 0) {
myAmmo++;
}
// Draws the green rectangle to show visual reloading progress.
pushMatrix();
fill(0, 255, 0);
rectMode(CORNER);
rect(myX - 50, myY+ 20, 100*(myAmmo/myMaxAmmo), 10);
rectMode(CENTER);
fill(0);
textAlign(CENTER);
textSize(5);
text("RELOADING", myX, myY+30);
popMatrix();
} else {
reloading = false;
}
}
}
boolean getReloading() {
return reloading;
}
void drawGun() {
// ke
myX = truck.gunX();
myY = truck.gunY();
// calls the reload function when the button is pressed (causing reloading to be true) or automatically when ammo runs out
if (reloading || myAmmo == 0) {
reload();
}
// sets angle to rotate gun based on the x position of the truck (CAST)
if (mouseX > truck.getX()) {
myTheta = atan((truck.getY() - mouseY)/(truck.getX() - mouseX));
} else {
myTheta = (atan((mouseY-truck.getY())/(mouseX - truck.getX()))) + radians(180);
}
pushMatrix();
imageMode(CENTER);
translate(truck.getX(), truck.getY());
rotate(myTheta);
image(gunImage, 0, 0);
if (flashing) {
image(flash, gunImage.width/2 + gunImage.width*.15, 0);
if (frameCount%4==0) {
flashing = false;
}
}
popMatrix();
}
}