player.c 21 KB

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