player.c 23 KB

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