1
+ #include <stdlib.h>
2
+
3
+ #include "particle.h"
4
+ #include "scientist.h"
5
+ #include "barry.h"
6
+
7
+ void particle_tick (PARTICLE * const particles , SCIENTIST * const scientists , int * const points ) {
8
+ // Move particles
9
+ for (int i = 0 ; i < PARTICLES_MAX ; i ++ ) {
10
+ if (particles [i ].point .y > 0 ) {
11
+ particles [i ].point .y += PARTICLE_VELOCITY ;
12
+
13
+ // Check collision with scientists
14
+ for (int j = 0 ; j < SCIENTISTS_MAX ; j ++ ) {
15
+ if (scientists [j ].state == ScientistStateAlive && scientists [j ].point .x > 0 ) {
16
+ // Added half the width and height of the scientist sprite to the scientist's x and y respectively
17
+ float scientist_center_x = scientists [j ].point .x + 5.5 ;
18
+ float scientist_center_y = scientists [j ].point .y + 7.5 ;
19
+ if (!(particles [i ].point .x >
20
+ scientist_center_x +
21
+ 5.5 || // particle is to the right of the scientist
22
+ particles [i ].point .x + 11 <
23
+ scientist_center_x -
24
+ 5.5 || // particle is to the left of the scientist
25
+ particles [i ].point .y >
26
+ scientist_center_y + 7.5 || // particle is below the scientist
27
+ particles [i ].point .y + 15 <
28
+ scientist_center_y - 7.5 )) { // particle is above the scientist
29
+ scientists [j ].state = ScientistStateDead ;
30
+ (* points ) += 2 ; // Increase the score by 2
31
+ }
32
+ }
33
+ }
34
+
35
+ if (particles [i ].point .x < 0 || particles [i ].point .x > 128 ||
36
+ particles [i ].point .y < 0 || particles [i ].point .y > 64 ) {
37
+ particles [i ].point .y = 0 ;
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ void spawn_random_particles (PARTICLE * const particles , BARRY * const barry ) {
44
+ for (int i = 0 ; i < PARTICLES_MAX ; i ++ ) {
45
+ if (particles [i ].point .y <= 0 ) {
46
+ particles [i ].point .x = barry -> point .x + (rand () % 7 ) - 3 ;
47
+ particles [i ].point .y = barry -> point .y ;
48
+ break ;
49
+ }
50
+ }
51
+ }
52
+
53
+ void draw_particles (const PARTICLE * particles , Canvas * const canvas ) {
54
+ for (int i = 0 ; i < PARTICLES_MAX ; i ++ ) {
55
+ if (particles [i ].point .y > 0 ) {
56
+ canvas_draw_line (
57
+ canvas ,
58
+ particles [i ].point .x ,
59
+ particles [i ].point .y ,
60
+ particles [i ].point .x ,
61
+ particles [i ].point .y + 3 );
62
+ }
63
+ }
64
+ }
0 commit comments