game.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "game.h"
  2. /****** Entities: Player ******/
  3. // Forward declaration of player_desc, because it's used in player_spawn function.
  4. void player_spawn(Level *level, GameManager *manager)
  5. {
  6. Entity *player = level_add_entity(level, &player_desc);
  7. // Set player position.
  8. // Depends on your game logic, it can be done in start entity function, but also can be done here.
  9. entity_pos_set(player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  10. // Add collision box to player entity
  11. // Box is centered in player x and y, and it's size is 10x10
  12. entity_collider_add_rect(player, 10 + PLAYER_COLLISION_HORIZONTAL, 10 + PLAYER_COLLISION_VERTICAL);
  13. // Get player context
  14. PlayerContext *player_context = entity_context_get(player);
  15. // Load player sprite
  16. player_context->sprite_right = game_manager_sprite_load(manager, "player_right.fxbm");
  17. player_context->sprite_left = game_manager_sprite_load(manager, "player_left.fxbm");
  18. player_context->is_looking_left = false; // player starts looking right
  19. }
  20. // Modify player_update to track direction
  21. static void player_update(Entity *self, GameManager *manager, void *context)
  22. {
  23. PlayerContext *player = (PlayerContext *)context;
  24. InputState input = game_manager_input_get(manager);
  25. Vector pos = entity_pos_get(self);
  26. // Store previous direction
  27. int prev_dx = player->dx;
  28. int prev_dy = player->dy;
  29. // Reset movement deltas each frame
  30. player->dx = 0;
  31. player->dy = 0;
  32. // Handle movement input
  33. if (input.held & GameKeyUp)
  34. {
  35. pos.y -= 2;
  36. player->dy = -1;
  37. }
  38. if (input.held & GameKeyDown)
  39. {
  40. pos.y += 2;
  41. player->dy = 1;
  42. }
  43. if (input.held & GameKeyLeft)
  44. {
  45. pos.x -= 2;
  46. player->dx = -1;
  47. player->is_looking_left = true;
  48. }
  49. if (input.held & GameKeyRight)
  50. {
  51. pos.x += 2;
  52. player->dx = 1;
  53. player->is_looking_left = false;
  54. }
  55. // switch levels if holding OK
  56. if (input.held & GameKeyOk)
  57. {
  58. game_manager_next_level_set(manager, game_manager_current_level_get(manager) == level_tree ? level_example : level_tree);
  59. furi_delay_ms(1000);
  60. return;
  61. }
  62. // Clamp the player's position to stay within world bounds
  63. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  64. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  65. // Update player position
  66. entity_pos_set(self, pos);
  67. // If the player is not moving, retain the last movement direction
  68. if (player->dx == 0 && player->dy == 0)
  69. {
  70. player->dx = prev_dx;
  71. player->dy = prev_dy;
  72. }
  73. // Handle back button to stop the game
  74. if (input.pressed & GameKeyBack)
  75. {
  76. game_manager_game_stop(manager);
  77. }
  78. }
  79. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  80. {
  81. // Get player context
  82. UNUSED(manager);
  83. PlayerContext *player = context;
  84. // Get player position
  85. Vector pos = entity_pos_get(self);
  86. // Draw background (updates camera_x and camera_y)
  87. draw_background(canvas, pos);
  88. // Draw player sprite relative to camera, centered on the player's position
  89. canvas_draw_sprite(
  90. canvas,
  91. player->is_looking_left ? player->sprite_left : player->sprite_right,
  92. pos.x - camera_x - 5, // Center the sprite horizontally
  93. pos.y - camera_y - 5 // Center the sprite vertically
  94. );
  95. }
  96. const EntityDescription player_desc = {
  97. .start = NULL, // called when entity is added to the level
  98. .stop = NULL, // called when entity is removed from the level
  99. .update = player_update, // called every frame
  100. .render = player_render, // called every frame, after update
  101. .collision = NULL, // called when entity collides with another entity
  102. .event = NULL, // called when entity receives an event
  103. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  104. };
  105. /****** Game ******/
  106. /*
  107. Write here the start code for your game, for example: creating a level and so on.
  108. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  109. */
  110. static void game_start(GameManager *game_manager, void *ctx)
  111. {
  112. // Do some initialization here, for example you can load score from storage.
  113. // For simplicity, we will just set it to 0.
  114. GameContext *game_context = ctx;
  115. game_context->score = 0;
  116. // load all levels
  117. // if (level_load_all())
  118. // {
  119. // // loop through all levels and add them to the game
  120. // for (int i = level_count; i > 0; i--)
  121. // {
  122. // levels[i] = game_manager_add_level(game_manager, level_behaviors[i]);
  123. // }
  124. // }
  125. // else
  126. // {
  127. // FURI_LOG_E("Game", "Failed to load levels");
  128. // easy_flipper_dialog("[LEVEL ERROR]", "No level data installed.\n\n\nSettings -> Game ->\nInstall Official Level Pack");
  129. // game_manager_add_level(game_manager, &example_level);
  130. // }
  131. level_tree = game_manager_add_level(game_manager, &tree_level);
  132. level_example = game_manager_add_level(game_manager, &example_level);
  133. }
  134. /*
  135. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  136. You don't need to free level, sprites or entities, it will be done automatically.
  137. Also, you don't need to free game_context, it will be done automatically, after this function.
  138. */
  139. static void game_stop(void *ctx)
  140. {
  141. UNUSED(ctx);
  142. // GameContext *game_context = ctx;
  143. // Do some deinitialization here, for example you can save score to storage.
  144. // For simplicity, we will just print it.
  145. // FURI_LOG_I("Game", "Your score: %lu", game_context->score);
  146. }
  147. float game_fps_int()
  148. {
  149. if (strcmp(game_fps, "30") == 0)
  150. {
  151. return 30.0;
  152. }
  153. else if (strcmp(game_fps, "60") == 0)
  154. {
  155. return 60.0;
  156. }
  157. else if (strcmp(game_fps, "120") == 0)
  158. {
  159. return 120.0;
  160. }
  161. else if (strcmp(game_fps, "240") == 0)
  162. {
  163. return 240.0;
  164. }
  165. else
  166. {
  167. return 60.0;
  168. }
  169. }
  170. /*
  171. Your game configuration, do not rename this variable, but you can change its content here.
  172. */
  173. const Game game = {
  174. .target_fps = 30, // target fps, game will try to keep this value
  175. .show_fps = false, // show fps counter on the screen
  176. .always_backlight = true, // keep display backlight always on
  177. .start = game_start, // will be called once, when game starts
  178. .stop = game_stop, // will be called once, when game stops
  179. .context_size = sizeof(GameContext), // size of game context
  180. };