player.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <game/player.h>
  2. #include <game/storage.h>
  3. /****** Entities: Player ******/
  4. static Level *get_next_level(GameManager *manager)
  5. {
  6. Level *current_level = game_manager_current_level_get(manager);
  7. GameContext *game_context = game_manager_game_context_get(manager);
  8. for (int i = 0; i < game_context->level_count; i++)
  9. {
  10. if (game_context->levels[i] == current_level)
  11. {
  12. // check if i+1 is out of bounds, if so, return the first level
  13. game_context->current_level = (i + 1) % game_context->level_count;
  14. return game_context->levels[(i + 1) % game_context->level_count] ? game_context->levels[(i + 1) % game_context->level_count] : game_context->levels[0];
  15. }
  16. }
  17. game_context->current_level = 0;
  18. return game_context->levels[0] ? game_context->levels[0] : game_manager_add_level(manager, generic_level("town_world", 0));
  19. }
  20. void player_spawn(Level *level, GameManager *manager)
  21. {
  22. if (!level || !manager)
  23. {
  24. FURI_LOG_E(TAG, "Invalid arguments to player_spawn");
  25. return;
  26. }
  27. GameContext *game_context = game_manager_game_context_get(manager);
  28. if (!game_context)
  29. {
  30. FURI_LOG_E(TAG, "Failed to get game context");
  31. return;
  32. }
  33. game_context->players[0] = level_add_entity(level, &player_desc);
  34. if (!game_context->players[0])
  35. {
  36. FURI_LOG_E(TAG, "Failed to add player entity to level");
  37. return;
  38. }
  39. // Set player position.
  40. entity_pos_set(game_context->players[0], (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  41. // Box is centered in player x and y, and its size
  42. entity_collider_add_rect(game_context->players[0], 13, 11);
  43. // Get player context
  44. PlayerContext *player_context = entity_context_get(game_context->players[0]);
  45. if (!player_context)
  46. {
  47. FURI_LOG_E(TAG, "Failed to get player context");
  48. return;
  49. }
  50. // player context must be set each level or NULL pointer will be dereferenced
  51. if (!load_player_context(player_context))
  52. {
  53. FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
  54. // Initialize default player context
  55. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
  56. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
  57. player_context->direction = PLAYER_RIGHT; // default direction
  58. player_context->health = 100;
  59. player_context->strength = 10;
  60. player_context->level = 1;
  61. player_context->xp = 0;
  62. player_context->start_position = entity_pos_get(game_context->players[0]);
  63. player_context->attack_timer = 0.1f;
  64. player_context->elapsed_attack_timer = player_context->attack_timer;
  65. player_context->health_regen = 1; // 1 health per second
  66. player_context->elapsed_health_regen = 0;
  67. player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
  68. // Set player username
  69. if (!load_char("Flip-Social-Username", player_context->username, sizeof(player_context->username)))
  70. {
  71. // If loading username fails, default to "Player"
  72. snprintf(player_context->username, sizeof(player_context->username), "Player");
  73. }
  74. game_context->player_context = player_context;
  75. // Save the initialized context
  76. if (!save_player_context(player_context))
  77. {
  78. FURI_LOG_E(TAG, "Failed to save player context after initialization");
  79. }
  80. return;
  81. }
  82. // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
  83. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
  84. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
  85. // Assign loaded player context to game context
  86. game_context->player_context = player_context;
  87. }
  88. // Modify player_update to track direction
  89. static void player_update(Entity *self, GameManager *manager, void *context)
  90. {
  91. PlayerContext *player = (PlayerContext *)context;
  92. InputState input = game_manager_input_get(manager);
  93. Vector pos = entity_pos_get(self);
  94. GameContext *game_context = game_manager_game_context_get(manager);
  95. // apply health regeneration
  96. player->elapsed_health_regen += 1.0f / game_context->fps;
  97. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  98. {
  99. player->health += (player->health_regen + player->health > player->max_health) ? player->max_health - player->health : player->health_regen;
  100. player->elapsed_health_regen = 0;
  101. }
  102. // Increment the elapsed_attack_timer for the player
  103. player->elapsed_attack_timer += 1.0f / game_context->fps;
  104. // Store previous direction
  105. int prev_dx = player->dx;
  106. int prev_dy = player->dy;
  107. // Reset movement deltas each frame
  108. player->dx = 0;
  109. player->dy = 0;
  110. // Handle movement input
  111. if (input.held & GameKeyUp)
  112. {
  113. pos.y -= 2;
  114. player->dy = -1;
  115. player->direction = PLAYER_UP;
  116. game_context->user_input = GameKeyUp;
  117. }
  118. if (input.held & GameKeyDown)
  119. {
  120. pos.y += 2;
  121. player->dy = 1;
  122. player->direction = PLAYER_DOWN;
  123. game_context->user_input = GameKeyDown;
  124. }
  125. if (input.held & GameKeyLeft)
  126. {
  127. pos.x -= 2;
  128. player->dx = -1;
  129. player->direction = PLAYER_LEFT;
  130. game_context->user_input = GameKeyLeft;
  131. }
  132. if (input.held & GameKeyRight)
  133. {
  134. pos.x += 2;
  135. player->dx = 1;
  136. player->direction = PLAYER_RIGHT;
  137. game_context->user_input = GameKeyRight;
  138. }
  139. // Clamp the player's position to stay within world bounds
  140. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  141. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  142. // Update player position
  143. entity_pos_set(self, pos);
  144. // switch levels if holding OK
  145. if (input.held & GameKeyOk)
  146. {
  147. // if all enemies are dead, allow the "OK" button to switch levels
  148. // otherwise the "OK" button will be used to attack
  149. if (game_context->enemy_count == 0)
  150. {
  151. FURI_LOG_I(TAG, "Switching levels");
  152. save_player_context(player);
  153. game_manager_next_level_set(manager, get_next_level(manager));
  154. furi_delay_ms(500);
  155. }
  156. else
  157. {
  158. game_context->user_input = GameKeyOk;
  159. furi_delay_ms(100);
  160. }
  161. return;
  162. }
  163. // If the player is not moving, retain the last movement direction
  164. if (player->dx == 0 && player->dy == 0)
  165. {
  166. player->dx = prev_dx;
  167. player->dy = prev_dy;
  168. player->state = PLAYER_IDLE;
  169. game_context->user_input = -1; // reset user input
  170. }
  171. else
  172. {
  173. player->state = PLAYER_MOVING;
  174. }
  175. // Handle back button to stop the game
  176. if (input.pressed & GameKeyBack)
  177. {
  178. game_manager_game_stop(manager);
  179. }
  180. }
  181. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  182. {
  183. // Get player context
  184. UNUSED(manager);
  185. PlayerContext *player = context;
  186. // Get player position
  187. Vector pos = entity_pos_get(self);
  188. // Draw background (updates camera_x and camera_y)
  189. draw_background(canvas, pos);
  190. // Draw player sprite relative to camera, centered on the player's position
  191. canvas_draw_sprite(
  192. canvas,
  193. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  194. pos.x - camera_x - 5, // Center the sprite horizontally
  195. pos.y - camera_y - 5 // Center the sprite vertically
  196. );
  197. // draw username over player's head
  198. // draw_username(canvas, pos, player->username);
  199. }
  200. const EntityDescription player_desc = {
  201. .start = NULL, // called when entity is added to the level
  202. .stop = NULL, // called when entity is removed from the level
  203. .update = player_update, // called every frame
  204. .render = player_render, // called every frame, after update
  205. .collision = NULL, // called when entity collides with another entity
  206. .event = NULL, // called when entity receives an event
  207. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  208. };
  209. static SpriteContext *sprite_generic_alloc(const char *id, uint8_t width, uint8_t height)
  210. {
  211. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  212. if (!ctx)
  213. {
  214. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  215. return NULL;
  216. }
  217. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  218. ctx->width = width;
  219. ctx->height = height;
  220. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  221. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  222. return ctx;
  223. }
  224. SpriteContext *get_sprite_context(const char *name)
  225. {
  226. if (strcmp(name, "axe") == 0)
  227. {
  228. return sprite_generic_alloc("axe", 15, 11);
  229. }
  230. else if (strcmp(name, "bow") == 0)
  231. {
  232. return sprite_generic_alloc("bow", 13, 11);
  233. }
  234. else if (strcmp(name, "naked") == 0)
  235. {
  236. return sprite_generic_alloc("naked", 10, 10);
  237. }
  238. else if (strcmp(name, "sword") == 0)
  239. {
  240. return sprite_generic_alloc("sword", 15, 11);
  241. }
  242. // If no match is found
  243. FURI_LOG_E("Game", "Sprite not found: %s", name);
  244. return NULL;
  245. }