jetpack.c 13 KB

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