player.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. if (game_context->last_button == GameKeyUp)
  226. game_context->elapsed_button_timer += 1;
  227. else
  228. game_context->elapsed_button_timer = 0;
  229. if (!game_context->is_menu_open)
  230. {
  231. pos.y -= 2;
  232. player->dy = -1;
  233. player->direction = PLAYER_UP;
  234. }
  235. else
  236. {
  237. // next menu view
  238. // we can only go up to info from settings
  239. game_context->menu_screen = GAME_MENU_INFO;
  240. }
  241. game_context->last_button = GameKeyUp;
  242. }
  243. if (input.held & GameKeyDown)
  244. {
  245. if (game_context->last_button == GameKeyDown)
  246. game_context->elapsed_button_timer += 1;
  247. else
  248. game_context->elapsed_button_timer = 0;
  249. if (!game_context->is_menu_open)
  250. {
  251. pos.y += 2;
  252. player->dy = 1;
  253. player->direction = PLAYER_DOWN;
  254. }
  255. else
  256. {
  257. // next menu view
  258. // we can only go down to more from info
  259. game_context->menu_screen = GAME_MENU_MORE;
  260. }
  261. game_context->last_button = GameKeyDown;
  262. }
  263. if (input.held & GameKeyLeft)
  264. {
  265. if (game_context->last_button == GameKeyLeft)
  266. game_context->elapsed_button_timer += 1;
  267. else
  268. game_context->elapsed_button_timer = 0;
  269. if (!game_context->is_menu_open)
  270. {
  271. pos.x -= 2;
  272. player->dx = -1;
  273. player->direction = PLAYER_LEFT;
  274. }
  275. else
  276. {
  277. // if the menu is open, move the selection left
  278. if (game_context->menu_selection < 1)
  279. {
  280. game_context->menu_selection += 1;
  281. }
  282. }
  283. game_context->last_button = GameKeyLeft;
  284. }
  285. if (input.held & GameKeyRight)
  286. {
  287. if (game_context->last_button == GameKeyRight)
  288. game_context->elapsed_button_timer += 1;
  289. else
  290. game_context->elapsed_button_timer = 0;
  291. if (!game_context->is_menu_open)
  292. {
  293. pos.x += 2;
  294. player->dx = 1;
  295. player->direction = PLAYER_RIGHT;
  296. }
  297. else
  298. {
  299. // if the menu is open, move the selection right
  300. if (game_context->menu_selection < 1)
  301. {
  302. game_context->menu_selection += 1;
  303. }
  304. }
  305. game_context->last_button = GameKeyRight;
  306. }
  307. if (input.held & GameKeyOk)
  308. {
  309. if (game_context->last_button == GameKeyOk)
  310. game_context->elapsed_button_timer += 1;
  311. else
  312. game_context->elapsed_button_timer = 0;
  313. game_context->last_button = GameKeyOk;
  314. // if all enemies are dead, allow the "OK" button to switch levels
  315. // otherwise the "OK" button will be used to attack
  316. if (game_context->enemy_count == 0 && !game_context->is_switching_level)
  317. {
  318. game_context->is_switching_level = true;
  319. save_player_context(player);
  320. game_manager_next_level_set(manager, get_next_level(manager));
  321. return;
  322. }
  323. // if the OK button is held for 1 seconds,show the menu
  324. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  325. {
  326. // open up menu on the INFO screen
  327. game_context->menu_screen = GAME_MENU_INFO;
  328. game_context->menu_selection = 0;
  329. game_context->is_menu_open = true;
  330. FURI_LOG_I(TAG, "Menu opened");
  331. }
  332. }
  333. if (input.held & GameKeyBack)
  334. {
  335. if (game_context->last_button == GameKeyBack)
  336. game_context->elapsed_button_timer += 1;
  337. else
  338. game_context->elapsed_button_timer = 0;
  339. game_context->last_button = GameKeyBack;
  340. if (game_context->is_menu_open)
  341. {
  342. game_context->is_menu_open = false;
  343. FURI_LOG_I(TAG, "Menu closed");
  344. }
  345. // if the back button is held for 1 seconds, stop the game
  346. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  347. {
  348. if (!game_context->is_menu_open)
  349. {
  350. game_manager_game_stop(manager);
  351. return;
  352. }
  353. }
  354. }
  355. // Clamp the player's position to stay within world bounds
  356. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  357. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  358. // Update player position
  359. entity_pos_set(self, pos);
  360. // If the player is not moving, retain the last movement direction
  361. if (player->dx == 0 && player->dy == 0)
  362. {
  363. player->dx = prev_dx;
  364. player->dy = prev_dy;
  365. player->state = PLAYER_IDLE;
  366. }
  367. else
  368. {
  369. player->state = PLAYER_MOVING;
  370. }
  371. }
  372. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  373. {
  374. if (!self || !context || !canvas || !manager)
  375. return;
  376. // Get player context
  377. PlayerContext *player = context;
  378. // Get player position
  379. Vector pos = entity_pos_get(self);
  380. // Draw background (updates camera_x and camera_y)
  381. draw_background(canvas, pos);
  382. // Draw player sprite relative to camera, centered on the player's position
  383. canvas_draw_sprite(
  384. canvas,
  385. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  386. pos.x - camera_x - 5, // Center the sprite horizontally
  387. pos.y - camera_y - 5 // Center the sprite vertically
  388. );
  389. }
  390. const EntityDescription player_desc = {
  391. .start = NULL, // called when entity is added to the level
  392. .stop = NULL, // called when entity is removed from the level
  393. .update = player_update, // called every frame
  394. .render = player_render, // called every frame, after update
  395. .collision = NULL, // called when entity collides with another entity
  396. .event = NULL, // called when entity receives an event
  397. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  398. };
  399. static SpriteContext *sprite_generic_alloc(const char *id, bool is_enemy, uint8_t width, uint8_t height)
  400. {
  401. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  402. if (!ctx)
  403. {
  404. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  405. return NULL;
  406. }
  407. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  408. ctx->width = width;
  409. ctx->height = height;
  410. if (!is_enemy)
  411. {
  412. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  413. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  414. }
  415. else
  416. {
  417. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  418. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  419. }
  420. return ctx;
  421. }
  422. SpriteContext *get_sprite_context(const char *name)
  423. {
  424. if (strcmp(name, "axe") == 0)
  425. {
  426. return sprite_generic_alloc("axe", false, 15, 11);
  427. }
  428. else if (strcmp(name, "bow") == 0)
  429. {
  430. return sprite_generic_alloc("bow", false, 13, 11);
  431. }
  432. else if (strcmp(name, "naked") == 0)
  433. {
  434. return sprite_generic_alloc("naked", false, 10, 10);
  435. }
  436. else if (strcmp(name, "sword") == 0)
  437. {
  438. return sprite_generic_alloc("sword", false, 15, 11);
  439. }
  440. else if (strcmp(name, "cyclops") == 0)
  441. {
  442. return sprite_generic_alloc("cyclops", true, 10, 11);
  443. }
  444. else if (strcmp(name, "ghost") == 0)
  445. {
  446. return sprite_generic_alloc("ghost", true, 15, 15);
  447. }
  448. else if (strcmp(name, "ogre") == 0)
  449. {
  450. return sprite_generic_alloc("ogre", true, 10, 13);
  451. }
  452. // If no match is found
  453. FURI_LOG_E("Game", "Sprite not found: %s", name);
  454. return NULL;
  455. }