game.c 8.8 KB

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