enemy.c 26 KB

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