player.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. uint16_t elapsed_ws_timer = 0;
  194. static void player_update(Entity *self, GameManager *manager, void *context)
  195. {
  196. if (!self || !manager || !context)
  197. return;
  198. PlayerContext *player = (PlayerContext *)context;
  199. InputState input = game_manager_input_get(manager);
  200. Vector pos = entity_pos_get(self);
  201. GameContext *game_context = game_manager_game_context_get(manager);
  202. // update websocket player context
  203. if (game_context->game_mode == GAME_MODE_PVP && (player->old_position.x != pos.x || player->old_position.y != pos.y))
  204. {
  205. elapsed_ws_timer++;
  206. // only send the websocket update every 100ms
  207. if (elapsed_ws_timer >= (game_context->fps / 10))
  208. {
  209. if (game_context->fhttp)
  210. {
  211. player->start_position = player->old_position;
  212. websocket_player_context(player, game_context->fhttp);
  213. }
  214. elapsed_ws_timer = 0;
  215. }
  216. }
  217. player->old_position = pos;
  218. // Determine the player's level based on XP
  219. player->level = get_player_level_iterative(player->xp);
  220. player->strength = 10 + (player->level * 1); // 1 strength per level
  221. player->max_health = 100 + ((player->level - 1) * 10); // 10 health per level
  222. // Store previous direction
  223. int prev_dx = player->dx;
  224. int prev_dy = player->dy;
  225. // Reset movement deltas each frame
  226. player->dx = 0;
  227. player->dy = 0;
  228. if (game_context->imu_present)
  229. {
  230. // update position using the IMU
  231. vgm_direction(game_context->imu, player, &pos);
  232. }
  233. // Apply health regeneration
  234. player->elapsed_health_regen += 1.0f / game_context->fps;
  235. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  236. {
  237. player->health += (player->health_regen + player->health > player->max_health)
  238. ? (player->max_health - player->health)
  239. : player->health_regen;
  240. player->elapsed_health_regen = 0;
  241. }
  242. // Increment the elapsed_attack_timer for the player
  243. player->elapsed_attack_timer += 1.0f / game_context->fps;
  244. // Handle movement input
  245. if (input.held & GameKeyUp)
  246. {
  247. if (game_context->last_button == GameKeyUp)
  248. game_context->elapsed_button_timer += 1;
  249. else
  250. game_context->elapsed_button_timer = 0;
  251. if (!game_context->is_menu_open)
  252. {
  253. pos.y -= (1 + game_context->icon_offset);
  254. player->dy = -1;
  255. player->direction = ENTITY_UP;
  256. }
  257. else
  258. {
  259. // next menu view
  260. // we can only go up to info from settings
  261. game_context->menu_screen = GAME_MENU_INFO;
  262. }
  263. game_context->last_button = GameKeyUp;
  264. }
  265. if (input.held & GameKeyDown)
  266. {
  267. if (game_context->last_button == GameKeyDown)
  268. game_context->elapsed_button_timer += 1;
  269. else
  270. game_context->elapsed_button_timer = 0;
  271. if (!game_context->is_menu_open)
  272. {
  273. pos.y += (1 + game_context->icon_offset);
  274. player->dy = 1;
  275. player->direction = ENTITY_DOWN;
  276. }
  277. else
  278. {
  279. // next menu view
  280. // we can only go down to more from info
  281. game_context->menu_screen = GAME_MENU_MORE;
  282. }
  283. game_context->last_button = GameKeyDown;
  284. }
  285. if (input.held & GameKeyLeft)
  286. {
  287. if (game_context->last_button == GameKeyLeft)
  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 -= (1 + game_context->icon_offset);
  294. player->dx = -1;
  295. player->direction = ENTITY_LEFT;
  296. }
  297. else
  298. {
  299. // if the menu is open, move the selection left
  300. if (game_context->menu_selection < 1)
  301. {
  302. game_context->menu_selection += 1;
  303. }
  304. }
  305. game_context->last_button = GameKeyLeft;
  306. }
  307. if (input.held & GameKeyRight)
  308. {
  309. if (game_context->last_button == GameKeyRight)
  310. game_context->elapsed_button_timer += 1;
  311. else
  312. game_context->elapsed_button_timer = 0;
  313. if (!game_context->is_menu_open)
  314. {
  315. pos.x += (1 + game_context->icon_offset);
  316. player->dx = 1;
  317. player->direction = ENTITY_RIGHT;
  318. }
  319. else
  320. {
  321. // if the menu is open, move the selection right
  322. if (game_context->menu_selection < 1)
  323. {
  324. game_context->menu_selection += 1;
  325. }
  326. }
  327. game_context->last_button = GameKeyRight;
  328. }
  329. if (input.held & GameKeyOk)
  330. {
  331. if (game_context->last_button == GameKeyOk)
  332. game_context->elapsed_button_timer += 1;
  333. else
  334. game_context->elapsed_button_timer = 0;
  335. game_context->last_button = GameKeyOk;
  336. // if all enemies are dead, allow the "OK" button to switch levels
  337. // otherwise the "OK" button will be used to attack
  338. if (game_context->game_mode != GAME_MODE_PVP && game_context->enemy_count == 0 && !game_context->is_switching_level)
  339. {
  340. game_context->is_switching_level = true;
  341. save_player_context(player);
  342. furi_delay_ms(100);
  343. game_manager_next_level_set(manager, next_level(manager));
  344. return;
  345. }
  346. // if the OK button is held for 1 seconds,show the menu
  347. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  348. {
  349. // open up menu on the INFO screen
  350. game_context->menu_screen = GAME_MENU_INFO;
  351. game_context->menu_selection = 0;
  352. game_context->is_menu_open = true;
  353. }
  354. }
  355. if (input.held & GameKeyBack)
  356. {
  357. if (game_context->last_button == GameKeyBack)
  358. game_context->elapsed_button_timer += 1;
  359. else
  360. game_context->elapsed_button_timer = 0;
  361. game_context->last_button = GameKeyBack;
  362. if (game_context->is_menu_open)
  363. {
  364. game_context->is_menu_open = false;
  365. }
  366. // if the back button is held for 1 seconds, stop the game
  367. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  368. {
  369. if (!game_context->is_menu_open)
  370. {
  371. save_player_context(player);
  372. furi_delay_ms(100);
  373. game_manager_game_stop(manager);
  374. return;
  375. }
  376. }
  377. }
  378. // adjust tutorial step
  379. if (game_context->game_mode == GAME_MODE_STORY)
  380. {
  381. switch (game_context->tutorial_step)
  382. {
  383. case 0:
  384. if (input.held & GameKeyLeft)
  385. game_context->tutorial_step++;
  386. break;
  387. case 1:
  388. if (input.held & GameKeyRight)
  389. game_context->tutorial_step++;
  390. break;
  391. case 2:
  392. if (input.held & GameKeyUp)
  393. game_context->tutorial_step++;
  394. break;
  395. case 3:
  396. if (input.held & GameKeyDown)
  397. game_context->tutorial_step++;
  398. break;
  399. case 5:
  400. if (input.held & GameKeyOk && game_context->is_menu_open)
  401. game_context->tutorial_step++;
  402. break;
  403. case 6:
  404. if (input.held & GameKeyBack)
  405. game_context->tutorial_step++;
  406. break;
  407. case 7:
  408. if (input.held & GameKeyBack)
  409. game_context->tutorial_step++;
  410. break;
  411. }
  412. }
  413. // Clamp the player's position to stay within world bounds
  414. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  415. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  416. // Update player position
  417. entity_pos_set(self, pos);
  418. // If the player is not moving, retain the last movement direction
  419. if (player->dx == 0 && player->dy == 0)
  420. {
  421. player->dx = prev_dx;
  422. player->dy = prev_dy;
  423. player->state = ENTITY_IDLE;
  424. }
  425. else
  426. player->state = ENTITY_MOVING;
  427. }
  428. static void draw_tutorial(Canvas *canvas, GameManager *manager)
  429. {
  430. GameContext *game_context = game_manager_game_context_get(manager);
  431. canvas_set_font(canvas, FontPrimary);
  432. canvas_draw_str(canvas, 45, 12, "Tutorial");
  433. canvas_set_font_custom(canvas, FONT_SIZE_SMALL);
  434. switch (game_context->tutorial_step)
  435. {
  436. case 0:
  437. canvas_draw_str(canvas, 15, 20, "Press LEFT to move left");
  438. break;
  439. case 1:
  440. canvas_draw_str(canvas, 15, 20, "Press RIGHT to move right");
  441. break;
  442. case 2:
  443. canvas_draw_str(canvas, 15, 20, "Press UP to move up");
  444. break;
  445. case 3:
  446. canvas_draw_str(canvas, 15, 20, "Press DOWN to move down");
  447. break;
  448. case 4:
  449. canvas_draw_str(canvas, 0, 20, "Press OK + collide with an enemy to attack");
  450. break;
  451. case 5:
  452. canvas_draw_str(canvas, 15, 20, "Hold OK to open the menu");
  453. break;
  454. case 6:
  455. canvas_draw_str(canvas, 15, 20, "Press BACK to escape the menu");
  456. break;
  457. case 7:
  458. canvas_draw_str(canvas, 15, 20, "Hold BACK to save and exit");
  459. break;
  460. case 8:
  461. // end of tutorial so quit
  462. game_context->tutorial_step = 0;
  463. game_context->is_menu_open = false;
  464. game_context->is_switching_level = true;
  465. game_manager_game_stop(manager);
  466. return;
  467. default:
  468. break;
  469. }
  470. }
  471. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  472. {
  473. if (!self || !context || !canvas || !manager)
  474. return;
  475. // Get game context
  476. GameContext *game_context = game_manager_game_context_get(manager);
  477. // Get player context
  478. PlayerContext *player = context;
  479. // Get player position
  480. Vector pos = entity_pos_get(self);
  481. // Calculate camera offset to center the player
  482. camera_x = pos.x - (SCREEN_WIDTH / 2);
  483. camera_y = pos.y - (SCREEN_HEIGHT / 2);
  484. // Clamp camera position to prevent showing areas outside the world
  485. camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
  486. camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
  487. // if player is moving right or left, draw the corresponding sprite
  488. if (player->direction == ENTITY_RIGHT || player->direction == ENTITY_LEFT)
  489. {
  490. canvas_draw_sprite(
  491. canvas,
  492. player->direction == ENTITY_RIGHT ? player->sprite_right : player->sprite_left,
  493. pos.x - camera_x - 5, // Center the sprite horizontally
  494. pos.y - camera_y - 5 // Center the sprite vertically
  495. );
  496. player->left = false;
  497. }
  498. else // otherwise
  499. {
  500. // Default to last sprite direction
  501. canvas_draw_sprite(
  502. canvas,
  503. player->left ? player->sprite_left : player->sprite_right,
  504. pos.x - camera_x - 5, // Center the sprite horizontally
  505. pos.y - camera_y - 5 // Center the sprite vertically
  506. );
  507. }
  508. // Draw the outer bounds adjusted by camera offset
  509. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  510. // render tutorial
  511. if (game_context->game_mode == GAME_MODE_STORY)
  512. {
  513. draw_tutorial(canvas, manager);
  514. if (game_context->is_menu_open)
  515. {
  516. background_render(canvas, manager);
  517. }
  518. }
  519. else
  520. {
  521. // render background
  522. background_render(canvas, manager);
  523. }
  524. }
  525. const EntityDescription player_desc = {
  526. .start = NULL, // called when entity is added to the level
  527. .stop = NULL, // called when entity is removed from the level
  528. .update = player_update, // called every frame
  529. .render = player_render, // called every frame, after update
  530. .collision = NULL, // called when entity collides with another entity
  531. .event = NULL, // called when entity receives an event
  532. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  533. };
  534. static SpriteContext *sprite_generic_alloc(const char *id, const char *type, uint8_t width, uint8_t height)
  535. {
  536. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  537. if (!ctx)
  538. {
  539. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  540. return NULL;
  541. }
  542. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  543. ctx->width = width;
  544. ctx->height = height;
  545. if (is_str(type, "player"))
  546. {
  547. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  548. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  549. }
  550. else if (is_str(type, "enemy"))
  551. {
  552. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  553. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  554. }
  555. else if (is_str(type, "npc"))
  556. {
  557. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "npc_right_%s_%dx%dpx.fxbm", id, width, height);
  558. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "npc_left_%s_%dx%dpx.fxbm", id, width, height);
  559. }
  560. return ctx;
  561. }
  562. SpriteContext *get_sprite_context(const char *name)
  563. {
  564. if (is_str(name, "axe"))
  565. return sprite_generic_alloc("axe", "player", 15, 11);
  566. else if (is_str(name, "bow"))
  567. return sprite_generic_alloc("bow", "player", 13, 11);
  568. else if (is_str(name, "naked"))
  569. return sprite_generic_alloc("naked", "player", 10, 10);
  570. else if (is_str(name, "sword"))
  571. return sprite_generic_alloc("sword", "player", 15, 11);
  572. //
  573. else if (is_str(name, "cyclops"))
  574. return sprite_generic_alloc("cyclops", "enemy", 10, 11);
  575. else if (is_str(name, "ghost"))
  576. return sprite_generic_alloc("ghost", "enemy", 15, 15);
  577. else if (is_str(name, "ogre"))
  578. return sprite_generic_alloc("ogre", "enemy", 10, 13);
  579. //
  580. else if (is_str(name, "funny"))
  581. return sprite_generic_alloc("funny", "npc", 15, 21);
  582. // If no match is found
  583. FURI_LOG_E("Game", "Sprite not found: %s", name);
  584. return NULL;
  585. }