game.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "game.h"
  2. /****** Entities: Player ******/
  3. static Level *levels[10];
  4. static int level_count = 0;
  5. static bool has_pressed_ok = false;
  6. static Level *get_next_level(GameManager *manager)
  7. {
  8. has_pressed_ok = false;
  9. Level *current_level = game_manager_current_level_get(manager);
  10. for (int i = 0; i < level_count; i++)
  11. {
  12. if (levels[i] == current_level)
  13. {
  14. // check if i+1 is out of bounds, if so, return the first level
  15. return levels[(i + 1) % level_count] ? levels[(i + 1) % level_count] : levels[0];
  16. }
  17. }
  18. return levels[0] ? levels[0] : game_manager_add_level(manager, generic_level("town_world", 0));
  19. }
  20. void player_spawn(Level *level, GameManager *manager)
  21. {
  22. Entity *player = level_add_entity(level, &player_desc);
  23. // Set player position.
  24. // Depends on your game logic, it can be done in start entity function, but also can be done here.
  25. entity_pos_set(player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  26. // Add collision box to player entity
  27. // Box is centered in player x and y, and it's size is 10x10
  28. entity_collider_add_rect(player, 10 + PLAYER_COLLISION_HORIZONTAL, 10 + PLAYER_COLLISION_VERTICAL);
  29. // Get player context
  30. PlayerContext *player_context = entity_context_get(player);
  31. // Load player sprite
  32. player_context->sprite_right = game_manager_sprite_load(manager, "player_right.fxbm");
  33. player_context->sprite_left = game_manager_sprite_load(manager, "player_left.fxbm");
  34. player_context->is_looking_left = false; // player starts looking right
  35. }
  36. // Modify player_update to track direction
  37. static void player_update(Entity *self, GameManager *manager, void *context)
  38. {
  39. PlayerContext *player = (PlayerContext *)context;
  40. InputState input = game_manager_input_get(manager);
  41. Vector pos = entity_pos_get(self);
  42. // Store previous direction
  43. int prev_dx = player->dx;
  44. int prev_dy = player->dy;
  45. // Reset movement deltas each frame
  46. player->dx = 0;
  47. player->dy = 0;
  48. // Handle movement input
  49. if (input.held & GameKeyUp)
  50. {
  51. pos.y -= 2;
  52. player->dy = -1;
  53. }
  54. if (input.held & GameKeyDown)
  55. {
  56. pos.y += 2;
  57. player->dy = 1;
  58. }
  59. if (input.held & GameKeyLeft)
  60. {
  61. pos.x -= 2;
  62. player->dx = -1;
  63. player->is_looking_left = true;
  64. }
  65. if (input.held & GameKeyRight)
  66. {
  67. pos.x += 2;
  68. player->dx = 1;
  69. player->is_looking_left = false;
  70. }
  71. // switch levels if holding OK
  72. if (input.held & GameKeyOk)
  73. {
  74. if (!has_pressed_ok)
  75. {
  76. has_pressed_ok = true;
  77. game_manager_next_level_set(manager, get_next_level(manager));
  78. furi_delay_ms(500);
  79. }
  80. return;
  81. }
  82. // Clamp the player's position to stay within world bounds
  83. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  84. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  85. // Update player position
  86. entity_pos_set(self, pos);
  87. // If the player is not moving, retain the last movement direction
  88. if (player->dx == 0 && player->dy == 0)
  89. {
  90. player->dx = prev_dx;
  91. player->dy = prev_dy;
  92. }
  93. // Handle back button to stop the game
  94. if (input.pressed & GameKeyBack)
  95. {
  96. game_manager_game_stop(manager);
  97. }
  98. }
  99. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  100. {
  101. // Get player context
  102. UNUSED(manager);
  103. PlayerContext *player = context;
  104. // Get player position
  105. Vector pos = entity_pos_get(self);
  106. // Draw background (updates camera_x and camera_y)
  107. draw_background(canvas, pos);
  108. // Draw player sprite relative to camera, centered on the player's position
  109. canvas_draw_sprite(
  110. canvas,
  111. player->is_looking_left ? player->sprite_left : player->sprite_right,
  112. pos.x - camera_x - 5, // Center the sprite horizontally
  113. pos.y - camera_y - 5 // Center the sprite vertically
  114. );
  115. }
  116. const EntityDescription player_desc = {
  117. .start = NULL, // called when entity is added to the level
  118. .stop = NULL, // called when entity is removed from the level
  119. .update = player_update, // called every frame
  120. .render = player_render, // called every frame, after update
  121. .collision = NULL, // called when entity collides with another entity
  122. .event = NULL, // called when entity receives an event
  123. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  124. };
  125. /****** Game ******/
  126. /*
  127. Write here the start code for your game, for example: creating a level and so on.
  128. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  129. */
  130. static void game_start(GameManager *game_manager, void *ctx)
  131. {
  132. // Do some initialization here, for example you can load score from storage.
  133. // For simplicity, we will just set it to 0.
  134. GameContext *game_context = ctx;
  135. game_context->level = 1; // we'll load this from SD later
  136. game_context->fps = game_fps_choices_2[game_fps_index];
  137. // open the world list from storage, then create a level for each world
  138. char file_path[128];
  139. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/worlds/world_list.json");
  140. FuriString *world_list = flipper_http_load_from_file(file_path);
  141. if (!world_list)
  142. {
  143. FURI_LOG_E("Game", "Failed to load world list");
  144. levels[0] = game_manager_add_level(game_manager, generic_level("town_world", 0));
  145. level_count = 1;
  146. return;
  147. }
  148. for (int i = 0; i < 10; i++)
  149. {
  150. FuriString *world_name = get_json_array_value_furi("worlds", i, world_list);
  151. if (!world_name)
  152. {
  153. break;
  154. }
  155. levels[i] = game_manager_add_level(game_manager, generic_level(furi_string_get_cstr(world_name), i));
  156. furi_string_free(world_name);
  157. level_count++;
  158. }
  159. furi_string_free(world_list);
  160. // add one enemy
  161. level_add_entity(levels[0], enemy(game_manager,
  162. "player",
  163. 0,
  164. (Vector){10, 10},
  165. (Vector){WORLD_WIDTH / 2 + 11, WORLD_HEIGHT / 2 + 16},
  166. (Vector){WORLD_WIDTH / 2 - 11, WORLD_HEIGHT / 2 + 16},
  167. 1,
  168. 32,
  169. 10,
  170. 10,
  171. 100));
  172. }
  173. /*
  174. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  175. You don't need to free level, sprites or entities, it will be done automatically.
  176. Also, you don't need to free game_context, it will be done automatically, after this function.
  177. */
  178. static void game_stop(void *ctx)
  179. {
  180. UNUSED(ctx);
  181. // If you want to do other final logic (like saving scores), do it here.
  182. // But do NOT free levels[] if the engine manages them.
  183. // Just clear out your pointer array if you like (not strictly necessary)
  184. for (int i = 0; i < level_count; i++)
  185. {
  186. levels[i] = NULL;
  187. }
  188. level_count = 0;
  189. }
  190. /*
  191. Your game configuration, do not rename this variable, but you can change its content here.
  192. */
  193. const Game game = {
  194. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  195. .show_fps = false, // show fps counter on the screen
  196. .always_backlight = true, // keep display backlight always on
  197. .start = game_start, // will be called once, when game starts
  198. .stop = game_stop, // will be called once, when game stops
  199. .context_size = sizeof(GameContext), // size of game context
  200. };