enemy.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. // enemy.c
  2. #include <game/enemy.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #define EPSILON 0.1f
  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. bool vibration_allowed = strstr(yes_or_no_choices[game_vibration_on_index], "Yes") != NULL;
  147. 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(game_context->notifications, &sequence_success);
  153. }
  154. else if (vibration_allowed && !sound_allowed)
  155. {
  156. notification_message(game_context->notifications, &sequence_single_vibro);
  157. }
  158. else if (!vibration_allowed && sound_allowed)
  159. {
  160. // change this to sound later
  161. notification_message(game_context->notifications, &sequence_blink_blue_100);
  162. }
  163. else
  164. {
  165. notification_message(game_context->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(game_context->notifications, &sequence_error);
  174. }
  175. else if (vibration_allowed && !sound_allowed)
  176. {
  177. notification_message(game_context->notifications, &sequence_single_vibro);
  178. }
  179. else if (!vibration_allowed && sound_allowed)
  180. {
  181. // change this to sound later
  182. notification_message(game_context->notifications, &sequence_blink_red_100);
  183. }
  184. else
  185. {
  186. notification_message(game_context->notifications, &sequence_blink_red_100);
  187. }
  188. FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
  189. }
  190. }
  191. // Enemy collision function
  192. static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  193. {
  194. if (!self || !other || !context)
  195. {
  196. FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
  197. return;
  198. }
  199. // Check if the enemy collided with the player
  200. if (entity_description_get(other) == &player_desc)
  201. {
  202. // Retrieve enemy context
  203. EnemyContext *enemy_context = (EnemyContext *)context;
  204. GameContext *game_context = game_manager_game_context_get(manager);
  205. if (!enemy_context)
  206. {
  207. FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
  208. return;
  209. }
  210. if (!game_context)
  211. {
  212. FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
  213. return;
  214. }
  215. // Get positions of the enemy and the player
  216. Vector enemy_pos = entity_pos_get(self);
  217. Vector player_pos = entity_pos_get(other);
  218. // Determine if the enemy is facing the player or player is facing the enemy
  219. bool enemy_is_facing_player = false;
  220. bool player_is_facing_enemy = false;
  221. // Determine if the enemy is facing the player
  222. if ((enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x) ||
  223. (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x) ||
  224. (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y) ||
  225. (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y))
  226. {
  227. enemy_is_facing_player = true;
  228. }
  229. // Determine if the player is facing the enemy
  230. if ((game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x) ||
  231. (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x) ||
  232. (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y) ||
  233. (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y))
  234. {
  235. player_is_facing_enemy = true;
  236. }
  237. // Handle Player Attacking Enemy (Press OK, facing enemy, and enemy not facing player)
  238. if (player_is_facing_enemy && game_context->user_input == GameKeyOk && !enemy_is_facing_player)
  239. {
  240. if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
  241. {
  242. send_attack_notification(game_context, enemy_context, true);
  243. // Reset player's elapsed attack timer
  244. game_context->player_context->elapsed_attack_timer = 0.0f;
  245. enemy_context->elapsed_attack_timer = 0.0f; // Reset enemy's attack timer to block enemy attack
  246. // Increase XP by the enemy's strength
  247. game_context->player_context->xp += enemy_context->strength;
  248. // Increase healthy by 10% of the enemy's strength
  249. game_context->player_context->health += enemy_context->strength * 0.1f;
  250. if (game_context->player_context->health > game_context->player_context->max_health)
  251. {
  252. game_context->player_context->health = game_context->player_context->max_health;
  253. }
  254. // Decrease enemy health by player strength
  255. enemy_context->health -= game_context->player_context->strength;
  256. if (enemy_context->health <= 0)
  257. {
  258. FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
  259. enemy_context->state = ENEMY_DEAD;
  260. // Reset enemy position and health
  261. enemy_context->health = 100; // this needs to be set to the enemy's max health
  262. // remove from game context and set in safe zone
  263. game_context->enemies[enemy_context->index] = NULL;
  264. game_context->enemy_count--;
  265. entity_collider_remove(self);
  266. entity_pos_set(self, (Vector){-100, -100});
  267. return;
  268. }
  269. else
  270. {
  271. FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
  272. enemy_context->state = ENEMY_ATTACKED;
  273. // Bounce the enemy back by X units opposite their last movement direction
  274. enemy_pos.x -= game_context->player_context->dx * enemy_context->radius;
  275. enemy_pos.y -= game_context->player_context->dy * enemy_context->radius;
  276. entity_pos_set(self, enemy_pos);
  277. // Reset enemy's movement direction to prevent immediate re-collision
  278. game_context->player_context->dx = 0;
  279. game_context->player_context->dy = 0;
  280. }
  281. }
  282. else
  283. {
  284. 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));
  285. }
  286. }
  287. // Handle Enemy Attacking Player (enemy facing player)
  288. else if (enemy_is_facing_player)
  289. {
  290. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  291. {
  292. send_attack_notification(game_context, enemy_context, false);
  293. // Reset enemy's elapsed attack timer
  294. enemy_context->elapsed_attack_timer = 0.0f;
  295. // Decrease player health by enemy strength
  296. game_context->player_context->health -= enemy_context->strength;
  297. if (game_context->player_context->health <= 0)
  298. {
  299. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  300. game_context->player_context->state = PLAYER_DEAD;
  301. // Reset player position and health
  302. entity_pos_set(other, game_context->player_context->start_position);
  303. game_context->player_context->health = game_context->player_context->max_health;
  304. // subtract player's XP by the enemy's strength
  305. game_context->player_context->xp -= enemy_context->strength;
  306. if ((int)game_context->player_context->xp < 0)
  307. {
  308. game_context->player_context->xp = 0;
  309. }
  310. }
  311. else
  312. {
  313. FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
  314. game_context->player_context->state = PLAYER_ATTACKED;
  315. // Bounce the player back by X units opposite their last movement direction
  316. player_pos.x -= game_context->player_context->dx * enemy_context->radius;
  317. player_pos.y -= game_context->player_context->dy * enemy_context->radius;
  318. entity_pos_set(other, player_pos);
  319. // Reset player's movement direction to prevent immediate re-collision
  320. game_context->player_context->dx = 0;
  321. game_context->player_context->dy = 0;
  322. }
  323. }
  324. else
  325. {
  326. 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));
  327. }
  328. }
  329. else // handle other collisions
  330. {
  331. // bounce player and enemy away from each other
  332. Vector player_pos = entity_pos_get(other);
  333. Vector enemy_pos = entity_pos_get(self);
  334. // Calculate the direction vector from player to enemy
  335. Vector direction_vector = {
  336. enemy_pos.x - player_pos.x,
  337. enemy_pos.y - player_pos.y};
  338. // Normalize the direction vector
  339. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  340. if (length != 0)
  341. {
  342. direction_vector.x /= length;
  343. direction_vector.y /= length;
  344. }
  345. // Move the player and enemy away from each other
  346. player_pos.y -= direction_vector.y * 3;
  347. entity_pos_set(other, player_pos);
  348. enemy_pos.x += direction_vector.x * 3;
  349. entity_pos_set(self, enemy_pos);
  350. // Reset player's movement direction to prevent immediate re-collision
  351. game_context->player_context->dx = 0;
  352. game_context->player_context->dy = 0;
  353. }
  354. // Reset enemy's state
  355. enemy_context->state = ENEMY_IDLE;
  356. enemy_context->elapsed_move_timer = 0.0f;
  357. if (game_context->player_context->state == PLAYER_DEAD)
  358. {
  359. // Reset player's position and health
  360. entity_pos_set(other, game_context->player_context->start_position);
  361. game_context->player_context->health = 100;
  362. }
  363. }
  364. }
  365. // Enemy update function
  366. static void enemy_update(Entity *self, GameManager *manager, void *context)
  367. {
  368. if (!self || !context || !manager)
  369. return;
  370. EnemyContext *enemy_context = (EnemyContext *)context;
  371. if (!enemy_context || enemy_context->state == ENEMY_DEAD)
  372. {
  373. return;
  374. }
  375. GameContext *game_context = game_manager_game_context_get(manager);
  376. if (!game_context)
  377. {
  378. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  379. return;
  380. }
  381. float delta_time = 1.0f / game_context->fps;
  382. // Increment the elapsed_attack_timer for the enemy
  383. enemy_context->elapsed_attack_timer += delta_time;
  384. switch (enemy_context->state)
  385. {
  386. case ENEMY_IDLE:
  387. // Increment the elapsed_move_timer
  388. enemy_context->elapsed_move_timer += delta_time;
  389. // Check if it's time to move again
  390. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  391. {
  392. // Determine the next state based on the current position
  393. Vector current_pos = entity_pos_get(self);
  394. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)EPSILON &&
  395. fabs(current_pos.y - enemy_context->start_position.y) < (double)EPSILON)
  396. {
  397. enemy_context->state = ENEMY_MOVING_TO_END;
  398. }
  399. else
  400. {
  401. enemy_context->state = ENEMY_MOVING_TO_START;
  402. }
  403. enemy_context->elapsed_move_timer = 0.0f;
  404. }
  405. break;
  406. case ENEMY_MOVING_TO_END:
  407. case ENEMY_MOVING_TO_START:
  408. {
  409. // Determine the target position based on the current state
  410. Vector target_position = (enemy_context->state == ENEMY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  411. // Get current position
  412. Vector current_pos = entity_pos_get(self);
  413. Vector direction_vector = {0, 0};
  414. // Calculate direction towards the target
  415. if (current_pos.x < target_position.x)
  416. {
  417. direction_vector.x = 1.0f;
  418. enemy_context->direction = ENEMY_RIGHT;
  419. }
  420. else if (current_pos.x > target_position.x)
  421. {
  422. direction_vector.x = -1.0f;
  423. enemy_context->direction = ENEMY_LEFT;
  424. }
  425. if (current_pos.y < target_position.y)
  426. {
  427. direction_vector.y = 1.0f;
  428. enemy_context->direction = ENEMY_DOWN;
  429. }
  430. else if (current_pos.y > target_position.y)
  431. {
  432. direction_vector.y = -1.0f;
  433. enemy_context->direction = ENEMY_UP;
  434. }
  435. // Normalize direction vector
  436. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  437. if (length != 0)
  438. {
  439. direction_vector.x /= length;
  440. direction_vector.y /= length;
  441. }
  442. // Update position based on direction and speed
  443. Vector new_pos = current_pos;
  444. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  445. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  446. // Clamp the position to the target to prevent overshooting
  447. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  448. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  449. {
  450. new_pos.x = target_position.x;
  451. }
  452. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  453. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  454. {
  455. new_pos.y = target_position.y;
  456. }
  457. entity_pos_set(self, new_pos);
  458. // Check if the enemy has reached or surpassed the target_position
  459. bool reached_x = fabs(new_pos.x - target_position.x) < (double)EPSILON;
  460. bool reached_y = fabs(new_pos.y - target_position.y) < (double)EPSILON;
  461. // If reached the target position on both axes, transition to IDLE
  462. if (reached_x && reached_y)
  463. {
  464. enemy_context->state = ENEMY_IDLE;
  465. enemy_context->elapsed_move_timer = 0.0f;
  466. }
  467. }
  468. break;
  469. default:
  470. break;
  471. }
  472. }
  473. // Free function for the entity
  474. static void enemy_free(Entity *self, GameManager *manager, void *context)
  475. {
  476. UNUSED(self);
  477. UNUSED(manager);
  478. if (context)
  479. enemy_generic_free(context);
  480. }
  481. // Enemy behavior structure
  482. static const EntityDescription _generic_enemy = {
  483. .start = enemy_start,
  484. .stop = enemy_free,
  485. .update = enemy_update,
  486. .render = enemy_render,
  487. .collision = enemy_collision,
  488. .event = NULL,
  489. .context_size = sizeof(EnemyContext),
  490. };
  491. // Enemy function to return the entity description
  492. const EntityDescription *enemy(
  493. GameManager *manager,
  494. const char *id,
  495. int index,
  496. Vector start_position,
  497. Vector end_position,
  498. float move_timer, // Wait duration before moving again
  499. float speed,
  500. float attack_timer,
  501. float strength,
  502. float health)
  503. {
  504. SpriteContext *sprite_context = get_sprite_context(id);
  505. if (!sprite_context)
  506. {
  507. FURI_LOG_E("Game", "Failed to get SpriteContext");
  508. return NULL;
  509. }
  510. // Allocate a new EnemyContext with provided parameters
  511. enemy_context_generic = enemy_generic_alloc(
  512. id,
  513. index,
  514. (Vector){sprite_context->width, sprite_context->height},
  515. start_position,
  516. end_position,
  517. move_timer, // Set wait duration
  518. speed,
  519. attack_timer,
  520. strength,
  521. health);
  522. if (!enemy_context_generic)
  523. {
  524. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  525. return NULL;
  526. }
  527. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  528. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  529. // Set initial direction based on start and end positions
  530. if (start_position.x < end_position.x)
  531. {
  532. enemy_context_generic->direction = ENEMY_RIGHT;
  533. }
  534. else
  535. {
  536. enemy_context_generic->direction = ENEMY_LEFT;
  537. }
  538. // Set initial state based on movement
  539. if (start_position.x != end_position.x || start_position.y != end_position.y)
  540. {
  541. enemy_context_generic->state = ENEMY_MOVING_TO_END;
  542. }
  543. else
  544. {
  545. enemy_context_generic->state = ENEMY_IDLE;
  546. }
  547. return &_generic_enemy;
  548. }
  549. void spawn_enemy_json_furi(Level *level, GameManager *manager, FuriString *json)
  550. {
  551. if (!level)
  552. {
  553. FURI_LOG_E("Game", "Level is NULL");
  554. return;
  555. }
  556. if (!json)
  557. {
  558. FURI_LOG_E("Game", "JSON is NULL");
  559. return;
  560. }
  561. if (!manager)
  562. {
  563. FURI_LOG_E("Game", "GameManager is NULL");
  564. return;
  565. }
  566. // 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
  567. FuriString *id = get_json_value_furi("id", json);
  568. FuriString *_index = get_json_value_furi("index", json);
  569. //
  570. FuriString *start_position = get_json_value_furi("start_position", json);
  571. FuriString *start_position_x = get_json_value_furi("x", start_position);
  572. FuriString *start_position_y = get_json_value_furi("y", start_position);
  573. //
  574. FuriString *end_position = get_json_value_furi("end_position", json);
  575. FuriString *end_position_x = get_json_value_furi("x", end_position);
  576. FuriString *end_position_y = get_json_value_furi("y", end_position);
  577. //
  578. FuriString *move_timer = get_json_value_furi("move_timer", json);
  579. FuriString *speed = get_json_value_furi("speed", json);
  580. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  581. FuriString *strength = get_json_value_furi("strength", json);
  582. FuriString *health = get_json_value_furi("health", json);
  583. //
  584. 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)
  585. {
  586. FURI_LOG_E("Game", "Failed to parse JSON values");
  587. return;
  588. }
  589. GameContext *game_context = game_manager_game_context_get(manager);
  590. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  591. {
  592. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  593. manager,
  594. furi_string_get_cstr(id),
  595. atoi(furi_string_get_cstr(_index)),
  596. (Vector){strtod(furi_string_get_cstr(start_position_x), NULL), strtod(furi_string_get_cstr(start_position_y), NULL)},
  597. (Vector){strtod(furi_string_get_cstr(end_position_x), NULL), strtod(furi_string_get_cstr(end_position_y), NULL)},
  598. strtod(furi_string_get_cstr(move_timer), NULL),
  599. strtod(furi_string_get_cstr(speed), NULL),
  600. strtod(furi_string_get_cstr(attack_timer), NULL),
  601. strtod(furi_string_get_cstr(strength), NULL),
  602. strtod(furi_string_get_cstr(health), NULL)));
  603. game_context->enemy_count++;
  604. }
  605. furi_string_free(id);
  606. furi_string_free(_index);
  607. furi_string_free(start_position);
  608. furi_string_free(start_position_x);
  609. furi_string_free(start_position_y);
  610. furi_string_free(end_position);
  611. furi_string_free(end_position_x);
  612. furi_string_free(end_position_y);
  613. furi_string_free(move_timer);
  614. furi_string_free(speed);
  615. furi_string_free(attack_timer);
  616. furi_string_free(strength);
  617. furi_string_free(health);
  618. }