particle.c 2.3 KB

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