enemy.c 27 KB

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