game.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <gui/view_holder.h>
  2. #include <game/game.h>
  3. #include <game/storage.h>
  4. #include <alloc/alloc.h>
  5. /****** Game ******/
  6. /*
  7. Write here the start code for your game, for example: creating a level and so on.
  8. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  9. */
  10. static void game_start(GameManager *game_manager, void *ctx)
  11. {
  12. // Do some initialization here, for example you can load score from storage.
  13. // check if enough memory
  14. if (!is_enough_heap(sizeof(GameContext), true))
  15. {
  16. FURI_LOG_E("Game", "Not enough heap memory.. ending game early.");
  17. GameContext *game_context = ctx;
  18. game_context->ended_early = true;
  19. game_manager_game_stop(game_manager); // end game early
  20. return;
  21. }
  22. // For simplicity, we will just set it to 0.
  23. GameContext *game_context = ctx;
  24. game_context->fps = atof_(fps_choices_str[fps_index]);
  25. game_context->player = NULL;
  26. game_context->ended_early = false;
  27. game_context->current_level = 0;
  28. game_context->level_count = 0;
  29. game_context->enemy_count = 0;
  30. game_context->npc_count = 0;
  31. game_context->game_mode = game_mode_index;
  32. // set all levels to NULL
  33. for (int i = 0; i < MAX_LEVELS; i++)
  34. game_context->levels[i] = NULL;
  35. // set all enemies to NULL
  36. for (int i = 0; i < MAX_ENEMIES; i++)
  37. game_context->enemies[i] = NULL;
  38. // set all npcs to NULL
  39. for (int i = 0; i < MAX_NPCS; i++)
  40. game_context->npcs[i] = NULL;
  41. if (game_context->game_mode == GAME_MODE_PVE)
  42. {
  43. // attempt to allocate all levels
  44. for (int i = 0; i < MAX_LEVELS; i++)
  45. {
  46. if (!allocate_level(game_manager, i))
  47. {
  48. if (i == 0)
  49. {
  50. game_context->levels[0] = game_manager_add_level(game_manager, training_world());
  51. game_context->level_count = 1;
  52. }
  53. break;
  54. }
  55. else
  56. game_context->level_count++;
  57. }
  58. }
  59. else if (game_context->game_mode == GAME_MODE_STORY)
  60. {
  61. // show tutorial only for now
  62. game_context->levels[0] = game_manager_add_level(game_manager, training_world());
  63. game_context->level_count = 1;
  64. }
  65. else if (game_context->game_mode == GAME_MODE_PVP)
  66. {
  67. // show pvp
  68. game_context->levels[0] = game_manager_add_level(game_manager, pvp_world());
  69. game_context->level_count = 1;
  70. }
  71. // imu
  72. game_context->imu = imu_alloc();
  73. game_context->imu_present = imu_present(game_context->imu);
  74. // FlipperHTTP
  75. if (game_context->game_mode == GAME_MODE_PVP)
  76. {
  77. // check if enough memory
  78. if (!is_enough_heap(sizeof(FlipperHTTP), true))
  79. {
  80. FURI_LOG_E("Game", "Not enough heap memory.. ending game early.");
  81. game_context->ended_early = true;
  82. game_manager_game_stop(game_manager); // end game early
  83. return;
  84. }
  85. game_context->fhttp = flipper_http_alloc();
  86. if (!game_context->fhttp)
  87. {
  88. FURI_LOG_E("Game", "Failed to allocate FlipperHTTP");
  89. game_context->ended_early = true;
  90. game_manager_game_stop(game_manager); // end game early
  91. return;
  92. }
  93. }
  94. }
  95. static void thanks(Canvas *canvas, void *context)
  96. {
  97. UNUSED(context);
  98. canvas_set_font(canvas, FontPrimary);
  99. canvas_draw_str(canvas, 35, 8, "Saving game");
  100. canvas_set_font(canvas, FontSecondary);
  101. canvas_draw_str(canvas, 0, 50, "Please wait while your");
  102. canvas_draw_str(canvas, 0, 60, "game is saved.");
  103. }
  104. /*
  105. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  106. You don't need to free level, sprites or entities, it will be done automatically.
  107. Also, you don't need to free game_context, it will be done automatically, after this function.
  108. */
  109. static void game_stop(void *ctx)
  110. {
  111. furi_check(ctx);
  112. GameContext *game_context = ctx;
  113. const size_t heap_size = memmgr_heap_get_max_free_block();
  114. imu_free(game_context->imu);
  115. game_context->imu = NULL;
  116. // clear current level early
  117. if (game_context->levels[game_context->current_level])
  118. {
  119. level_clear(game_context->levels[game_context->current_level]);
  120. }
  121. if (game_context->game_mode == GAME_MODE_PVP)
  122. {
  123. if (game_context->fhttp)
  124. {
  125. flipper_http_websocket_stop(game_context->fhttp); // close websocket
  126. remove_player_from_lobby(game_context->fhttp); // remove player from lobby
  127. flipper_http_free(game_context->fhttp);
  128. }
  129. }
  130. PlayerContext *player_context = malloc(sizeof(PlayerContext));
  131. if (!player_context)
  132. {
  133. FURI_LOG_E("Game", "Failed to allocate PlayerContext");
  134. return;
  135. }
  136. if (!game_context->ended_early)
  137. easy_flipper_dialog(
  138. "Game Over",
  139. "Thanks for playing FlipWorld!\nHit BACK then wait for\nthe game to save.");
  140. else
  141. {
  142. char message[128];
  143. snprintf(message, sizeof(message), "Ran out of memory so the\ngame ended early. There were\n%zu bytes free.\n\nHit BACK to exit.", heap_size);
  144. easy_flipper_dialog("Game Over", message);
  145. }
  146. // save the player context
  147. if (load_player_context(player_context))
  148. {
  149. ViewPort *view_port = view_port_alloc();
  150. view_port_draw_callback_set(view_port, thanks, NULL);
  151. Gui *gui = furi_record_open(RECORD_GUI);
  152. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  153. uint32_t tick_count = furi_get_tick();
  154. furi_delay_ms(800);
  155. // save the player context to the API
  156. game_context->fhttp = flipper_http_alloc();
  157. if (game_context->fhttp)
  158. {
  159. save_player_context_api(player_context, game_context->fhttp);
  160. flipper_http_free(game_context->fhttp);
  161. }
  162. const uint32_t delay = 3500;
  163. tick_count = (tick_count + delay) - furi_get_tick();
  164. if (tick_count <= delay)
  165. {
  166. furi_delay_ms(tick_count);
  167. }
  168. easy_flipper_dialog("Game Saved", "Hit BACK to exit.");
  169. flip_world_show_submenu();
  170. gui_remove_view_port(gui, view_port);
  171. furi_record_close(RECORD_GUI);
  172. }
  173. // free the player context
  174. if (player_context)
  175. {
  176. free(player_context);
  177. player_context = NULL;
  178. }
  179. }
  180. /*
  181. Your game configuration, do not rename this variable, but you can change its content here.
  182. */
  183. const Game game = {
  184. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  185. .show_fps = false, // show fps counter on the screen
  186. .always_backlight = true, // keep display backlight always on
  187. .start = game_start, // will be called once, when game starts
  188. .stop = game_stop, // will be called once, when game stops
  189. .context_size = sizeof(GameContext), // size of game context
  190. };