-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pistol.pde
133 lines (113 loc) · 2.58 KB
/
Pistol.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
import java.util.*;
class Pistol {
Tee tee;
int capacity = 2; // ammu capacity
// {0,1}
// 1 means ability to shoot, 0 otherwise
// if within injury cool time, it is 0
int shootable = 1;
ArrayDeque<PBullet> ammu = new ArrayDeque<>();
ArrayDeque<PBullet> bulletsInAir = new ArrayDeque<>();
/* <drawing> */
PShape pistolShape;
/* </drawing> */
Pistol(Tee t) {
tee = t;
initAmmu();
loadWeaponShape();
}
void initAmmu() {
for (int i = 0; i < capacity; i++) {
ammu.add(new PBullet(this));
}
}
boolean shootReady() {
return tee.pressShoot && shootable == 1;
}
void shoot() {
if (!ammu.isEmpty() && !tee.prevPressShoot && tee.injuryCD == 0) {
shootable = 1;
} else {
shootable = 0;
}
if (shootReady()) {
PBullet bullet = ammu.pollFirst();
bullet.prepare();
bulletsInAir.add(bullet);
}
tee.prevPressShoot = tee.pressShoot;
}
void bulletsMove() {
for (Iterator<PBullet> iterator = bulletsInAir.iterator(); iterator.hasNext(); ) {
PBullet b = iterator.next();
b.move();
if (!b.isShot) {
iterator.remove();
ammu.add(b);
}
}
}
void loadWeaponShape() {
// TBD: use svg
pistolShape = createShape(GROUP);
PShape p0 = createShape();
p0.beginShape();
p0.fill(85);
p0.stroke(20);
p0.strokeWeight(1.5);
p0.vertex(0, -1);
p0.vertex(16, -1);
p0.vertex(17, 1);
p0.vertex(38, 1);
p0.vertex(35, 9);
p0.vertex(28, 9);
p0.vertex(0, 9);
p0.endShape(CLOSE);
PShape p1 = createShape();
p1.beginShape();
p1.fill(217);
p1.stroke(20);
p1.strokeWeight(1.5);
p1.vertex(0, 1);
p1.vertex(24, 1);
p1.vertex(23, 4);
p1.vertex(26, 4);
p1.vertex(28, 0);
p1.vertex(33, 0);
p1.vertex(27, 15);
p1.vertex(0, 15);
p1.endShape(CLOSE);
PShape p2 = createShape();
p2.beginShape();
p2.fill(192);
p2.noStroke();
p2.fill(192);
p2.vertex(0, 9);
p2.vertex(28, 9);
p2.vertex(26, 14);
p2.vertex(0, 14);
p2.endShape(CLOSE);
pistolShape.addChild(p0);
pistolShape.addChild(p1);
pistolShape.addChild(p2);
}
void renderWeapon(float x, float y) {
if (tee.face == -1) { // flip the weapon
pushMatrix();
scale(-1, 1);
shape(pistolShape, tee.face * x + 23, y-5);
popMatrix();
} else {
shape(pistolShape, x+23, y-5);
}
}
void renderBullets() {
for (PBullet b : bulletsInAir) {
b.render();
}
}
void render(float x, float y) {
renderWeapon(x, y);
renderBullets();
}
}