particle.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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) {
  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. // Check whether the particle lies within the scientist's bounding box
  14. if(!(particles[i].point.x > scientists[j].point.x + SCIENTIST_WIDTH ||
  15. particles[i].point.x < scientists[j].point.x ||
  16. particles[i].point.y > scientists[j].point.y + SCIENTIST_HEIGHT ||
  17. particles[i].point.y < scientists[j].point.y)) {
  18. scientists[j].state = ScientistStateDead;
  19. // (*points) += 2; // Increase the score by 2
  20. }
  21. }
  22. }
  23. if(particles[i].point.x < 0 || particles[i].point.x > SCREEN_WIDTH ||
  24. particles[i].point.y < 0 || particles[i].point.y > SCREEN_HEIGHT) {
  25. particles[i].point.y = 0;
  26. }
  27. }
  28. }
  29. }
  30. void spawn_random_particles(PARTICLE* const particles, BARRY* const barry) {
  31. for(int i = 0; i < PARTICLES_MAX; i++) {
  32. if(particles[i].point.y <= 0) {
  33. particles[i].point.x = barry->point.x + (rand() % 4);
  34. particles[i].point.y = barry->point.y + 14;
  35. break;
  36. }
  37. }
  38. }
  39. void draw_particles(const PARTICLE* particles, Canvas* const canvas) {
  40. canvas_set_color(canvas, ColorBlack);
  41. for(int i = 0; i < PARTICLES_MAX; i++) {
  42. if(particles[i].point.y > 0) {
  43. canvas_draw_line(
  44. canvas,
  45. particles[i].point.x,
  46. particles[i].point.y,
  47. particles[i].point.x,
  48. particles[i].point.y + 3);
  49. }
  50. }
  51. }