game.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. imu_free(game_context->imu);
  114. game_context->imu = NULL;
  115. // clear current level early
  116. if (game_context->levels[game_context->current_level])
  117. {
  118. level_clear(game_context->levels[game_context->current_level]);
  119. }
  120. if (game_context->game_mode == GAME_MODE_PVP)
  121. {
  122. if (game_context->fhttp)
  123. {
  124. flipper_http_websocket_stop(game_context->fhttp); // close websocket
  125. remove_player_from_lobby(game_context->fhttp); // remove player from lobby
  126. flipper_http_free(game_context->fhttp);
  127. }
  128. }
  129. PlayerContext *player_context = malloc(sizeof(PlayerContext));
  130. if (!player_context)
  131. {
  132. FURI_LOG_E("Game", "Failed to allocate PlayerContext");
  133. return;
  134. }
  135. if (!game_context->ended_early)
  136. easy_flipper_dialog(
  137. "Game Over",
  138. "Thanks for playing FlipWorld!\nHit BACK then wait for\nthe game to save.");
  139. else
  140. easy_flipper_dialog(
  141. "Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
  142. // save the player context
  143. if (load_player_context(player_context))
  144. {
  145. ViewPort *view_port = view_port_alloc();
  146. view_port_draw_callback_set(view_port, thanks, NULL);
  147. Gui *gui = furi_record_open(RECORD_GUI);
  148. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  149. uint32_t tick_count = furi_get_tick();
  150. furi_delay_ms(800);
  151. // save the player context to the API
  152. game_context->fhttp = flipper_http_alloc();
  153. if (game_context->fhttp)
  154. {
  155. save_player_context_api(player_context, game_context->fhttp);
  156. flipper_http_free(game_context->fhttp);
  157. }
  158. const uint32_t delay = 3500;
  159. tick_count = (tick_count + delay) - furi_get_tick();
  160. if (tick_count <= delay)
  161. {
  162. furi_delay_ms(tick_count);
  163. }
  164. easy_flipper_dialog("Game Saved", "Hit BACK to exit.");
  165. flip_world_show_submenu();
  166. gui_remove_view_port(gui, view_port);
  167. furi_record_close(RECORD_GUI);
  168. }
  169. // free the player context
  170. if (player_context)
  171. {
  172. free(player_context);
  173. player_context = NULL;
  174. }
  175. }
  176. /*
  177. Your game configuration, do not rename this variable, but you can change its content here.
  178. */
  179. const Game game = {
  180. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  181. .show_fps = false, // show fps counter on the screen
  182. .always_backlight = true, // keep display backlight always on
  183. .start = game_start, // will be called once, when game starts
  184. .stop = game_stop, // will be called once, when game stops
  185. .context_size = sizeof(GameContext), // size of game context
  186. };