enemy.c 27 KB

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