game.c 4.3 KB

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