-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulletSpawner.pde
91 lines (79 loc) · 2.03 KB
/
BulletSpawner.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
class BulletSpawner
{
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
float timeUntilCheck = 0.5f;
float chanceToShoot = 0.7f;
float elapsed = 0;
void update(float dt)
{
if (elapsed > timeUntilCheck)
{
elapsed = 0;
//we are going to shoot this frame
if (random(0f, 1f) > 1f - chanceToShoot)
{
//choose a side to shoot from
//we will do this by deciding if we are shooting from a
//horizontal edge or vertical edge
//after that we will decide where along the edge we will fire from
boolean vertical = random(0f, 1f) > 0.5f;
float x;
float y;
float w = 16;
float h = 16;
float speed = 256;
PVector dir = new PVector();
if (vertical)
{
boolean top = random(0f, 1f) > 0.5f;
if (top) {
y = -5;
x = random(0, width - w);
} else {
y = height + 5;
x = random(0, width - w);
}
} else {
boolean left = random(0f, 1f) > 0.5f;
if (left) {
x = -5;
y = random(0, height - h);
} else {
x = width + 5;
y = random(0, height - h);
}
}
dir.x = player.baby.x - x;
dir.y = player.baby.y - y;
dir.normalize();
Bullet b = new Bullet(x, y, w, h, speed, dir);
bullets.add(b);
}
} else {
elapsed += dt;
}
for (Bullet b : bullets) {
b.update(dt);
}
}
// diam is a ratio of the screen
void clearRocks(float diam) {
PVector rock, child;
for (int i = 0; i < bullets.size(); i++) {
rock = new PVector(bullets.get(i).x, bullets.get(i).y);
child = new PVector(player.baby.x, player.baby.y);
if (rock.dist(child) < (diam * width) / 2 + bullets.get(i).WIDTH) {
player.rocks++;
bullets.remove(i);
}
}
}
void render()
{
for (Bullet b : bullets)
{
if (b.active)
b.render();
}
}
}