-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullet.cpp
68 lines (53 loc) · 1.57 KB
/
bullet.cpp
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
#include "bullet.h"
#include "game.h"
#include "player.h"
/**
* First checks whether dynamic arraysize has to be increased
*/
void add_bullet(uint8_t player_bullet,uint8_t shooter_x, uint8_t shooter_y){ // at the moment only a player can fire
if(array_bullets_length == array_bullets_allocated){
array_bullets_allocated += 2;
game.bullets_array = (Bullet *)realloc(game.bullets_array, sizeof(Bullet) * array_bullets_allocated);
}
// is dit correct toevoegen?
Bullet bullet;
bullet.player_bullet = player_bullet;
bullet.x = shooter_x;
bullet.y = shooter_y;
game.bullets_array[array_bullets_length++] = bullet;
}
void move_bullets(){
for(uint8_t i = 0; i < array_bullets_length; i++){
if(game.bullets_array[i].player_bullet){
game.bullets_array[i].x += 1;
} else{
if(game.bullets_array[i].x == 0){
game.bullets_array[i].x = RIGHT_BORDER + 1;
game.bullets_array[i].player_bullet = 1;
} else{
game.bullets_array[i].x -= 1;
}
}
}
//collision detection
general_collision_detection();
}
/**
* removes bullets that aren't needed anymore
*/
void clean_bullets_array(){
for(uint8_t i = 0; i< array_bullets_length; i++){
//if current bullet can be removed, make array counter
while(game.bullets_array[i].x > RIGHT_BORDER){
// if current bullet is the last one in the array
if(i + 1 == array_bullets_length){
//make array_length smaller
array_bullets_length -= 1;
break;
} else {
//else place last bullet on current bullet (to overwrite)
game.bullets_array[i] = game.bullets_array[--array_bullets_length];
}
}
}
}