jetpack.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include <storage/storage.h>
  8. #include "includes/point.h"
  9. #include "includes/barry.h"
  10. #include "includes/scientist.h"
  11. #include "includes/particle.h"
  12. #include "includes/coin.h"
  13. #include "includes/missile.h"
  14. #include "includes/background_assets.h"
  15. #include "includes/game_state.h"
  16. #define TAG "Jetpack Joyride"
  17. #define SAVING_DIRECTORY "/ext/apps/Games"
  18. #define SAVING_FILENAME SAVING_DIRECTORY "/jetpack.save"
  19. static GameState* global_state;
  20. typedef enum {
  21. EventTypeTick,
  22. EventTypeKey,
  23. } EventType;
  24. typedef struct {
  25. EventType type;
  26. InputEvent input;
  27. } GameEvent;
  28. typedef struct {
  29. int max_distance;
  30. int max_score;
  31. } SaveGame;
  32. static SaveGame save_game;
  33. static bool storage_game_state_load() {
  34. Storage* storage = furi_record_open(RECORD_STORAGE);
  35. File* file = storage_file_alloc(storage);
  36. uint16_t bytes_readed = 0;
  37. if(storage_file_open(file, SAVING_FILENAME, FSAM_READ, FSOM_OPEN_EXISTING))
  38. bytes_readed = storage_file_read(file, &save_game, sizeof(SaveGame));
  39. storage_file_close(file);
  40. storage_file_free(file);
  41. furi_record_close(RECORD_STORAGE);
  42. return bytes_readed == sizeof(SaveGame);
  43. }
  44. static void storage_game_state_save() {
  45. Storage* storage = furi_record_open(RECORD_STORAGE);
  46. if(storage_common_stat(storage, SAVING_DIRECTORY, NULL) == FSE_NOT_EXIST) {
  47. if(!storage_simply_mkdir(storage, SAVING_DIRECTORY)) {
  48. return;
  49. }
  50. }
  51. File* file = storage_file_alloc(storage);
  52. if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  53. storage_file_write(file, &save_game, sizeof(SaveGame));
  54. }
  55. storage_file_close(file);
  56. storage_file_free(file);
  57. furi_record_close(RECORD_STORAGE);
  58. }
  59. void handle_death() {
  60. global_state->state = GameStateGameOver;
  61. global_state->new_highscore = global_state->distance > save_game.max_distance;
  62. if(global_state->distance > save_game.max_distance) {
  63. save_game.max_distance = global_state->distance;
  64. }
  65. if(global_state->points > save_game.max_score) {
  66. save_game.max_score = global_state->points;
  67. }
  68. storage_game_state_save();
  69. }
  70. static void jetpack_game_state_init(GameState* const game_state) {
  71. UNUSED(game_state);
  72. UNUSED(storage_game_state_save);
  73. BARRY barry;
  74. barry.gravity = 0;
  75. barry.point.x = 32 + 5;
  76. barry.point.y = 32;
  77. barry.isBoosting = false;
  78. GameSprites sprites;
  79. sprites.barry = icon_animation_alloc(&A_barry);
  80. sprites.barry_infill = &I_barry_infill;
  81. sprites.scientist_left = (&I_scientist_left);
  82. sprites.scientist_left_infill = (&I_scientist_left_infill);
  83. sprites.scientist_right = (&I_scientist_right);
  84. sprites.scientist_right_infill = (&I_scientist_right_infill);
  85. sprites.coin = (&I_coin);
  86. sprites.coin_infill = (&I_coin_infill);
  87. sprites.missile = icon_animation_alloc(&A_missile);
  88. sprites.missile_infill = &I_missile_infill;
  89. sprites.alert = icon_animation_alloc(&A_alert);
  90. icon_animation_start(sprites.barry);
  91. icon_animation_start(sprites.missile);
  92. icon_animation_start(sprites.alert);
  93. game_state->barry = barry;
  94. game_state->points = 0;
  95. game_state->distance = 7000;
  96. game_state->new_highscore = false;
  97. game_state->sprites = sprites;
  98. game_state->state = GameStateLife;
  99. game_state->death_handler = handle_death;
  100. memset(game_state->bg_assets, 0, sizeof(game_state->bg_assets));
  101. memset(game_state->scientists, 0, sizeof(game_state->scientists));
  102. memset(game_state->coins, 0, sizeof(game_state->coins));
  103. memset(game_state->particles, 0, sizeof(game_state->particles));
  104. memset(game_state->missiles, 0, sizeof(game_state->missiles));
  105. }
  106. static void jetpack_game_state_free(GameState* const game_state) {
  107. icon_animation_free(game_state->sprites.barry);
  108. icon_animation_free(game_state->sprites.missile);
  109. icon_animation_free(game_state->sprites.alert);
  110. free(game_state);
  111. }
  112. static void jetpack_game_tick(GameState* const game_state) {
  113. if(game_state->state == GameStateGameOver) return;
  114. barry_tick(&game_state->barry);
  115. game_state_tick(game_state);
  116. coin_tick(game_state->coins, &game_state->barry, &game_state->points);
  117. particle_tick(game_state->particles, game_state->scientists, &game_state->points);
  118. scientist_tick(game_state->scientists);
  119. missile_tick(game_state->missiles, &game_state->barry, game_state->death_handler);
  120. background_assets_tick(game_state->bg_assets);
  121. // generate background every 64px aka. ticks
  122. if(game_state->distance % 64 == 0 && rand() % 3 == 0) {
  123. spawn_random_background_asset(game_state->bg_assets);
  124. }
  125. if(game_state->distance % 48 == 0 && rand() % 2 == 0) {
  126. spawn_random_coin(game_state->coins);
  127. }
  128. if(game_state->distance % get_rocket_spawn_distance(game_state->distance) == 0 &&
  129. rand() % 2 == 0) {
  130. spawn_random_missile(game_state->missiles);
  131. }
  132. spawn_random_scientist(game_state->scientists);
  133. if(game_state->barry.isBoosting) {
  134. spawn_random_particles(game_state->particles, &game_state->barry);
  135. }
  136. }
  137. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  138. furi_assert(ctx);
  139. const GameState* game_state = ctx;
  140. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  141. if(game_state->state == GameStateLife) {
  142. canvas_set_bitmap_mode(canvas, false);
  143. draw_background_assets(game_state->bg_assets, canvas, game_state->distance);
  144. canvas_set_bitmap_mode(canvas, true);
  145. draw_coins(game_state->coins, canvas, &game_state->sprites);
  146. draw_scientists(game_state->scientists, canvas, &game_state->sprites);
  147. draw_particles(game_state->particles, canvas);
  148. draw_missiles(game_state->missiles, canvas, &game_state->sprites);
  149. draw_barry(&game_state->barry, canvas, &game_state->sprites);
  150. canvas_set_color(canvas, ColorBlack);
  151. canvas_set_font(canvas, FontSecondary);
  152. char buffer[12];
  153. snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  154. canvas_draw_str_aligned(canvas, 123, 12, AlignRight, AlignBottom, buffer);
  155. snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  156. canvas_draw_str_aligned(canvas, 5, 12, AlignLeft, AlignBottom, buffer);
  157. }
  158. if(game_state->state == GameStateGameOver) {
  159. // Show highscore
  160. char buffer[24];
  161. canvas_set_font(canvas, FontSecondary);
  162. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignTop, "You flew");
  163. snprintf(
  164. buffer,
  165. sizeof(buffer),
  166. game_state->new_highscore ? "%u m (new best)" : "%u m",
  167. game_state->distance);
  168. canvas_set_font(canvas, FontPrimary);
  169. canvas_draw_str_aligned(canvas, 64, 16, AlignCenter, AlignTop, buffer);
  170. canvas_set_font(canvas, FontSecondary);
  171. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, "and collected");
  172. snprintf(buffer, sizeof(buffer), "%u coins", game_state->points);
  173. canvas_set_font(canvas, FontPrimary);
  174. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignTop, buffer);
  175. snprintf(buffer, sizeof(buffer), "Your best was %u m", save_game.max_distance);
  176. canvas_set_font(canvas, FontSecondary);
  177. canvas_draw_str_aligned(canvas, 64, 63, AlignCenter, AlignBottom, buffer);
  178. canvas_draw_frame(canvas, 0, 3, 128, 49);
  179. // char buffer[12];
  180. // snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  181. // canvas_draw_str_aligned(canvas, 123, 12, AlignRight, AlignBottom, buffer);
  182. // snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  183. // canvas_draw_str_aligned(canvas, 5, 12, AlignLeft, AlignBottom, buffer);
  184. // canvas_draw_str_aligned(canvas, 64, 34, AlignCenter, AlignCenter, "Highscore:");
  185. // snprintf(buffer, sizeof(buffer), "Dist: %u", save_game.max_distance);
  186. // canvas_draw_str_aligned(canvas, 123, 50, AlignRight, AlignBottom, buffer);
  187. // snprintf(buffer, sizeof(buffer), "Score: %u", save_game.max_score);
  188. // canvas_draw_str_aligned(canvas, 5, 50, AlignLeft, AlignBottom, buffer);
  189. // canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "boom.");
  190. // if(furi_timer_is_running(game_state->timer)) {
  191. // furi_timer_start(game_state->timer, 0);
  192. // }
  193. }
  194. // canvas_draw_frame(canvas, 0, 0, 128, 64);
  195. furi_mutex_release(game_state->mutex);
  196. }
  197. static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  198. furi_assert(event_queue);
  199. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  200. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  201. }
  202. static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
  203. furi_assert(event_queue);
  204. GameEvent event = {.type = EventTypeTick};
  205. furi_message_queue_put(event_queue, &event, 0);
  206. }
  207. int32_t jetpack_game_app(void* p) {
  208. UNUSED(p);
  209. int32_t return_code = 0;
  210. if(!storage_game_state_load()) {
  211. memset(&save_game, 0, sizeof(save_game));
  212. }
  213. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  214. GameState* game_state = malloc(sizeof(GameState));
  215. global_state = game_state;
  216. jetpack_game_state_init(game_state);
  217. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  218. if(!game_state->mutex) {
  219. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  220. return_code = 255;
  221. goto free_and_exit;
  222. }
  223. // Set system callbacks
  224. ViewPort* view_port = view_port_alloc();
  225. view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
  226. view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
  227. FuriTimer* timer =
  228. furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  229. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  230. game_state->timer = timer;
  231. // Open GUI and register view_port
  232. Gui* gui = furi_record_open(RECORD_GUI);
  233. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  234. GameEvent event;
  235. for(bool processing = true; processing;) {
  236. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  237. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  238. if(event_status == FuriStatusOk) {
  239. // press events
  240. if(event.type == EventTypeKey) {
  241. if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
  242. game_state->barry.isBoosting = false;
  243. }
  244. // Reset highscore, for debug purposes
  245. if(event.input.type == InputTypeLong && event.input.key == InputKeyLeft) {
  246. save_game.max_distance = 0;
  247. save_game.max_score = 0;
  248. storage_game_state_save();
  249. }
  250. if(event.input.type == InputTypePress) {
  251. switch(event.input.key) {
  252. case InputKeyUp:
  253. break;
  254. case InputKeyDown:
  255. break;
  256. case InputKeyRight:
  257. break;
  258. case InputKeyLeft:
  259. break;
  260. case InputKeyOk:
  261. if(game_state->state == GameStateGameOver) {
  262. jetpack_game_state_init(game_state);
  263. }
  264. if(game_state->state == GameStateLife) {
  265. // Do something
  266. game_state->barry.isBoosting = true;
  267. }
  268. break;
  269. case InputKeyBack:
  270. processing = false;
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. } else if(event.type == EventTypeTick) {
  277. jetpack_game_tick(game_state);
  278. }
  279. }
  280. view_port_update(view_port);
  281. furi_mutex_release(game_state->mutex);
  282. }
  283. furi_timer_free(timer);
  284. view_port_enabled_set(view_port, false);
  285. gui_remove_view_port(gui, view_port);
  286. furi_record_close(RECORD_GUI);
  287. view_port_free(view_port);
  288. furi_mutex_free(game_state->mutex);
  289. free_and_exit:
  290. jetpack_game_state_free(game_state);
  291. furi_message_queue_free(event_queue);
  292. return return_code;
  293. }