-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.pde
50 lines (42 loc) · 1.11 KB
/
Bullet.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
class Bullet {
static final int TTL = 20;
static final int SIZE = 6;
static final float SPEED = 5;
static final int PLAYER_DAMAGE = 10;
static final int ENEMY_DAMAGE = 5;
PVector position;
PVector direction;
boolean belongsToPlayer;
int timeAlive;
public Bullet(PVector position, PVector direction, boolean belongsToPlayer) {
this.position = position;
this.direction = direction.normalize();
this.belongsToPlayer = belongsToPlayer;
this.timeAlive = 0;
}
int getDamage() {
return belongsToPlayer ? PLAYER_DAMAGE : ENEMY_DAMAGE;
}
boolean isExpired() {
return timeAlive >= TTL;
}
boolean collidesWith(PVector position, int size) {
return this.position.dist(position) < SIZE / 2 + size / 2;
}
PVector getNextPosition() {
return PVector.add(position, PVector.mult(this.direction, SPEED));
}
void update() {
this.position = getNextPosition();
this.timeAlive++;
}
void draw() {
noStroke();
if (belongsToPlayer) {
fill(#3030ff);
} else {
fill(#f4424b);
}
circle(position.x, position.y, SIZE);
}
}