jetpack.c 8.9 KB

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