game.c 8.0 KB

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