game.c 11 KB

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