jetpack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #include <stdlib.h>
  2. #include <jetpack_game_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 Game"
  17. #define SAVING_FILENAME APP_DATA_PATH("jetpack.save")
  18. static GameState* global_state;
  19. typedef enum {
  20. EventTypeTick,
  21. EventTypeKey,
  22. } EventType;
  23. typedef struct {
  24. EventType type;
  25. InputEvent input;
  26. } GameEvent;
  27. typedef struct {
  28. int max_distance;
  29. int total_coins;
  30. } SaveGame;
  31. static SaveGame save_game;
  32. static bool storage_game_state_load() {
  33. Storage* storage = furi_record_open(RECORD_STORAGE);
  34. storage_common_migrate(storage, EXT_PATH("apps/Games/jetpack.save"), SAVING_FILENAME);
  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. File* file = storage_file_alloc(storage);
  47. if(storage_file_open(file, SAVING_FILENAME, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  48. storage_file_write(file, &save_game, sizeof(SaveGame));
  49. }
  50. storage_file_close(file);
  51. storage_file_free(file);
  52. furi_record_close(RECORD_STORAGE);
  53. }
  54. void handle_death() {
  55. global_state->state = GameStateGameOver;
  56. global_state->new_highscore = global_state->distance > save_game.max_distance;
  57. if(global_state->distance > save_game.max_distance) {
  58. save_game.max_distance = global_state->distance;
  59. }
  60. save_game.total_coins += global_state->total_coins;
  61. storage_game_state_save();
  62. }
  63. static void jetpack_game_state_init(GameState* const game_state) {
  64. UNUSED(game_state);
  65. UNUSED(storage_game_state_save);
  66. BARRY barry;
  67. barry.gravity = 0;
  68. barry.point.x = 32 + 5;
  69. barry.point.y = 32;
  70. barry.isBoosting = false;
  71. GameSprites sprites;
  72. sprites.barry = icon_animation_alloc(&A_barry);
  73. sprites.barry_infill = &I_barry_infill;
  74. sprites.scientist_left = (&I_scientist_left);
  75. sprites.scientist_left_infill = (&I_scientist_left_infill);
  76. sprites.scientist_right = (&I_scientist_right);
  77. sprites.scientist_right_infill = (&I_scientist_right_infill);
  78. sprites.coin = (&I_coin);
  79. sprites.coin_infill = (&I_coin_infill);
  80. sprites.missile = icon_animation_alloc(&A_missile);
  81. sprites.missile_infill = &I_missile_infill;
  82. sprites.alert = icon_animation_alloc(&A_alert);
  83. icon_animation_start(sprites.barry);
  84. icon_animation_start(sprites.missile);
  85. icon_animation_start(sprites.alert);
  86. game_state->barry = barry;
  87. game_state->total_coins = 0;
  88. game_state->distance = 0;
  89. game_state->new_highscore = false;
  90. game_state->sprites = sprites;
  91. game_state->state = GameStateLife;
  92. game_state->death_handler = handle_death;
  93. memset(game_state->bg_assets, 0, sizeof(game_state->bg_assets));
  94. memset(game_state->scientists, 0, sizeof(game_state->scientists));
  95. memset(game_state->coins, 0, sizeof(game_state->coins));
  96. memset(game_state->particles, 0, sizeof(game_state->particles));
  97. memset(game_state->missiles, 0, sizeof(game_state->missiles));
  98. }
  99. static void jetpack_game_state_free(GameState* const game_state) {
  100. icon_animation_free(game_state->sprites.barry);
  101. icon_animation_free(game_state->sprites.missile);
  102. icon_animation_free(game_state->sprites.alert);
  103. free(game_state);
  104. }
  105. static void jetpack_game_tick(GameState* const game_state) {
  106. if(game_state->state == GameStateGameOver) return;
  107. barry_tick(&game_state->barry);
  108. game_state_tick(game_state);
  109. coin_tick(game_state->coins, &game_state->barry, &game_state->total_coins);
  110. particle_tick(game_state->particles, game_state->scientists);
  111. scientist_tick(game_state->scientists);
  112. missile_tick(game_state->missiles, &game_state->barry, game_state->death_handler);
  113. background_assets_tick(game_state->bg_assets);
  114. // generate background every 64px aka. ticks
  115. if(game_state->distance % 64 == 0 && rand() % 3 == 0) {
  116. spawn_random_background_asset(game_state->bg_assets);
  117. }
  118. if(game_state->distance % 48 == 0 && rand() % 2 == 0) {
  119. spawn_random_coin(game_state->coins);
  120. }
  121. if(game_state->distance % get_rocket_spawn_distance(game_state->distance) == 0 &&
  122. rand() % 2 == 0) {
  123. spawn_random_missile(game_state->missiles);
  124. }
  125. spawn_random_scientist(game_state->scientists);
  126. if(game_state->barry.isBoosting) {
  127. spawn_random_particles(game_state->particles, &game_state->barry);
  128. }
  129. }
  130. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  131. furi_assert(ctx);
  132. const GameState* game_state = ctx;
  133. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  134. if(game_state->state == GameStateLife) {
  135. canvas_set_bitmap_mode(canvas, false);
  136. draw_background_assets(game_state->bg_assets, canvas, game_state->distance);
  137. canvas_set_bitmap_mode(canvas, true);
  138. draw_coins(game_state->coins, canvas, &game_state->sprites);
  139. draw_scientists(game_state->scientists, canvas, &game_state->sprites);
  140. draw_particles(game_state->particles, canvas);
  141. draw_missiles(game_state->missiles, canvas, &game_state->sprites);
  142. draw_barry(&game_state->barry, canvas, &game_state->sprites);
  143. canvas_set_color(canvas, ColorBlack);
  144. canvas_set_font(canvas, FontSecondary);
  145. char buffer[12];
  146. snprintf(buffer, sizeof(buffer), "%u m", game_state->distance / 10);
  147. canvas_draw_str_aligned(canvas, 123, 15, AlignRight, AlignBottom, buffer);
  148. snprintf(buffer, sizeof(buffer), "$%u", game_state->total_coins);
  149. canvas_draw_str_aligned(canvas, 5, 15, AlignLeft, AlignBottom, buffer);
  150. }
  151. if(game_state->state == GameStateGameOver) {
  152. // Show highscore
  153. char buffer[64];
  154. canvas_set_font(canvas, FontSecondary);
  155. canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignTop, "You flew");
  156. snprintf(
  157. buffer,
  158. sizeof(buffer),
  159. game_state->new_highscore ? "%u m (new best)" : "%u m",
  160. game_state->distance / 10);
  161. canvas_set_font(canvas, FontPrimary);
  162. canvas_draw_str_aligned(canvas, 64, 16, AlignCenter, AlignTop, buffer);
  163. canvas_set_font(canvas, FontSecondary);
  164. canvas_draw_str_aligned(canvas, 64, 30, AlignCenter, AlignTop, "and collected");
  165. snprintf(buffer, sizeof(buffer), "$%u", game_state->total_coins);
  166. canvas_set_font(canvas, FontPrimary);
  167. canvas_draw_str_aligned(canvas, 64, 41, AlignCenter, AlignTop, buffer);
  168. snprintf(
  169. buffer,
  170. sizeof(buffer),
  171. "Best: %u m, Tot: $%u",
  172. save_game.max_distance / 10,
  173. save_game.total_coins);
  174. canvas_set_font(canvas, FontSecondary);
  175. canvas_draw_str_aligned(canvas, 64, 63, AlignCenter, AlignBottom, buffer);
  176. canvas_draw_rframe(canvas, 0, 3, 128, 49, 5);
  177. // char buffer[12];
  178. // snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  179. // canvas_draw_str_aligned(canvas, 123, 12, AlignRight, AlignBottom, buffer);
  180. // snprintf(buffer, sizeof(buffer), "Score: %u", game_state->points);
  181. // canvas_draw_str_aligned(canvas, 5, 12, AlignLeft, AlignBottom, buffer);
  182. // canvas_draw_str_aligned(canvas, 64, 34, AlignCenter, AlignCenter, "Highscore:");
  183. // snprintf(buffer, sizeof(buffer), "Dist: %u", save_game.max_distance);
  184. // canvas_draw_str_aligned(canvas, 123, 50, AlignRight, AlignBottom, buffer);
  185. // snprintf(buffer, sizeof(buffer), "Score: %u", save_game.max_score);
  186. // canvas_draw_str_aligned(canvas, 5, 50, AlignLeft, AlignBottom, buffer);
  187. // canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignCenter, "boom.");
  188. // if(furi_timer_is_running(game_state->timer)) {
  189. // furi_timer_start(game_state->timer, 0);
  190. // }
  191. }
  192. // canvas_draw_frame(canvas, 0, 0, 128, 64);
  193. furi_mutex_release(game_state->mutex);
  194. }
  195. static void jetpack_game_input_callback(InputEvent* input_event, void* ctx) {
  196. furi_assert(ctx);
  197. FuriMessageQueue* event_queue = ctx;
  198. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  199. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  200. }
  201. static void jetpack_game_update_timer_callback(void* ctx) {
  202. furi_assert(ctx);
  203. FuriMessageQueue* event_queue = ctx;
  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.total_coins = 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. furi_mutex_release(game_state->mutex);
  281. view_port_update(view_port);
  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. }