player.c 23 KB

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