player.c 10 KB

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