enemy.c 27 KB

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