enemy.c 28 KB

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