player.c 24 KB

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