particle.c 2.5 KB

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