enemy.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // enemy.c
  2. #include <game/enemy.h>
  3. #include <notification/notification_messages.h>
  4. static EnemyContext *enemy_context_generic;
  5. // Allocation function
  6. static EnemyContext *enemy_generic_alloc(
  7. const char *id,
  8. int index,
  9. Vector size,
  10. Vector start_position,
  11. Vector end_position,
  12. float move_timer, // Wait duration before moving again
  13. float speed,
  14. float attack_timer,
  15. float strength,
  16. float health)
  17. {
  18. if (!enemy_context_generic)
  19. {
  20. enemy_context_generic = malloc(sizeof(EnemyContext));
  21. }
  22. if (!enemy_context_generic)
  23. {
  24. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  25. return NULL;
  26. }
  27. snprintf(enemy_context_generic->id, sizeof(enemy_context_generic->id), "%s", id);
  28. enemy_context_generic->index = index;
  29. enemy_context_generic->size = size;
  30. enemy_context_generic->start_position = start_position;
  31. enemy_context_generic->end_position = end_position;
  32. enemy_context_generic->move_timer = move_timer; // Set wait duration
  33. enemy_context_generic->elapsed_move_timer = 0.0f; // Initialize elapsed timer
  34. enemy_context_generic->speed = speed;
  35. enemy_context_generic->attack_timer = attack_timer;
  36. enemy_context_generic->strength = strength;
  37. enemy_context_generic->health = health;
  38. // Initialize other fields as needed
  39. enemy_context_generic->sprite_right = NULL; // Assign appropriate sprite
  40. enemy_context_generic->sprite_left = NULL; // Assign appropriate sprite
  41. enemy_context_generic->direction = ENEMY_RIGHT; // Default direction
  42. enemy_context_generic->state = ENEMY_MOVING_TO_END; // Start in IDLE state
  43. // Set radius based on size, for example, average of size.x and size.y divided by 2
  44. enemy_context_generic->radius = (size.x + size.y) / 4.0f;
  45. return enemy_context_generic;
  46. }
  47. // Free function
  48. static void enemy_generic_free(void *context)
  49. {
  50. if (context)
  51. {
  52. free(context);
  53. context = NULL;
  54. }
  55. if (enemy_context_generic)
  56. {
  57. free(enemy_context_generic);
  58. enemy_context_generic = NULL;
  59. }
  60. }
  61. // Enemy start function
  62. static void enemy_start(Entity *self, GameManager *manager, void *context)
  63. {
  64. UNUSED(manager);
  65. if (!self || !context)
  66. {
  67. FURI_LOG_E("Game", "Enemy start: Invalid parameters");
  68. return;
  69. }
  70. if (!enemy_context_generic)
  71. {
  72. FURI_LOG_E("Game", "Enemy start: Enemy context not set");
  73. return;
  74. }
  75. EnemyContext *enemy_context = (EnemyContext *)context;
  76. // Copy fields from generic context
  77. snprintf(enemy_context->id, sizeof(enemy_context->id), "%s", enemy_context_generic->id);
  78. enemy_context->index = enemy_context_generic->index;
  79. enemy_context->size = enemy_context_generic->size;
  80. enemy_context->start_position = enemy_context_generic->start_position;
  81. enemy_context->end_position = enemy_context_generic->end_position;
  82. enemy_context->move_timer = enemy_context_generic->move_timer;
  83. enemy_context->elapsed_move_timer = enemy_context_generic->elapsed_move_timer;
  84. enemy_context->speed = enemy_context_generic->speed;
  85. enemy_context->attack_timer = enemy_context_generic->attack_timer;
  86. enemy_context->strength = enemy_context_generic->strength;
  87. enemy_context->health = enemy_context_generic->health;
  88. enemy_context->sprite_right = enemy_context_generic->sprite_right;
  89. enemy_context->sprite_left = enemy_context_generic->sprite_left;
  90. enemy_context->direction = enemy_context_generic->direction;
  91. enemy_context->state = enemy_context_generic->state;
  92. enemy_context->radius = enemy_context_generic->radius;
  93. // Set enemy's initial position based on start_position
  94. entity_pos_set(self, enemy_context->start_position);
  95. // Add collision circle based on the enemy's radius
  96. entity_collider_add_circle(self, enemy_context->radius);
  97. }
  98. // Enemy render function
  99. static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  100. {
  101. if (!self || !context || !canvas || !manager)
  102. return;
  103. EnemyContext *enemy_context = (EnemyContext *)context;
  104. // Get the position of the enemy
  105. Vector pos = entity_pos_get(self);
  106. // Choose sprite based on direction
  107. Sprite *current_sprite = NULL;
  108. if (enemy_context->direction == ENEMY_LEFT)
  109. {
  110. current_sprite = enemy_context->sprite_left;
  111. }
  112. else
  113. {
  114. current_sprite = enemy_context->sprite_right;
  115. }
  116. // Draw enemy sprite relative to camera, centered on the enemy's position
  117. canvas_draw_sprite(
  118. canvas,
  119. current_sprite,
  120. pos.x - camera_x - (enemy_context->size.x / 2),
  121. pos.y - camera_y - (enemy_context->size.y / 2));
  122. // draw health of enemy
  123. char health_str[32];
  124. snprintf(health_str, sizeof(health_str), "%.0f", (double)enemy_context->health);
  125. draw_username(canvas, pos, health_str);
  126. // Draw user stats (this has to be done for all enemies)
  127. draw_user_stats(canvas, (Vector){0, 50}, manager);
  128. }
  129. static void atk_notify(GameContext *game_context, EnemyContext *enemy_context, bool player_attacked)
  130. {
  131. if (!game_context || !enemy_context)
  132. {
  133. FURI_LOG_E("Game", "Send attack notification: Invalid parameters");
  134. return;
  135. }
  136. NotificationApp *notifications = furi_record_open(RECORD_NOTIFICATION);
  137. const bool vibration_allowed = strstr(yes_or_no_choices[vibration_on_index], "Yes") != NULL;
  138. const bool sound_allowed = strstr(yes_or_no_choices[sound_on_index], "Yes") != NULL;
  139. if (player_attacked)
  140. {
  141. if (vibration_allowed && sound_allowed)
  142. {
  143. notification_message(notifications, &sequence_success);
  144. }
  145. else if (vibration_allowed && !sound_allowed)
  146. {
  147. notification_message(notifications, &sequence_single_vibro);
  148. }
  149. else if (!vibration_allowed && sound_allowed)
  150. {
  151. // change this to sound later
  152. notification_message(notifications, &sequence_blink_blue_100);
  153. }
  154. else
  155. {
  156. notification_message(notifications, &sequence_blink_blue_100);
  157. }
  158. FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
  159. }
  160. else
  161. {
  162. if (vibration_allowed && sound_allowed)
  163. {
  164. notification_message(notifications, &sequence_error);
  165. }
  166. else if (vibration_allowed && !sound_allowed)
  167. {
  168. notification_message(notifications, &sequence_single_vibro);
  169. }
  170. else if (!vibration_allowed && sound_allowed)
  171. {
  172. // change this to sound later
  173. notification_message(notifications, &sequence_blink_red_100);
  174. }
  175. else
  176. {
  177. notification_message(notifications, &sequence_blink_red_100);
  178. }
  179. FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
  180. }
  181. // close the notifications
  182. furi_record_close(RECORD_NOTIFICATION);
  183. }
  184. // Enemy collision function
  185. static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  186. {
  187. if (!self || !other || !context || !manager)
  188. {
  189. FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
  190. return;
  191. }
  192. // Check if the enemy collided with the player
  193. if (entity_description_get(other) == &player_desc)
  194. {
  195. // Retrieve enemy context
  196. EnemyContext *enemy_context = (EnemyContext *)context;
  197. GameContext *game_context = game_manager_game_context_get(manager);
  198. // InputState input = game_manager_input_get(manager);
  199. if (!enemy_context)
  200. {
  201. FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
  202. return;
  203. }
  204. if (!game_context)
  205. {
  206. FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
  207. return;
  208. }
  209. // Get positions of the enemy and the player
  210. Vector enemy_pos = entity_pos_get(self);
  211. Vector player_pos = entity_pos_get(other);
  212. // Determine if the enemy is facing the player or player is facing the enemy
  213. bool enemy_is_facing_player = false;
  214. bool player_is_facing_enemy = false;
  215. // Determine if the enemy is facing the player
  216. if ((enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x) ||
  217. (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x) ||
  218. (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y) ||
  219. (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y))
  220. {
  221. enemy_is_facing_player = true;
  222. }
  223. // Determine if the player is facing the enemy
  224. if ((game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x) ||
  225. (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x) ||
  226. (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y) ||
  227. (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y))
  228. {
  229. player_is_facing_enemy = true;
  230. }
  231. // Handle Player Attacking Enemy (Press OK, facing enemy, and enemy not facing player)
  232. if (player_is_facing_enemy && game_context->last_button == GameKeyOk && !enemy_is_facing_player)
  233. {
  234. // Reset last button
  235. game_context->last_button = -1;
  236. if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
  237. {
  238. atk_notify(game_context, enemy_context, true);
  239. // Reset player's elapsed attack timer
  240. game_context->player_context->elapsed_attack_timer = 0.0f;
  241. enemy_context->elapsed_attack_timer = 0.0f; // Reset enemy's attack timer to block enemy attack
  242. // Increase XP by the enemy's strength
  243. game_context->player_context->xp += enemy_context->strength;
  244. // Increase healthy by 10% of the enemy's strength
  245. game_context->player_context->health += enemy_context->strength * 0.1f;
  246. if (game_context->player_context->health > game_context->player_context->max_health)
  247. {
  248. game_context->player_context->health = game_context->player_context->max_health;
  249. }
  250. // Decrease enemy health by player strength
  251. enemy_context->health -= game_context->player_context->strength;
  252. if (enemy_context->health <= 0)
  253. {
  254. FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
  255. enemy_context->state = ENEMY_DEAD;
  256. // Reset enemy position and health
  257. enemy_context->health = 100; // this needs to be set to the enemy's max health
  258. // remove from game context and set in safe zone
  259. game_context->enemies[enemy_context->index] = NULL;
  260. game_context->enemy_count--;
  261. entity_collider_remove(self);
  262. entity_pos_set(self, (Vector){-100, -100});
  263. return;
  264. }
  265. else
  266. {
  267. FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
  268. enemy_context->state = ENEMY_ATTACKED;
  269. // Bounce the enemy back by X units opposite their last movement direction
  270. enemy_pos.x -= game_context->player_context->dx * enemy_context->radius + game_context->icon_offset;
  271. enemy_pos.y -= game_context->player_context->dy * enemy_context->radius + game_context->icon_offset;
  272. entity_pos_set(self, enemy_pos);
  273. // Reset enemy's movement direction to prevent immediate re-collision
  274. game_context->player_context->dx = 0;
  275. game_context->player_context->dy = 0;
  276. }
  277. }
  278. else
  279. {
  280. FURI_LOG_I("Game", "Player attack on enemy '%s' is on cooldown: %f seconds remaining", enemy_context->id, (double)(game_context->player_context->attack_timer - game_context->player_context->elapsed_attack_timer));
  281. }
  282. }
  283. // Handle Enemy Attacking Player (enemy facing player)
  284. else if (enemy_is_facing_player)
  285. {
  286. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  287. {
  288. atk_notify(game_context, enemy_context, false);
  289. // Reset enemy's elapsed attack timer
  290. enemy_context->elapsed_attack_timer = 0.0f;
  291. // Decrease player health by enemy strength
  292. game_context->player_context->health -= enemy_context->strength;
  293. if (game_context->player_context->health <= 0)
  294. {
  295. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  296. game_context->player_context->state = PLAYER_DEAD;
  297. // Reset player position and health
  298. entity_pos_set(other, game_context->player_context->start_position);
  299. game_context->player_context->health = game_context->player_context->max_health;
  300. // subtract player's XP by the enemy's strength
  301. game_context->player_context->xp -= enemy_context->strength;
  302. if ((int)game_context->player_context->xp < 0)
  303. {
  304. game_context->player_context->xp = 0;
  305. }
  306. }
  307. else
  308. {
  309. FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
  310. game_context->player_context->state = PLAYER_ATTACKED;
  311. // Bounce the player back by X units opposite their last movement direction
  312. player_pos.x -= game_context->player_context->dx * enemy_context->radius + game_context->icon_offset;
  313. player_pos.y -= game_context->player_context->dy * enemy_context->radius + game_context->icon_offset;
  314. entity_pos_set(other, player_pos);
  315. // Reset player's movement direction to prevent immediate re-collision
  316. game_context->player_context->dx = 0;
  317. game_context->player_context->dy = 0;
  318. }
  319. }
  320. }
  321. else // handle other collisions
  322. {
  323. // Bounce player in the direction they came
  324. Vector player_pos = entity_pos_get(other);
  325. switch (game_context->player_context->direction)
  326. {
  327. case PLAYER_UP:
  328. player_pos.y += enemy_context->size.y;
  329. break;
  330. case PLAYER_DOWN:
  331. player_pos.y -= enemy_context->size.y;
  332. break;
  333. case PLAYER_LEFT:
  334. player_pos.x += enemy_context->size.x;
  335. break;
  336. case PLAYER_RIGHT:
  337. player_pos.x -= enemy_context->size.x;
  338. break;
  339. };
  340. entity_pos_set(other, player_pos);
  341. // Reset player's movement direction to prevent immediate re-collision
  342. game_context->player_context->dx = 0;
  343. game_context->player_context->dy = 0;
  344. }
  345. // Reset enemy's state
  346. enemy_context->state = ENEMY_IDLE;
  347. enemy_context->elapsed_move_timer = 0.0f;
  348. if (game_context->player_context->state == PLAYER_DEAD)
  349. {
  350. // Reset player's position and health
  351. entity_pos_set(other, game_context->player_context->start_position);
  352. game_context->player_context->health = 100;
  353. }
  354. }
  355. }
  356. // Enemy update function
  357. static void enemy_update(Entity *self, GameManager *manager, void *context)
  358. {
  359. if (!self || !context || !manager)
  360. return;
  361. EnemyContext *enemy_context = (EnemyContext *)context;
  362. if (!enemy_context || enemy_context->state == ENEMY_DEAD)
  363. {
  364. return;
  365. }
  366. GameContext *game_context = game_manager_game_context_get(manager);
  367. if (!game_context)
  368. {
  369. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  370. return;
  371. }
  372. float delta_time = 1.0f / game_context->fps;
  373. // Increment the elapsed_attack_timer for the enemy
  374. enemy_context->elapsed_attack_timer += delta_time;
  375. switch (enemy_context->state)
  376. {
  377. case ENEMY_IDLE:
  378. // Increment the elapsed_move_timer
  379. enemy_context->elapsed_move_timer += delta_time;
  380. // Check if it's time to move again
  381. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  382. {
  383. // Determine the next state based on the current position
  384. Vector current_pos = entity_pos_get(self);
  385. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
  386. fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
  387. {
  388. enemy_context->state = ENEMY_MOVING_TO_END;
  389. }
  390. else
  391. {
  392. enemy_context->state = ENEMY_MOVING_TO_START;
  393. }
  394. enemy_context->elapsed_move_timer = 0.0f;
  395. }
  396. break;
  397. case ENEMY_MOVING_TO_END:
  398. case ENEMY_MOVING_TO_START:
  399. {
  400. // Determine the target position based on the current state
  401. Vector target_position = (enemy_context->state == ENEMY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  402. // Get current position
  403. Vector current_pos = entity_pos_get(self);
  404. Vector direction_vector = {0, 0};
  405. // Calculate direction towards the target
  406. if (current_pos.x < target_position.x)
  407. {
  408. direction_vector.x = 1.0f;
  409. enemy_context->direction = ENEMY_RIGHT;
  410. }
  411. else if (current_pos.x > target_position.x)
  412. {
  413. direction_vector.x = -1.0f;
  414. enemy_context->direction = ENEMY_LEFT;
  415. }
  416. if (current_pos.y < target_position.y)
  417. {
  418. direction_vector.y = 1.0f;
  419. enemy_context->direction = ENEMY_DOWN;
  420. }
  421. else if (current_pos.y > target_position.y)
  422. {
  423. direction_vector.y = -1.0f;
  424. enemy_context->direction = ENEMY_UP;
  425. }
  426. // Normalize direction vector
  427. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  428. if (length != 0)
  429. {
  430. direction_vector.x /= length;
  431. direction_vector.y /= length;
  432. }
  433. // Update position based on direction and speed
  434. Vector new_pos = current_pos;
  435. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  436. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  437. // Clamp the position to the target to prevent overshooting
  438. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  439. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  440. {
  441. new_pos.x = target_position.x;
  442. }
  443. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  444. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  445. {
  446. new_pos.y = target_position.y;
  447. }
  448. entity_pos_set(self, new_pos);
  449. // Check if the enemy has reached or surpassed the target_position
  450. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  451. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  452. // If reached the target position on both axes, transition to IDLE
  453. if (reached_x && reached_y)
  454. {
  455. enemy_context->state = ENEMY_IDLE;
  456. enemy_context->elapsed_move_timer = 0.0f;
  457. }
  458. }
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. // Free function for the entity
  465. static void enemy_free(Entity *self, GameManager *manager, void *context)
  466. {
  467. UNUSED(self);
  468. UNUSED(manager);
  469. if (context)
  470. enemy_generic_free(context);
  471. }
  472. // Enemy behavior structure
  473. static const EntityDescription _generic_enemy = {
  474. .start = enemy_start,
  475. .stop = enemy_free,
  476. .update = enemy_update,
  477. .render = enemy_render,
  478. .collision = enemy_collision,
  479. .event = NULL,
  480. .context_size = sizeof(EnemyContext),
  481. };
  482. // Enemy function to return the entity description
  483. const EntityDescription *enemy(
  484. GameManager *manager,
  485. const char *id,
  486. int index,
  487. Vector start_position,
  488. Vector end_position,
  489. float move_timer, // Wait duration before moving again
  490. float speed,
  491. float attack_timer,
  492. float strength,
  493. float health)
  494. {
  495. SpriteContext *sprite_context = get_sprite_context(id);
  496. if (!sprite_context)
  497. {
  498. FURI_LOG_E("Game", "Failed to get SpriteContext");
  499. return NULL;
  500. }
  501. // Allocate a new EnemyContext with provided parameters
  502. enemy_context_generic = enemy_generic_alloc(
  503. id,
  504. index,
  505. (Vector){sprite_context->width, sprite_context->height},
  506. start_position,
  507. end_position,
  508. move_timer, // Set wait duration
  509. speed,
  510. attack_timer,
  511. strength,
  512. health);
  513. if (!enemy_context_generic)
  514. {
  515. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  516. return NULL;
  517. }
  518. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  519. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  520. // Set initial direction based on start and end positions
  521. if (start_position.x < end_position.x)
  522. {
  523. enemy_context_generic->direction = ENEMY_RIGHT;
  524. }
  525. else
  526. {
  527. enemy_context_generic->direction = ENEMY_LEFT;
  528. }
  529. // Set initial state based on movement
  530. if (start_position.x != end_position.x || start_position.y != end_position.y)
  531. {
  532. enemy_context_generic->state = ENEMY_MOVING_TO_END;
  533. }
  534. else
  535. {
  536. enemy_context_generic->state = ENEMY_IDLE;
  537. }
  538. return &_generic_enemy;
  539. }
  540. void spawn_enemy_json_furi(Level *level, GameManager *manager, FuriString *json)
  541. {
  542. if (!level || !manager || !json)
  543. {
  544. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  545. return;
  546. }
  547. FuriString *id = get_json_value_furi("id", json);
  548. FuriString *_index = get_json_value_furi("index", json);
  549. //
  550. FuriString *start_position = get_json_value_furi("start_position", json);
  551. FuriString *start_position_x = get_json_value_furi("x", start_position);
  552. FuriString *start_position_y = get_json_value_furi("y", start_position);
  553. //
  554. FuriString *end_position = get_json_value_furi("end_position", json);
  555. FuriString *end_position_x = get_json_value_furi("x", end_position);
  556. FuriString *end_position_y = get_json_value_furi("y", end_position);
  557. //
  558. FuriString *move_timer = get_json_value_furi("move_timer", json);
  559. FuriString *speed = get_json_value_furi("speed", json);
  560. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  561. FuriString *strength = get_json_value_furi("strength", json);
  562. FuriString *health = get_json_value_furi("health", json);
  563. //
  564. if (!id || !_index || !start_position || !start_position_x || !start_position_y || !end_position || !end_position_x || !end_position_y || !move_timer || !speed || !attack_timer || !strength || !health)
  565. {
  566. FURI_LOG_E("Game", "Failed to parse JSON values");
  567. return;
  568. }
  569. GameContext *game_context = game_manager_game_context_get(manager);
  570. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  571. {
  572. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  573. manager,
  574. furi_string_get_cstr(id),
  575. atoi(furi_string_get_cstr(_index)),
  576. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  577. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  578. atof_furi(move_timer),
  579. atof_furi(speed),
  580. atof_furi(attack_timer),
  581. atof_furi(strength),
  582. atof_furi(health)));
  583. game_context->enemy_count++;
  584. }
  585. furi_string_free(id);
  586. furi_string_free(_index);
  587. furi_string_free(start_position);
  588. furi_string_free(start_position_x);
  589. furi_string_free(start_position_y);
  590. furi_string_free(end_position);
  591. furi_string_free(end_position_x);
  592. furi_string_free(end_position_y);
  593. furi_string_free(move_timer);
  594. furi_string_free(speed);
  595. furi_string_free(attack_timer);
  596. furi_string_free(strength);
  597. furi_string_free(health);
  598. }