player.c 13 KB

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