game.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // For simplicity, we will just set it to 0.
  14. GameContext *game_context = ctx;
  15. game_context->fps = atof_(fps_choices_str[fps_index]);
  16. game_context->player_context = NULL;
  17. game_context->ended_early = false;
  18. game_context->current_level = 0;
  19. game_context->level_count = 0;
  20. game_context->enemy_count = 0;
  21. game_context->npc_count = 0;
  22. game_context->game_mode = game_mode_index;
  23. // set all levels to NULL
  24. for (int i = 0; i < MAX_LEVELS; i++)
  25. game_context->levels[i] = NULL;
  26. // set all enemies to NULL
  27. for (int i = 0; i < MAX_ENEMIES; i++)
  28. game_context->enemies[i] = NULL;
  29. // set all npcs to NULL
  30. for (int i = 0; i < MAX_NPCS; i++)
  31. game_context->npcs[i] = NULL;
  32. if (game_context->game_mode == GAME_MODE_PVE)
  33. {
  34. // attempt to allocate all levels
  35. for (int i = 0; i < MAX_LEVELS; i++)
  36. {
  37. if (!allocate_level(game_manager, i))
  38. {
  39. if (i == 0)
  40. {
  41. game_context->levels[0] = game_manager_add_level(game_manager, training_world());
  42. game_context->level_count = 1;
  43. }
  44. break;
  45. }
  46. else
  47. game_context->level_count++;
  48. }
  49. }
  50. else if (game_context->game_mode == GAME_MODE_STORY)
  51. {
  52. // show tutorial only for now
  53. game_context->levels[0] = game_manager_add_level(game_manager, training_world());
  54. game_context->level_count = 1;
  55. }
  56. else if (game_context->game_mode == GAME_MODE_PVP)
  57. {
  58. // show pvp
  59. game_context->levels[0] = game_manager_add_level(game_manager, pvp_world());
  60. game_context->level_count = 1;
  61. }
  62. // imu
  63. game_context->imu = imu_alloc();
  64. game_context->imu_present = imu_present(game_context->imu);
  65. }
  66. static void thanks(Canvas *canvas, void *context)
  67. {
  68. UNUSED(context);
  69. canvas_set_font(canvas, FontPrimary);
  70. canvas_draw_str(canvas, 35, 8, "Saving game");
  71. canvas_set_font(canvas, FontSecondary);
  72. canvas_draw_str(canvas, 0, 50, "Please wait while your");
  73. canvas_draw_str(canvas, 0, 60, "game is saved.");
  74. }
  75. /*
  76. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  77. You don't need to free level, sprites or entities, it will be done automatically.
  78. Also, you don't need to free game_context, it will be done automatically, after this function.
  79. */
  80. static void game_stop(void *ctx)
  81. {
  82. furi_check(ctx);
  83. GameContext *game_context = ctx;
  84. imu_free(game_context->imu);
  85. game_context->imu = NULL;
  86. // clear current level early
  87. if (game_context->levels[game_context->current_level])
  88. {
  89. level_clear(game_context->levels[game_context->current_level]);
  90. }
  91. PlayerContext *player_context = malloc(sizeof(PlayerContext));
  92. if (!player_context)
  93. {
  94. FURI_LOG_E("Game", "Failed to allocate PlayerContext");
  95. return;
  96. }
  97. if (!game_context->ended_early)
  98. easy_flipper_dialog(
  99. "Game Over",
  100. "Thanks for playing FlipWorld!\nHit BACK then wait for\nthe game to save.");
  101. else
  102. easy_flipper_dialog(
  103. "Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
  104. if (load_player_context(player_context))
  105. {
  106. ViewPort *view_port = view_port_alloc();
  107. view_port_draw_callback_set(view_port, thanks, NULL);
  108. Gui *gui = furi_record_open(RECORD_GUI);
  109. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  110. uint32_t tick_count = furi_get_tick();
  111. furi_delay_ms(800);
  112. save_player_context_api(player_context);
  113. const uint32_t delay = 3500;
  114. tick_count = (tick_count + delay) - furi_get_tick();
  115. if (tick_count <= delay)
  116. {
  117. furi_delay_ms(tick_count);
  118. }
  119. easy_flipper_dialog("Game Saved", "Hit BACK to exit.");
  120. flip_world_show_submenu();
  121. gui_remove_view_port(gui, view_port);
  122. furi_record_close(RECORD_GUI);
  123. }
  124. // free the player context
  125. if (player_context)
  126. {
  127. free(player_context);
  128. player_context = NULL;
  129. }
  130. }
  131. /*
  132. Your game configuration, do not rename this variable, but you can change its content here.
  133. */
  134. const Game game = {
  135. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  136. .show_fps = false, // show fps counter on the screen
  137. .always_backlight = true, // keep display backlight always on
  138. .start = game_start, // will be called once, when game starts
  139. .stop = game_stop, // will be called once, when game stops
  140. .context_size = sizeof(GameContext), // size of game context
  141. };