player.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include <game/player.h>
  2. #include <game/storage.h>
  3. /****** Entities: Player ******/
  4. static Level *get_next_level(GameManager *manager)
  5. {
  6. GameContext *game_context = game_manager_game_context_get(manager);
  7. if (!game_context)
  8. {
  9. FURI_LOG_E(TAG, "Failed to get game context");
  10. return NULL;
  11. }
  12. game_context->current_level = game_context->current_level == 0 ? 1 : 0;
  13. if (!game_context->levels[game_context->current_level])
  14. {
  15. if (!allocate_level(manager, game_context->current_level))
  16. {
  17. FURI_LOG_E(TAG, "Failed to allocate level %d", game_context->current_level);
  18. return NULL;
  19. }
  20. }
  21. return game_context->levels[game_context->current_level];
  22. }
  23. // Define constants for XP scaling
  24. #define XP_BASE 100 // Base XP for level 2
  25. #define XP_GROWTH_FACTOR 1.5 // Growth factor per level
  26. #define MAX_LEVEL 100 // Maximum level supported
  27. // Function to get the current level based on XP iteratively
  28. int get_player_level_iterative(uint32_t xp)
  29. {
  30. int level = 1;
  31. uint32_t xp_required = XP_BASE;
  32. while (level < MAX_LEVEL && xp >= xp_required)
  33. {
  34. level++;
  35. xp_required = (uint32_t)(xp_required * XP_GROWTH_FACTOR);
  36. }
  37. return level;
  38. }
  39. void player_spawn(Level *level, GameManager *manager)
  40. {
  41. if (!level || !manager)
  42. {
  43. FURI_LOG_E(TAG, "Invalid arguments to player_spawn");
  44. return;
  45. }
  46. GameContext *game_context = game_manager_game_context_get(manager);
  47. if (!game_context)
  48. {
  49. FURI_LOG_E(TAG, "Failed to get game context");
  50. return;
  51. }
  52. game_context->player = level_add_entity(level, &player_desc);
  53. if (!game_context->player)
  54. {
  55. FURI_LOG_E(TAG, "Failed to add player entity to level");
  56. return;
  57. }
  58. // Set player position.
  59. entity_pos_set(game_context->player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  60. // Box is centered in player x and y, and its size
  61. entity_collider_add_rect(game_context->player, 13, 11);
  62. // Get player context
  63. PlayerContext *player_context = entity_context_get(game_context->player);
  64. if (!player_context)
  65. {
  66. FURI_LOG_E(TAG, "Failed to get player context");
  67. return;
  68. }
  69. // player context must be set each level or NULL pointer will be dereferenced
  70. if (!load_player_context(player_context))
  71. {
  72. FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
  73. // Initialize default player context
  74. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
  75. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
  76. player_context->direction = PLAYER_RIGHT; // default direction
  77. player_context->health = 100;
  78. player_context->strength = 10;
  79. player_context->level = 1;
  80. player_context->xp = 0;
  81. player_context->start_position = entity_pos_get(game_context->player);
  82. player_context->attack_timer = 0.1f;
  83. player_context->elapsed_attack_timer = player_context->attack_timer;
  84. player_context->health_regen = 1; // 1 health per second
  85. player_context->elapsed_health_regen = 0;
  86. player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
  87. // Set player username
  88. if (!load_char("Flip-Social-Username", player_context->username, sizeof(player_context->username)))
  89. {
  90. // If loading username fails, default to "Player"
  91. snprintf(player_context->username, sizeof(player_context->username), "Player");
  92. }
  93. game_context->player_context = player_context;
  94. // Save the initialized context
  95. if (!save_player_context(player_context))
  96. {
  97. FURI_LOG_E(TAG, "Failed to save player context after initialization");
  98. }
  99. return;
  100. }
  101. // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
  102. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_axe_15x11px.fxbm");
  103. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
  104. player_context->start_position = entity_pos_get(game_context->player);
  105. // Update player stats based on XP using iterative method
  106. // Determine the player's level based on XP
  107. player_context->level = get_player_level_iterative(player_context->xp);
  108. // Update strength and max health based on the new level
  109. player_context->strength = 10 + (player_context->level * 5); // 5 strength per level
  110. player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
  111. // Assign loaded player context to game context
  112. game_context->player_context = player_context;
  113. }
  114. // code from Derek Jamison
  115. // eventually we'll add dynamic positioning based on how much pitch/roll is detected
  116. // instead of assigning a fixed value
  117. static int player_x_from_pitch(float pitch)
  118. {
  119. if (pitch > 5.0) // was 9.0
  120. {
  121. return 1;
  122. }
  123. else if (pitch < -5.0) // was -9.0
  124. {
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static int player_y_from_roll(float roll)
  130. {
  131. if (roll > 5.0)
  132. {
  133. return 1;
  134. }
  135. else if (roll < -10.0) // was -20
  136. {
  137. return -1;
  138. }
  139. return 0;
  140. }
  141. static void player_update(Entity *self, GameManager *manager, void *context)
  142. {
  143. if (!self || !manager || !context)
  144. return;
  145. PlayerContext *player = (PlayerContext *)context;
  146. InputState input = game_manager_input_get(manager);
  147. Vector pos = entity_pos_get(self);
  148. GameContext *game_context = game_manager_game_context_get(manager);
  149. // Store previous direction
  150. int prev_dx = player->dx;
  151. int prev_dy = player->dy;
  152. // Reset movement deltas each frame
  153. player->dx = 0;
  154. player->dy = 0;
  155. if (game_context->imu_present)
  156. {
  157. player->dx = player_x_from_pitch(-imu_pitch_get(game_context->imu));
  158. player->dy = player_y_from_roll(-imu_roll_get(game_context->imu));
  159. switch (player->dx)
  160. {
  161. case -1:
  162. player->direction = PLAYER_LEFT;
  163. pos.x -= 1;
  164. break;
  165. case 1:
  166. player->direction = PLAYER_RIGHT;
  167. pos.x += 1;
  168. break;
  169. default:
  170. break;
  171. }
  172. switch (player->dy)
  173. {
  174. case -1:
  175. player->direction = PLAYER_UP;
  176. pos.y -= 1;
  177. break;
  178. case 1:
  179. player->direction = PLAYER_DOWN;
  180. pos.y += 1;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. // Apply health regeneration
  187. player->elapsed_health_regen += 1.0f / game_context->fps;
  188. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  189. {
  190. player->health += (player->health_regen + player->health > player->max_health)
  191. ? (player->max_health - player->health)
  192. : player->health_regen;
  193. player->elapsed_health_regen = 0;
  194. }
  195. // Increment the elapsed_attack_timer for the player
  196. player->elapsed_attack_timer += 1.0f / game_context->fps;
  197. // Handle movement input
  198. if (input.held & GameKeyUp)
  199. {
  200. pos.y -= 2;
  201. player->dy = -1;
  202. player->direction = PLAYER_UP;
  203. game_context->user_input = GameKeyUp;
  204. }
  205. if (input.held & GameKeyDown)
  206. {
  207. pos.y += 2;
  208. player->dy = 1;
  209. player->direction = PLAYER_DOWN;
  210. game_context->user_input = GameKeyDown;
  211. }
  212. if (input.held & GameKeyLeft)
  213. {
  214. pos.x -= 2;
  215. player->dx = -1;
  216. player->direction = PLAYER_LEFT;
  217. game_context->user_input = GameKeyLeft;
  218. }
  219. if (input.held & GameKeyRight)
  220. {
  221. pos.x += 2;
  222. player->dx = 1;
  223. player->direction = PLAYER_RIGHT;
  224. game_context->user_input = GameKeyRight;
  225. }
  226. // Clamp the player's position to stay within world bounds
  227. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  228. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  229. // Update player position
  230. entity_pos_set(self, pos);
  231. // switch levels if holding OK
  232. if (input.pressed & GameKeyOk)
  233. {
  234. FURI_LOG_I(TAG, "Player is pressing OK");
  235. // if all enemies are dead, allow the "OK" button to switch levels
  236. // otherwise the "OK" button will be used to attack
  237. if (game_context->enemy_count == 0)
  238. {
  239. FURI_LOG_I(TAG, "Switching levels");
  240. save_player_context(player);
  241. game_manager_next_level_set(manager, get_next_level(manager));
  242. furi_delay_ms(500);
  243. }
  244. else
  245. {
  246. FURI_LOG_I(TAG, "Player is attacking");
  247. game_context->user_input = GameKeyOk;
  248. // furi_delay_ms(100);
  249. }
  250. FURI_LOG_I(TAG, "Player is done pressing OK");
  251. }
  252. // If the player is not moving, retain the last movement direction
  253. if (player->dx == 0 && player->dy == 0)
  254. {
  255. player->dx = prev_dx;
  256. player->dy = prev_dy;
  257. player->state = PLAYER_IDLE;
  258. game_context->user_input = -1; // reset user input
  259. }
  260. else
  261. {
  262. player->state = PLAYER_MOVING;
  263. }
  264. // Handle back button to stop the game
  265. if (input.pressed & GameKeyBack)
  266. {
  267. game_manager_game_stop(manager);
  268. }
  269. }
  270. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  271. {
  272. UNUSED(manager);
  273. if (!self || !context || !canvas)
  274. return;
  275. // Get player context
  276. PlayerContext *player = context;
  277. // Get player position
  278. Vector pos = entity_pos_get(self);
  279. // Draw background (updates camera_x and camera_y)
  280. draw_background(canvas, pos);
  281. // Draw player sprite relative to camera, centered on the player's position
  282. canvas_draw_sprite(
  283. canvas,
  284. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  285. pos.x - camera_x - 5, // Center the sprite horizontally
  286. pos.y - camera_y - 5 // Center the sprite vertically
  287. );
  288. }
  289. const EntityDescription player_desc = {
  290. .start = NULL, // called when entity is added to the level
  291. .stop = NULL, // called when entity is removed from the level
  292. .update = player_update, // called every frame
  293. .render = player_render, // called every frame, after update
  294. .collision = NULL, // called when entity collides with another entity
  295. .event = NULL, // called when entity receives an event
  296. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  297. };
  298. static SpriteContext *sprite_generic_alloc(const char *id, bool is_enemy, uint8_t width, uint8_t height)
  299. {
  300. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  301. if (!ctx)
  302. {
  303. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  304. return NULL;
  305. }
  306. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  307. ctx->width = width;
  308. ctx->height = height;
  309. if (!is_enemy)
  310. {
  311. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  312. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  313. }
  314. else
  315. {
  316. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  317. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  318. }
  319. return ctx;
  320. }
  321. SpriteContext *get_sprite_context(const char *name)
  322. {
  323. if (strcmp(name, "axe") == 0)
  324. {
  325. return sprite_generic_alloc("axe", false, 15, 11);
  326. }
  327. else if (strcmp(name, "bow") == 0)
  328. {
  329. return sprite_generic_alloc("bow", false, 13, 11);
  330. }
  331. else if (strcmp(name, "naked") == 0)
  332. {
  333. return sprite_generic_alloc("naked", false, 10, 10);
  334. }
  335. else if (strcmp(name, "sword") == 0)
  336. {
  337. return sprite_generic_alloc("sword", false, 15, 11);
  338. }
  339. else if (strcmp(name, "cyclops") == 0)
  340. {
  341. return sprite_generic_alloc("cyclops", true, 10, 11);
  342. }
  343. else if (strcmp(name, "ghost") == 0)
  344. {
  345. return sprite_generic_alloc("ghost", true, 15, 15);
  346. }
  347. else if (strcmp(name, "ogre") == 0)
  348. {
  349. return sprite_generic_alloc("ogre", true, 10, 13);
  350. }
  351. // If no match is found
  352. FURI_LOG_E("Game", "Sprite not found: %s", name);
  353. return NULL;
  354. }