enemy.c 26 KB

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