jetpack.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // Do jetpack things
  84. game_state->barry.gravity += GRAVITY_TICK;
  85. game_state->barry.point.y += game_state->barry.gravity;
  86. // Increment distance
  87. game_state->distance++;
  88. // Move coins towards the player
  89. for(int i = 0; i < COINS_MAX; i++) {
  90. if(game_state->coins[i].point.x > 0) {
  91. game_state->coins[i].point.x -= 1; // move left by 1 unit
  92. if(game_state->coins[i].point.x < -16) { // if the coin is out of screen
  93. game_state->coins[i].point.x =
  94. 0; // set coin x coordinate to 0 to mark it as "inactive"
  95. }
  96. }
  97. }
  98. // Spawn scientists and coins...
  99. jetpack_game_random_coins(game_state);
  100. // Sprite height of Barry
  101. int sprite_height = 15;
  102. // Constrain barry's height within sprite_height and 64 - sprite_height
  103. if(game_state->barry.point.y > (64 - sprite_height)) {
  104. game_state->barry.point.y = 64 - sprite_height;
  105. game_state->barry.gravity = 0; // stop upward momentum
  106. } else if(game_state->barry.point.y < 0) {
  107. game_state->barry.point.y = 0;
  108. game_state->barry.gravity = 0; // stop downward momentum
  109. }
  110. // spawn scientists and coins...
  111. jetpack_game_random_coins(game_state);
  112. if(game_state->barry.isBoosting) {
  113. game_state->barry.gravity += GRAVITY_BOOST;
  114. }
  115. }
  116. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  117. furi_assert(ctx);
  118. const GameState* game_state = ctx;
  119. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  120. canvas_draw_frame(canvas, 0, 0, 128, 64);
  121. if(game_state->state == GameStateLife) {
  122. // Draw scene
  123. // Draw coins
  124. for(int i = 0; i < COINS_MAX; ++i) {
  125. if(game_state->coins[i].point.x > 0) {
  126. canvas_draw_icon(
  127. canvas, game_state->coins[i].point.x, game_state->coins[i].point.y, &I_coin);
  128. }
  129. }
  130. // Draw barry
  131. canvas_draw_icon_animation(
  132. canvas, game_state->barry.point.x, game_state->barry.point.y, game_state->barry.sprite);
  133. canvas_set_font(canvas, FontSecondary);
  134. char buffer[12];
  135. snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  136. canvas_draw_str_aligned(canvas, 100, 12, AlignCenter, AlignBottom, buffer);
  137. }
  138. if(game_state->state == GameStateGameOver) {
  139. // Show highscore
  140. }
  141. furi_mutex_release(game_state->mutex);
  142. }
  143. static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  144. furi_assert(event_queue);
  145. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  146. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  147. }
  148. static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
  149. furi_assert(event_queue);
  150. GameEvent event = {.type = EventTypeTick};
  151. furi_message_queue_put(event_queue, &event, 0);
  152. }
  153. int32_t jetpack_game_app(void* p) {
  154. UNUSED(p);
  155. int32_t return_code = 0;
  156. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  157. GameState* game_state = malloc(sizeof(GameState));
  158. jetpack_game_state_init(game_state);
  159. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  160. if(!game_state->mutex) {
  161. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  162. return_code = 255;
  163. goto free_and_exit;
  164. }
  165. // Set system callbacks
  166. ViewPort* view_port = view_port_alloc();
  167. view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
  168. view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
  169. FuriTimer* timer =
  170. furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  171. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  172. // Open GUI and register view_port
  173. Gui* gui = furi_record_open(RECORD_GUI);
  174. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  175. GameEvent event;
  176. for(bool processing = true; processing;) {
  177. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  178. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  179. if(event_status == FuriStatusOk) {
  180. // press events
  181. if(event.type == EventTypeKey) {
  182. if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
  183. game_state->barry.isBoosting = false;
  184. }
  185. if(event.input.type == InputTypePress) {
  186. switch(event.input.key) {
  187. case InputKeyUp:
  188. break;
  189. case InputKeyDown:
  190. break;
  191. case InputKeyRight:
  192. break;
  193. case InputKeyLeft:
  194. break;
  195. case InputKeyOk:
  196. if(game_state->state == GameStateGameOver) {
  197. jetpack_game_state_init(game_state);
  198. }
  199. if(game_state->state == GameStateLife) {
  200. // Do something
  201. game_state->barry.isBoosting = true;
  202. }
  203. break;
  204. case InputKeyBack:
  205. processing = false;
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. } else if(event.type == EventTypeTick) {
  212. jetpack_game_tick(game_state);
  213. }
  214. }
  215. view_port_update(view_port);
  216. furi_mutex_release(game_state->mutex);
  217. }
  218. furi_timer_free(timer);
  219. view_port_enabled_set(view_port, false);
  220. gui_remove_view_port(gui, view_port);
  221. furi_record_close(RECORD_GUI);
  222. view_port_free(view_port);
  223. furi_mutex_free(game_state->mutex);
  224. free_and_exit:
  225. jetpack_game_state_free(game_state);
  226. furi_message_queue_free(event_queue);
  227. return return_code;
  228. }