scientist.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "scientist.h"
  2. #include "game_sprites.h"
  3. #include <jetpack_joyride_icons.h>
  4. #include <gui/gui.h>
  5. void scientist_tick(SCIENTIST* const scientists) {
  6. for(int i = 0; i < SCIENTISTS_MAX; i++) {
  7. if(scientists[i].point.x > 0) {
  8. scientists[i].point.x -= 1 - scientists[i].velocity_x; // move based on velocity_x
  9. if(scientists[i].point.x < -16) { // if the scientist is out of screen
  10. scientists[i].point.x =
  11. 0; // set scientist x coordinate to 0 to mark it as "inactive"
  12. }
  13. }
  14. }
  15. }
  16. void spawn_random_scientist(SCIENTIST* const scientists) {
  17. float velocities[] = {-0.5f, 0.0f, 0.5f, -1.0f};
  18. // Check for an available slot for a new scientist
  19. for(int i = 0; i < SCIENTISTS_MAX; ++i) {
  20. if(scientists[i].point.x <= 0 &&
  21. (rand() % 1000) < 10) { // Spawn rate is less frequent than coins
  22. scientists[i].state = ScientistStateAlive;
  23. scientists[i].point.x = 127;
  24. scientists[i].point.y = 49;
  25. scientists[i].velocity_x = velocities[rand() % 4];
  26. break;
  27. }
  28. }
  29. }
  30. void draw_scientists(const SCIENTIST* scientists, Canvas* const canvas, const GameSprites* sprites) {
  31. for(int i = 0; i < SCIENTISTS_MAX; ++i) {
  32. if(scientists[i].point.x > 0) {
  33. if(scientists[i].state == ScientistStateAlive) {
  34. canvas_draw_icon_animation(
  35. canvas,
  36. scientists[i].point.x,
  37. scientists[i].point.y,
  38. scientists[i].velocity_x >= 0 ? sprites->scientist_right :
  39. sprites->scientist_left);
  40. } else {
  41. canvas_draw_icon(
  42. canvas, scientists[i].point.x, scientists[i].point.y + 5, &I_dead_scientist);
  43. }
  44. }
  45. }
  46. }