game.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <game/game.h>
  2. #include <game/storage.h>
  3. /****** Game ******/
  4. /*
  5. Write here the start code for your game, for example: creating a level and so on.
  6. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  7. */
  8. static void game_start(GameManager *game_manager, void *ctx)
  9. {
  10. // Do some initialization here, for example you can load score from storage.
  11. // For simplicity, we will just set it to 0.
  12. GameContext *game_context = ctx;
  13. game_context->fps = game_fps_choices_2[game_fps_index];
  14. game_context->player_context = NULL;
  15. // open the world list from storage, then create a level for each world
  16. char file_path[128];
  17. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  18. FuriString *world_list = flipper_http_load_from_file(file_path);
  19. if (!world_list)
  20. {
  21. FURI_LOG_E("Game", "Failed to load world list");
  22. game_context->levels[0] = game_manager_add_level(game_manager, generic_level("town_world", 0));
  23. game_context->level_count = 1;
  24. return;
  25. }
  26. for (int i = 0; i < 10; i++)
  27. {
  28. FuriString *world_name = get_json_array_value_furi("worlds", i, world_list);
  29. if (!world_name)
  30. {
  31. break;
  32. }
  33. game_context->levels[i] = game_manager_add_level(game_manager, generic_level(furi_string_get_cstr(world_name), i));
  34. furi_string_free(world_name);
  35. game_context->level_count++;
  36. }
  37. furi_string_free(world_list);
  38. // add one enemy
  39. game_context->enemies[0] = level_add_entity(game_context->levels[0], enemy(game_manager,
  40. "naked", // enemy id
  41. 0,
  42. (Vector){10, 10},
  43. (Vector){WORLD_WIDTH / 2 + 11, WORLD_HEIGHT / 2 + 16},
  44. (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 16},
  45. 1,
  46. 32,
  47. 0.5f,
  48. 10,
  49. 100));
  50. // add another enemy
  51. game_context->enemies[1] = level_add_entity(game_context->levels[0], enemy(game_manager,
  52. "naked", // enemy id
  53. 1,
  54. (Vector){10, 10},
  55. (Vector){WORLD_WIDTH / 2 + 11, WORLD_HEIGHT / 2 + 32},
  56. (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 32},
  57. 1,
  58. 32,
  59. 0.5f,
  60. 10,
  61. 100));
  62. game_context->enemy_count = 2;
  63. game_context->current_level = 0;
  64. }
  65. /*
  66. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  67. You don't need to free level, sprites or entities, it will be done automatically.
  68. Also, you don't need to free game_context, it will be done automatically, after this function.
  69. */
  70. static void game_stop(void *ctx)
  71. {
  72. GameContext *game_context = ctx;
  73. save_player_context(game_context->player_context);
  74. if (game_context->player_context)
  75. {
  76. free(game_context->player_context);
  77. }
  78. for (int i = 0; i < game_context->level_count; i++)
  79. {
  80. game_context->levels[i] = NULL;
  81. }
  82. game_context->level_count = 0;
  83. }
  84. /*
  85. Your game configuration, do not rename this variable, but you can change its content here.
  86. */
  87. const Game game = {
  88. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  89. .show_fps = false, // show fps counter on the screen
  90. .always_backlight = true, // keep display backlight always on
  91. .start = game_start, // will be called once, when game starts
  92. .stop = game_stop, // will be called once, when game stops
  93. .context_size = sizeof(GameContext), // size of game context
  94. };