coin.c 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <jetpack_game_icons.h>
  4. #include <gui/gui.h>
  5. #include "coin.h"
  6. #include "barry.h"
  7. #define PATTERN_MAX_HEIGHT 40
  8. // Patterns
  9. const COIN_PATTERN coin_patterns[] = {
  10. {// Square pattern
  11. .count = 9,
  12. .coins = {{0, 0}, {8, 0}, {16, 0}, {0, 8}, {8, 8}, {16, 8}, {0, 16}, {8, 16}, {16, 16}}},
  13. {// Wavy pattern (approximate sine wave)
  14. .count = 8,
  15. .coins = {{0, 8}, {8, 16}, {16, 24}, {24, 16}, {32, 8}, {40, 0}, {48, 8}, {56, 16}}},
  16. {// Diagonal pattern
  17. .count = 5,
  18. .coins = {{0, 0}, {8, 8}, {16, 16}, {24, 24}, {32, 32}}},
  19. // Add more patterns here
  20. };
  21. void coin_tick(COIN* const coins, BARRY* const barry, int* const total_coins) {
  22. // Move coins towards the player
  23. for(int i = 0; i < COINS_MAX; i++) {
  24. if(coin_colides(&coins[i], barry)) {
  25. coins[i].point.x = 0; // Remove the coin
  26. (*total_coins)++;
  27. }
  28. if(coins[i].point.x > 0) {
  29. coins[i].point.x -= 1; // move left by 1 unit
  30. if(coins[i].point.x < -COIN_WIDTH) { // if the coin is out of screen
  31. coins[i].point.x = 0; // set coin x coordinate to 0 to mark it as "inactive"
  32. }
  33. }
  34. }
  35. }
  36. bool coin_colides(COIN* const coin, BARRY* const barry) {
  37. return !(
  38. barry->point.x > coin->point.x + COIN_WIDTH || // Barry is to the right of the coin
  39. barry->point.x + BARRY_WIDTH < coin->point.x || // Barry is to the left of the coin
  40. barry->point.y > coin->point.y + COIN_WIDTH || // Barry is below the coin
  41. barry->point.y + BARRY_HEIGHT < coin->point.y); // Barry is above the coin
  42. }
  43. void spawn_random_coin(COIN* const coins) {
  44. // Select a random pattern
  45. int pattern_index = rand() % (sizeof(coin_patterns) / sizeof(coin_patterns[0]));
  46. const COIN_PATTERN* pattern = &coin_patterns[pattern_index];
  47. // Count available slots for new coins
  48. int available_slots = 0;
  49. for(int i = 0; i < COINS_MAX; ++i) {
  50. if(coins[i].point.x <= 0) {
  51. ++available_slots;
  52. }
  53. }
  54. // If there aren't enough slots, return without spawning coins
  55. if(available_slots < pattern->count) return;
  56. // Spawn coins according to the selected pattern
  57. int coin_index = 0;
  58. int random_offset = rand() % (SCREEN_HEIGHT - PATTERN_MAX_HEIGHT);
  59. int random_offset_x = rand() % 16;
  60. for(int i = 0; i < pattern->count; ++i) {
  61. // Find an available slot for a new coin
  62. while(coins[coin_index].point.x > 0 && coin_index < COINS_MAX) {
  63. ++coin_index;
  64. }
  65. // If no slot is available, stop spawning coins
  66. if(coin_index == COINS_MAX) break;
  67. // Spawn the coin
  68. coins[coin_index].point.x = SCREEN_WIDTH - 1 + pattern->coins[i].x + random_offset_x;
  69. coins[coin_index].point.y =
  70. random_offset +
  71. pattern->coins[i]
  72. .y; // The pattern is spawned at a random y position, but not too close to the screen edge
  73. }
  74. }
  75. void draw_coins(const COIN* coins, Canvas* const canvas, const GameSprites* sprites) {
  76. canvas_set_color(canvas, ColorBlack);
  77. for(int i = 0; i < COINS_MAX; ++i) {
  78. if(coins[i].point.x > 0) {
  79. canvas_set_color(canvas, ColorBlack);
  80. canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, sprites->coin);
  81. canvas_set_color(canvas, ColorWhite);
  82. canvas_draw_icon(canvas, coins[i].point.x, coins[i].point.y, sprites->coin_infill);
  83. }
  84. }
  85. }