enemy.c 29 KB

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