-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBullet.pde
109 lines (90 loc) · 2.39 KB
/
PBullet.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
class PBullet {
// TBD: generalize the Bullet type
Pistol weapon;
float speed = 12;
// 0 not fired
// 1 facing right
// -1 facing left
int face;
PVector pos;
PVector vel;
// bullet.pos - enemy.pos
// This is used by enemy's sensor
// Stored in this class is to improve perf and simplify code
// though in theory it is probably enemy's responbility to maintain the info
PVector bulletEnemyDist;
boolean isShot = false;
/* <drawing> */
PShape bulletRShape, bulletLShape;
/* </drawing> */
// TBD: need a Weapon absract class to generalize
PBullet(Pistol p) {
weapon = p;
pos = new PVector();
vel = new PVector();
bulletEnemyDist = new PVector();
loadBulletShape();
}
void prepare() {
face = weapon.tee.face;
pos.set(weapon.tee.pos.x + 65 * face, weapon.tee.pos.y);
vel.set(speed * face, 0);
isShot = true;
}
// Bullet - Enemy relative distance
void updateBulletEnemyDist(float x, float y) {
bulletEnemyDist.set(pos.x - x, pos.y - y);
}
void reset() {
face = 0;
pos.set(0, 0);
vel.set(0, 0);
bulletEnemyDist.set(0, 0);
isShot = false;
}
void move() {
if (isShot) {
float newPosX = pos.x + vel.x;
if (newPosX > terrain.rightBoundary || newPosX < terrain.leftBoundary) {
reset();
} else {
pos.x = newPosX;
}
}
}
void loadBulletShape() {
// Bullet facing right
bulletRShape = createShape(GROUP);
PShape r0 = createShape(ELLIPSE, 0, 0, 20, 15);
r0.setFill(color(253, 240, 156));
r0.setStroke(color(20));
r0.setStrokeWeight(1.8);
PShape r1 = createShape(ELLIPSE, 4, -2, 8, 6);
r1.setFill(color(255, 248, 198));
r1.setStroke(color(20));
r1.setStrokeWeight(0);
bulletRShape.addChild(r0);
bulletRShape.addChild(r1);
// Bullet facing left
bulletLShape = createShape(GROUP);
PShape l0 = createShape(ELLIPSE, 0, 0, 20, 15);
l0.setFill(color(253, 240, 156));
l0.setStroke(color(20));
l0.setStrokeWeight(1.8);
PShape l1 = createShape(ELLIPSE, -4, -2, 8, 6);
l1.setFill(color(255, 248, 198));
l1.setStroke(color(20));
l1.setStrokeWeight(0);
bulletLShape.addChild(l0);
bulletLShape.addChild(l1);
}
void render() {
if (isShot) {
if (face == 1) {
shape(bulletRShape, pos.x, pos.y);
} else {
shape(bulletLShape, pos.x, pos.y);
}
}
}
}