player.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. #include <game/player.h>
  2. #include <game/storage.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <engine/entity_i.h>
  7. /****** Entities: Player ******/
  8. static Level *next_level(GameManager *manager)
  9. {
  10. GameContext *game_context = game_manager_game_context_get(manager);
  11. if (!game_context)
  12. {
  13. FURI_LOG_E(TAG, "Failed to get game context");
  14. game_context->is_switching_level = false;
  15. return NULL;
  16. }
  17. // check if there are more levels to load
  18. if (game_context->current_level + 1 >= game_context->level_count)
  19. {
  20. game_context->current_level = 0;
  21. if (!game_context->levels[game_context->current_level])
  22. {
  23. if (!allocate_level(manager, game_context->current_level))
  24. {
  25. FURI_LOG_E(TAG, "Failed to allocate level %d", game_context->current_level);
  26. game_context->is_switching_level = false;
  27. furi_delay_ms(100);
  28. return NULL;
  29. }
  30. }
  31. game_context->is_switching_level = false;
  32. furi_delay_ms(100);
  33. return game_context->levels[game_context->current_level];
  34. }
  35. for (int i = game_context->current_level + 1; i < game_context->level_count; i++)
  36. {
  37. if (!game_context->levels[i])
  38. {
  39. if (!allocate_level(manager, i))
  40. {
  41. FURI_LOG_E(TAG, "Failed to allocate level %d", i);
  42. game_context->is_switching_level = false;
  43. furi_delay_ms(100);
  44. return NULL;
  45. }
  46. }
  47. game_context->current_level = i;
  48. game_context->is_switching_level = false;
  49. furi_delay_ms(100);
  50. return game_context->levels[i];
  51. }
  52. game_context->is_switching_level = false;
  53. furi_delay_ms(100);
  54. return NULL;
  55. }
  56. // Update player stats based on XP using iterative method
  57. static int get_player_level_iterative(uint32_t xp)
  58. {
  59. int level = 1;
  60. uint32_t xp_required = 100; // Base XP for level 2
  61. while (level < 100 && xp >= xp_required) // Maximum level supported
  62. {
  63. level++;
  64. xp_required = (uint32_t)(xp_required * 1.5); // 1.5 growth factor per level
  65. }
  66. return level;
  67. }
  68. void player_spawn(Level *level, GameManager *manager)
  69. {
  70. if (!level || !manager)
  71. {
  72. FURI_LOG_E(TAG, "Invalid arguments to player_spawn");
  73. return;
  74. }
  75. GameContext *game_context = game_manager_game_context_get(manager);
  76. if (!game_context)
  77. {
  78. FURI_LOG_E(TAG, "Failed to get game context");
  79. return;
  80. }
  81. game_context->player = level_add_entity(level, &player_desc);
  82. if (!game_context->player)
  83. {
  84. FURI_LOG_E(TAG, "Failed to add player entity to level");
  85. return;
  86. }
  87. // Set player position.
  88. entity_pos_set(game_context->player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  89. // Get player context
  90. PlayerContext *pctx = entity_context_get(game_context->player);
  91. if (!pctx)
  92. {
  93. FURI_LOG_E(TAG, "Failed to get player context");
  94. return;
  95. }
  96. SpriteContext *sprite_context = get_sprite_context(player_sprite_choices[player_sprite_index]);
  97. if (!sprite_context)
  98. {
  99. FURI_LOG_E(TAG, "Failed to get sprite context");
  100. return;
  101. }
  102. // add a collider to the player entity
  103. entity_collider_add_rect(game_context->player, sprite_context->width, sprite_context->height);
  104. // player context must be set each level or NULL pointer will be dereferenced
  105. if (!load_player_context(pctx))
  106. {
  107. FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
  108. // Initialize default player context
  109. pctx->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  110. pctx->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  111. pctx->direction = ENTITY_RIGHT; // default direction
  112. pctx->left = false; // default sprite direction
  113. pctx->health = 100;
  114. pctx->strength = 10;
  115. pctx->level = 1;
  116. pctx->xp = 0;
  117. pctx->start_position = entity_pos_get(game_context->player);
  118. pctx->attack_timer = 0.1f;
  119. pctx->elapsed_attack_timer = pctx->attack_timer;
  120. pctx->health_regen = 1; // 1 health per second
  121. pctx->elapsed_health_regen = 0;
  122. pctx->max_health = 100 + ((pctx->level - 1) * 10); // 10 health per level
  123. // Set player username
  124. if (!load_char("Flip-Social-Username", pctx->username, sizeof(pctx->username)))
  125. {
  126. // check if data/player/username
  127. if (!load_char("player/username", pctx->username, sizeof(pctx->username)))
  128. {
  129. // If loading username fails, default to "Player"
  130. snprintf(pctx->username, sizeof(pctx->username), "Player");
  131. }
  132. }
  133. game_context->player_context = pctx;
  134. // Save the initialized context
  135. if (!save_player_context(pctx))
  136. {
  137. FURI_LOG_E(TAG, "Failed to save player context after initialization");
  138. }
  139. free(sprite_context);
  140. return;
  141. }
  142. // Load player sprite
  143. pctx->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  144. pctx->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  145. pctx->start_position = entity_pos_get(game_context->player);
  146. // Determine the player's level based on XP
  147. pctx->level = get_player_level_iterative(pctx->xp);
  148. // Update strength and max health based on the new level
  149. pctx->strength = 10 + (pctx->level * 1); // 1 strength per level
  150. pctx->max_health = 100 + ((pctx->level - 1) * 10); // 10 health per level
  151. // set the player's left sprite direction
  152. pctx->left = pctx->direction == ENTITY_LEFT ? true : false;
  153. // Assign loaded player context to game context
  154. game_context->player_context = pctx;
  155. free(sprite_context);
  156. }
  157. static int vgm_increase(float value, float increase)
  158. {
  159. const int val = abs((int)(round(value + increase) / 2));
  160. return val < 1 ? 1 : val;
  161. }
  162. static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
  163. {
  164. const float pitch = -imu_pitch_get(imu);
  165. const float roll = -imu_roll_get(imu);
  166. const float min_x = atof_(vgm_levels[vgm_x_index]) + 5.0; // minimum of 3
  167. const float min_y = atof_(vgm_levels[vgm_y_index]) + 5.0; // minimum of 3
  168. if (pitch > min_x)
  169. {
  170. pos->x += vgm_increase(pitch, min_x);
  171. player->dx = 1;
  172. player->direction = ENTITY_RIGHT;
  173. }
  174. else if (pitch < -min_x)
  175. {
  176. pos->x += -vgm_increase(pitch, min_x);
  177. player->dx = -1;
  178. player->direction = ENTITY_LEFT;
  179. }
  180. if (roll > min_y)
  181. {
  182. pos->y += vgm_increase(roll, min_y);
  183. player->dy = 1;
  184. player->direction = ENTITY_DOWN;
  185. }
  186. else if (roll < -min_y)
  187. {
  188. pos->y += -vgm_increase(roll, min_y);
  189. player->dy = -1;
  190. player->direction = ENTITY_UP;
  191. }
  192. }
  193. static void player_update(Entity *self, GameManager *manager, void *context)
  194. {
  195. if (!self || !manager || !context)
  196. return;
  197. PlayerContext *player = (PlayerContext *)context;
  198. InputState input = game_manager_input_get(manager);
  199. Vector pos = entity_pos_get(self);
  200. player->old_position = pos;
  201. GameContext *game_context = game_manager_game_context_get(manager);
  202. // Determine the player's level based on XP
  203. player->level = get_player_level_iterative(player->xp);
  204. player->strength = 10 + (player->level * 1); // 1 strength per level
  205. player->max_health = 100 + ((player->level - 1) * 10); // 10 health per level
  206. // Store previous direction
  207. int prev_dx = player->dx;
  208. int prev_dy = player->dy;
  209. // Reset movement deltas each frame
  210. player->dx = 0;
  211. player->dy = 0;
  212. if (game_context->imu_present)
  213. {
  214. // update position using the IMU
  215. vgm_direction(game_context->imu, player, &pos);
  216. }
  217. // Apply health regeneration
  218. player->elapsed_health_regen += 1.0f / game_context->fps;
  219. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  220. {
  221. player->health += (player->health_regen + player->health > player->max_health)
  222. ? (player->max_health - player->health)
  223. : player->health_regen;
  224. player->elapsed_health_regen = 0;
  225. }
  226. // Increment the elapsed_attack_timer for the player
  227. player->elapsed_attack_timer += 1.0f / game_context->fps;
  228. // Handle movement input
  229. if (input.held & GameKeyUp)
  230. {
  231. if (game_context->last_button == GameKeyUp)
  232. game_context->elapsed_button_timer += 1;
  233. else
  234. game_context->elapsed_button_timer = 0;
  235. if (!game_context->is_menu_open)
  236. {
  237. pos.y -= (1 + game_context->icon_offset);
  238. player->dy = -1;
  239. player->direction = ENTITY_UP;
  240. }
  241. else
  242. {
  243. // next menu view
  244. // we can only go up to info from settings
  245. game_context->menu_screen = GAME_MENU_INFO;
  246. }
  247. game_context->last_button = GameKeyUp;
  248. }
  249. if (input.held & GameKeyDown)
  250. {
  251. if (game_context->last_button == GameKeyDown)
  252. game_context->elapsed_button_timer += 1;
  253. else
  254. game_context->elapsed_button_timer = 0;
  255. if (!game_context->is_menu_open)
  256. {
  257. pos.y += (1 + game_context->icon_offset);
  258. player->dy = 1;
  259. player->direction = ENTITY_DOWN;
  260. }
  261. else
  262. {
  263. // next menu view
  264. // we can only go down to more from info
  265. game_context->menu_screen = GAME_MENU_MORE;
  266. }
  267. game_context->last_button = GameKeyDown;
  268. }
  269. if (input.held & GameKeyLeft)
  270. {
  271. if (game_context->last_button == GameKeyLeft)
  272. game_context->elapsed_button_timer += 1;
  273. else
  274. game_context->elapsed_button_timer = 0;
  275. if (!game_context->is_menu_open)
  276. {
  277. pos.x -= (1 + game_context->icon_offset);
  278. player->dx = -1;
  279. player->direction = ENTITY_LEFT;
  280. }
  281. else
  282. {
  283. // if the menu is open, move the selection left
  284. if (game_context->menu_selection < 1)
  285. {
  286. game_context->menu_selection += 1;
  287. }
  288. }
  289. game_context->last_button = GameKeyLeft;
  290. }
  291. if (input.held & GameKeyRight)
  292. {
  293. if (game_context->last_button == GameKeyRight)
  294. game_context->elapsed_button_timer += 1;
  295. else
  296. game_context->elapsed_button_timer = 0;
  297. if (!game_context->is_menu_open)
  298. {
  299. pos.x += (1 + game_context->icon_offset);
  300. player->dx = 1;
  301. player->direction = ENTITY_RIGHT;
  302. }
  303. else
  304. {
  305. // if the menu is open, move the selection right
  306. if (game_context->menu_selection < 1)
  307. {
  308. game_context->menu_selection += 1;
  309. }
  310. }
  311. game_context->last_button = GameKeyRight;
  312. }
  313. if (input.held & GameKeyOk)
  314. {
  315. if (game_context->last_button == GameKeyOk)
  316. game_context->elapsed_button_timer += 1;
  317. else
  318. game_context->elapsed_button_timer = 0;
  319. game_context->last_button = GameKeyOk;
  320. // if all enemies are dead, allow the "OK" button to switch levels
  321. // otherwise the "OK" button will be used to attack
  322. if (game_context->enemy_count == 0 && !game_context->is_switching_level)
  323. {
  324. game_context->is_switching_level = true;
  325. save_player_context(player);
  326. furi_delay_ms(100);
  327. game_manager_next_level_set(manager, next_level(manager));
  328. return;
  329. }
  330. // if the OK button is held for 1 seconds,show the menu
  331. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  332. {
  333. // open up menu on the INFO screen
  334. game_context->menu_screen = GAME_MENU_INFO;
  335. game_context->menu_selection = 0;
  336. game_context->is_menu_open = true;
  337. }
  338. }
  339. if (input.held & GameKeyBack)
  340. {
  341. if (game_context->last_button == GameKeyBack)
  342. game_context->elapsed_button_timer += 1;
  343. else
  344. game_context->elapsed_button_timer = 0;
  345. game_context->last_button = GameKeyBack;
  346. if (game_context->is_menu_open)
  347. {
  348. game_context->is_menu_open = false;
  349. }
  350. // if the back button is held for 1 seconds, stop the game
  351. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  352. {
  353. if (!game_context->is_menu_open)
  354. {
  355. save_player_context(player);
  356. furi_delay_ms(100);
  357. game_manager_game_stop(manager);
  358. return;
  359. }
  360. }
  361. }
  362. // adjust tutorial step
  363. if (game_context->game_mode == GAME_MODE_STORY)
  364. {
  365. switch (game_context->tutorial_step)
  366. {
  367. case 0:
  368. if (input.held & GameKeyLeft)
  369. game_context->tutorial_step++;
  370. break;
  371. case 1:
  372. if (input.held & GameKeyRight)
  373. game_context->tutorial_step++;
  374. break;
  375. case 2:
  376. if (input.held & GameKeyUp)
  377. game_context->tutorial_step++;
  378. break;
  379. case 3:
  380. if (input.held & GameKeyDown)
  381. game_context->tutorial_step++;
  382. break;
  383. case 5:
  384. if (input.held & GameKeyOk && game_context->is_menu_open)
  385. game_context->tutorial_step++;
  386. break;
  387. case 6:
  388. if (input.held & GameKeyBack)
  389. game_context->tutorial_step++;
  390. break;
  391. case 7:
  392. if (input.held & GameKeyBack)
  393. game_context->tutorial_step++;
  394. break;
  395. }
  396. }
  397. // Clamp the player's position to stay within world bounds
  398. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  399. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  400. // Update player position
  401. entity_pos_set(self, pos);
  402. // If the player is not moving, retain the last movement direction
  403. if (player->dx == 0 && player->dy == 0)
  404. {
  405. player->dx = prev_dx;
  406. player->dy = prev_dy;
  407. player->state = ENTITY_IDLE;
  408. }
  409. else
  410. player->state = ENTITY_MOVING;
  411. }
  412. static void draw_tutorial(Canvas *canvas, GameManager *manager)
  413. {
  414. GameContext *game_context = game_manager_game_context_get(manager);
  415. canvas_set_font(canvas, FontPrimary);
  416. canvas_draw_str(canvas, 45, 12, "Tutorial");
  417. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  418. switch (game_context->tutorial_step)
  419. {
  420. case 0:
  421. canvas_draw_str(canvas, 15, 20, "Press LEFT to move left");
  422. break;
  423. case 1:
  424. canvas_draw_str(canvas, 15, 20, "Press RIGHT to move right");
  425. break;
  426. case 2:
  427. canvas_draw_str(canvas, 15, 20, "Press UP to move up");
  428. break;
  429. case 3:
  430. canvas_draw_str(canvas, 15, 20, "Press DOWN to move down");
  431. break;
  432. case 4:
  433. canvas_draw_str(canvas, 0, 20, "Press OK + collide with an enemy to attack");
  434. break;
  435. case 5:
  436. canvas_draw_str(canvas, 15, 20, "Hold OK to open the menu");
  437. break;
  438. case 6:
  439. canvas_draw_str(canvas, 15, 20, "Press BACK to escape the menu");
  440. break;
  441. case 7:
  442. canvas_draw_str(canvas, 15, 20, "Hold BACK to save and exit");
  443. break;
  444. case 8:
  445. // end of tutorial so quit
  446. game_context->tutorial_step = 0;
  447. game_context->is_menu_open = false;
  448. game_context->is_switching_level = true;
  449. game_manager_game_stop(manager);
  450. return;
  451. default:
  452. break;
  453. }
  454. }
  455. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  456. {
  457. if (!self || !context || !canvas || !manager)
  458. return;
  459. // Get game context
  460. GameContext *game_context = game_manager_game_context_get(manager);
  461. // Get player context
  462. PlayerContext *player = context;
  463. // Get player position
  464. Vector pos = entity_pos_get(self);
  465. // Calculate camera offset to center the player
  466. camera_x = pos.x - (SCREEN_WIDTH / 2);
  467. camera_y = pos.y - (SCREEN_HEIGHT / 2);
  468. // Clamp camera position to prevent showing areas outside the world
  469. camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
  470. camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
  471. // if player is moving right or left, draw the corresponding sprite
  472. if (player->direction == ENTITY_RIGHT || player->direction == ENTITY_LEFT)
  473. {
  474. canvas_draw_sprite(
  475. canvas,
  476. player->direction == ENTITY_RIGHT ? player->sprite_right : player->sprite_left,
  477. pos.x - camera_x - 5, // Center the sprite horizontally
  478. pos.y - camera_y - 5 // Center the sprite vertically
  479. );
  480. player->left = false;
  481. }
  482. else // otherwise
  483. {
  484. // Default to last sprite direction
  485. canvas_draw_sprite(
  486. canvas,
  487. player->left ? player->sprite_left : player->sprite_right,
  488. pos.x - camera_x - 5, // Center the sprite horizontally
  489. pos.y - camera_y - 5 // Center the sprite vertically
  490. );
  491. }
  492. // Draw the outer bounds adjusted by camera offset
  493. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  494. // render tutorial
  495. if (game_context->game_mode == GAME_MODE_STORY)
  496. {
  497. draw_tutorial(canvas, manager);
  498. if (game_context->is_menu_open)
  499. {
  500. background_render(canvas, manager);
  501. }
  502. }
  503. else
  504. {
  505. // render background
  506. background_render(canvas, manager);
  507. }
  508. }
  509. const EntityDescription player_desc = {
  510. .start = NULL, // called when entity is added to the level
  511. .stop = NULL, // called when entity is removed from the level
  512. .update = player_update, // called every frame
  513. .render = player_render, // called every frame, after update
  514. .collision = NULL, // called when entity collides with another entity
  515. .event = NULL, // called when entity receives an event
  516. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  517. };
  518. static SpriteContext *sprite_generic_alloc(const char *id, const char *type, uint8_t width, uint8_t height)
  519. {
  520. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  521. if (!ctx)
  522. {
  523. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  524. return NULL;
  525. }
  526. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  527. ctx->width = width;
  528. ctx->height = height;
  529. if (is_str(type, "player"))
  530. {
  531. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  532. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  533. }
  534. else if (is_str(type, "enemy"))
  535. {
  536. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  537. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  538. }
  539. else if (is_str(type, "npc"))
  540. {
  541. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "npc_right_%s_%dx%dpx.fxbm", id, width, height);
  542. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "npc_left_%s_%dx%dpx.fxbm", id, width, height);
  543. }
  544. return ctx;
  545. }
  546. SpriteContext *get_sprite_context(const char *name)
  547. {
  548. if (is_str(name, "axe"))
  549. return sprite_generic_alloc("axe", "player", 15, 11);
  550. else if (is_str(name, "bow"))
  551. return sprite_generic_alloc("bow", "player", 13, 11);
  552. else if (is_str(name, "naked"))
  553. return sprite_generic_alloc("naked", "player", 10, 10);
  554. else if (is_str(name, "sword"))
  555. return sprite_generic_alloc("sword", "player", 15, 11);
  556. //
  557. else if (is_str(name, "cyclops"))
  558. return sprite_generic_alloc("cyclops", "enemy", 10, 11);
  559. else if (is_str(name, "ghost"))
  560. return sprite_generic_alloc("ghost", "enemy", 15, 15);
  561. else if (is_str(name, "ogre"))
  562. return sprite_generic_alloc("ogre", "enemy", 10, 13);
  563. //
  564. else if (is_str(name, "funny"))
  565. return sprite_generic_alloc("funny", "npc", 15, 21);
  566. // If no match is found
  567. FURI_LOG_E("Game", "Sprite not found: %s", name);
  568. return NULL;
  569. }