jetpack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include <stdlib.h>
  2. #include <jetpack_joyride_icons.h>
  3. #include <furi.h>
  4. #include <gui/gui.h>
  5. #include <gui/icon_animation.h>
  6. #include <input/input.h>
  7. #define TAG "Jetpack Joyride"
  8. #define GRAVITY_BOOST -0.3
  9. #define GRAVITY_TICK 0.15
  10. #define SCIENTISTS_MAX 6
  11. #define COINS_MAX 25
  12. typedef struct {
  13. int x;
  14. int y;
  15. } POINT;
  16. typedef struct {
  17. float gravity;
  18. POINT point;
  19. IconAnimation* sprite;
  20. bool isBoosting;
  21. } BARRY;
  22. typedef struct {
  23. float gravity;
  24. POINT point;
  25. } COIN;
  26. typedef struct {
  27. float gravity;
  28. POINT point;
  29. IconAnimation* sprite;
  30. } SCIENTIST;
  31. typedef enum {
  32. GameStateLife,
  33. GameStateGameOver,
  34. } State;
  35. typedef struct {
  36. int points;
  37. int distance;
  38. BARRY barry;
  39. SCIENTIST scientists[SCIENTISTS_MAX];
  40. COIN coins[COINS_MAX];
  41. State state;
  42. FuriMutex* mutex;
  43. } GameState;
  44. typedef enum {
  45. EventTypeTick,
  46. EventTypeKey,
  47. } EventType;
  48. typedef struct {
  49. EventType type;
  50. InputEvent input;
  51. } GameEvent;
  52. static void jetpack_game_random_coins(GameState* const game_state) {
  53. // Check for an available slot for a new coin
  54. for(int i = 0; i < COINS_MAX; ++i) {
  55. if(game_state->coins[i].point.x <= 0 && (rand() % 100) < 5) {
  56. game_state->coins[i].point.x = 127;
  57. game_state->coins[i].point.y = rand() % 64;
  58. break;
  59. }
  60. }
  61. }
  62. static void jetpack_game_state_init(GameState* const game_state) {
  63. UNUSED(game_state);
  64. BARRY barry;
  65. barry.gravity = 0;
  66. barry.point.x = 64;
  67. barry.point.y = 32;
  68. barry.sprite = icon_animation_alloc(&A_barry);
  69. barry.isBoosting = false;
  70. icon_animation_start(barry.sprite);
  71. game_state->barry = barry;
  72. game_state->points = 0;
  73. game_state->distance = 0;
  74. game_state->state = GameStateLife;
  75. memset(game_state->scientists, 0, sizeof(game_state->scientists));
  76. memset(game_state->coins, 0, sizeof(game_state->coins));
  77. }
  78. static void jetpack_game_state_free(GameState* const game_state) {
  79. icon_animation_free(game_state->barry.sprite);
  80. free(game_state);
  81. }
  82. static void jetpack_game_tick(GameState* const game_state) {
  83. if(game_state->state == GameStateLife) {
  84. // Do jetpack things
  85. game_state->barry.gravity += GRAVITY_TICK;
  86. game_state->barry.point.y += game_state->barry.gravity;
  87. // Increment distance
  88. game_state->distance++;
  89. // Move coins towards the player
  90. for(int i = 0; i < COINS_MAX; i++) {
  91. if(game_state->coins[i].point.x > 0) {
  92. game_state->coins[i].point.x -= 1; // move left by 1 unit
  93. if(game_state->coins[i].point.x < -16) { // if the coin is out of screen
  94. game_state->coins[i].point.x =
  95. 0; // set coin x coordinate to 0 to mark it as "inactive"
  96. }
  97. }
  98. }
  99. // Spawn scientists and coins...
  100. jetpack_game_random_coins(game_state);
  101. if(game_state->barry.isBoosting) {
  102. game_state->barry.gravity += GRAVITY_BOOST;
  103. }
  104. }
  105. }
  106. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  107. furi_assert(ctx);
  108. const GameState* game_state = ctx;
  109. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  110. canvas_draw_frame(canvas, 0, 0, 128, 64);
  111. if(game_state->state == GameStateLife) {
  112. // Draw scene
  113. // Draw coins
  114. for(int i = 0; i < COINS_MAX; ++i) {
  115. if(game_state->coins[i].point.x > 0) {
  116. canvas_draw_icon(
  117. canvas, game_state->coins[i].point.x, game_state->coins[i].point.y, &I_coin);
  118. }
  119. }
  120. // Draw barry
  121. canvas_draw_icon_animation(
  122. canvas, game_state->barry.point.x, game_state->barry.point.y, game_state->barry.sprite);
  123. canvas_set_font(canvas, FontSecondary);
  124. char buffer[12];
  125. snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  126. canvas_draw_str_aligned(canvas, 100, 12, AlignCenter, AlignBottom, buffer);
  127. }
  128. if(game_state->state == GameStateGameOver) {
  129. // Show highscore
  130. }
  131. furi_mutex_release(game_state->mutex);
  132. }
  133. static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  134. furi_assert(event_queue);
  135. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  136. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  137. }
  138. static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
  139. furi_assert(event_queue);
  140. GameEvent event = {.type = EventTypeTick};
  141. furi_message_queue_put(event_queue, &event, 0);
  142. }
  143. int32_t jetpack_game_app(void* p) {
  144. UNUSED(p);
  145. int32_t return_code = 0;
  146. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  147. GameState* game_state = malloc(sizeof(GameState));
  148. jetpack_game_state_init(game_state);
  149. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  150. if(!game_state->mutex) {
  151. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  152. return_code = 255;
  153. goto free_and_exit;
  154. }
  155. // Set system callbacks
  156. ViewPort* view_port = view_port_alloc();
  157. view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
  158. view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
  159. FuriTimer* timer =
  160. furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  161. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  162. // Open GUI and register view_port
  163. Gui* gui = furi_record_open(RECORD_GUI);
  164. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  165. GameEvent event;
  166. for(bool processing = true; processing;) {
  167. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  168. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  169. if(event_status == FuriStatusOk) {
  170. // press events
  171. if(event.type == EventTypeKey) {
  172. if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
  173. game_state->barry.isBoosting = false;
  174. }
  175. if(event.input.type == InputTypePress) {
  176. switch(event.input.key) {
  177. case InputKeyUp:
  178. break;
  179. case InputKeyDown:
  180. break;
  181. case InputKeyRight:
  182. break;
  183. case InputKeyLeft:
  184. break;
  185. case InputKeyOk:
  186. if(game_state->state == GameStateGameOver) {
  187. jetpack_game_state_init(game_state);
  188. }
  189. if(game_state->state == GameStateLife) {
  190. // Do something
  191. game_state->barry.isBoosting = true;
  192. }
  193. break;
  194. case InputKeyBack:
  195. processing = false;
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. } else if(event.type == EventTypeTick) {
  202. jetpack_game_tick(game_state);
  203. }
  204. }
  205. view_port_update(view_port);
  206. furi_mutex_release(game_state->mutex);
  207. }
  208. furi_timer_free(timer);
  209. view_port_enabled_set(view_port, false);
  210. gui_remove_view_port(gui, view_port);
  211. furi_record_close(RECORD_GUI);
  212. view_port_free(view_port);
  213. furi_mutex_free(game_state->mutex);
  214. free_and_exit:
  215. jetpack_game_state_free(game_state);
  216. furi_message_queue_free(event_queue);
  217. return return_code;
  218. }