enemy.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. bool is_user)
  18. {
  19. if (!enemy_context_generic)
  20. {
  21. enemy_context_generic = malloc(sizeof(EntityContext));
  22. }
  23. if (!enemy_context_generic)
  24. {
  25. FURI_LOG_E("Game", "Failed to allocate EntityContext");
  26. return NULL;
  27. }
  28. snprintf(enemy_context_generic->id, sizeof(enemy_context_generic->id), "%s", id);
  29. enemy_context_generic->index = index;
  30. enemy_context_generic->size = size;
  31. enemy_context_generic->start_position = start_position;
  32. enemy_context_generic->end_position = end_position;
  33. enemy_context_generic->move_timer = move_timer; // Set wait duration
  34. enemy_context_generic->elapsed_move_timer = 0.0f; // Initialize elapsed timer
  35. enemy_context_generic->speed = speed;
  36. enemy_context_generic->attack_timer = attack_timer;
  37. enemy_context_generic->strength = strength;
  38. enemy_context_generic->health = health;
  39. // Initialize other fields as needed
  40. enemy_context_generic->sprite_right = NULL; // sprite is assigned later
  41. enemy_context_generic->sprite_left = NULL; // sprite is assigned later
  42. enemy_context_generic->direction = ENTITY_RIGHT; // Default direction
  43. enemy_context_generic->state = ENTITY_MOVING_TO_END; // Start in IDLE state
  44. // Set radius based on size, for example, average of size.x and size.y divided by 2
  45. enemy_context_generic->radius = (size.x + size.y) / 4.0f;
  46. //
  47. enemy_context_generic->is_user = is_user;
  48. return enemy_context_generic;
  49. }
  50. // Enemy start function
  51. static void enemy_start(Entity *self, GameManager *manager, void *context)
  52. {
  53. UNUSED(manager);
  54. if (!self || !context)
  55. {
  56. FURI_LOG_E("Game", "Enemy start: Invalid parameters");
  57. return;
  58. }
  59. if (!enemy_context_generic)
  60. {
  61. FURI_LOG_E("Game", "Enemy start: Enemy context not set");
  62. return;
  63. }
  64. EntityContext *enemy_context = (EntityContext *)context;
  65. // Copy fields from generic context
  66. snprintf(enemy_context->id, sizeof(enemy_context->id), "%s", enemy_context_generic->id);
  67. enemy_context->index = enemy_context_generic->index;
  68. enemy_context->size = enemy_context_generic->size;
  69. enemy_context->start_position = enemy_context_generic->start_position;
  70. enemy_context->end_position = enemy_context_generic->end_position;
  71. enemy_context->move_timer = enemy_context_generic->move_timer;
  72. enemy_context->elapsed_move_timer = enemy_context_generic->elapsed_move_timer;
  73. enemy_context->speed = enemy_context_generic->speed;
  74. enemy_context->attack_timer = enemy_context_generic->attack_timer;
  75. enemy_context->strength = enemy_context_generic->strength;
  76. enemy_context->health = enemy_context_generic->health;
  77. enemy_context->sprite_right = enemy_context_generic->sprite_right;
  78. enemy_context->sprite_left = enemy_context_generic->sprite_left;
  79. enemy_context->direction = enemy_context_generic->direction;
  80. enemy_context->state = enemy_context_generic->state;
  81. enemy_context->radius = enemy_context_generic->radius;
  82. enemy_context->is_user = enemy_context_generic->is_user;
  83. // Set enemy's initial position based on start_position
  84. entity_pos_set(self, enemy_context->start_position);
  85. // Add collision circle based on the enemy's radius
  86. entity_collider_add_circle(self, enemy_context->radius);
  87. }
  88. // Enemy render function
  89. static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  90. {
  91. if (!self || !context || !canvas || !manager)
  92. return;
  93. EntityContext *enemy_context = (EntityContext *)context;
  94. GameContext *game_context = game_manager_game_context_get(manager);
  95. // Get the position of the enemy
  96. Vector pos = entity_pos_get(self);
  97. // Get the camera position
  98. int x_pos = pos.x - camera_x - enemy_context->size.x / 2;
  99. int y_pos = pos.y - camera_y - enemy_context->size.y / 2;
  100. // check if position is within the screen
  101. if (x_pos + enemy_context->size.x < 0 || x_pos > SCREEN_WIDTH || y_pos + enemy_context->size.y < 0 || y_pos > SCREEN_HEIGHT)
  102. return;
  103. // Choose sprite based on direction
  104. Sprite *current_sprite = NULL;
  105. if (enemy_context->direction == ENTITY_LEFT)
  106. {
  107. current_sprite = enemy_context->sprite_left;
  108. }
  109. else
  110. {
  111. current_sprite = enemy_context->sprite_right;
  112. }
  113. // no enemies in story mode for now
  114. if (game_context->game_mode != GAME_MODE_STORY || (game_context->game_mode == GAME_MODE_STORY && game_context->tutorial_step == 4))
  115. {
  116. // Draw enemy sprite relative to camera, centered on the enemy's position
  117. canvas_draw_sprite(
  118. canvas,
  119. current_sprite,
  120. pos.x - camera_x - (enemy_context->size.x / 2),
  121. pos.y - camera_y - (enemy_context->size.y / 2));
  122. // draw health of enemy
  123. char health_str[32];
  124. snprintf(health_str, sizeof(health_str), "%.0f", (double)enemy_context->health);
  125. draw_username(canvas, pos, health_str);
  126. }
  127. }
  128. static void atk_notify(GameContext *game_context, EntityContext *enemy_context, bool player_attacked)
  129. {
  130. if (!game_context || !enemy_context)
  131. {
  132. FURI_LOG_E("Game", "Send attack notification: Invalid parameters");
  133. return;
  134. }
  135. NotificationApp *notifications = furi_record_open(RECORD_NOTIFICATION);
  136. const bool vibration_allowed = strstr(yes_or_no_choices[vibration_on_index], "Yes") != NULL;
  137. const bool sound_allowed = strstr(yes_or_no_choices[sound_on_index], "Yes") != NULL;
  138. if (player_attacked)
  139. {
  140. if (vibration_allowed && sound_allowed)
  141. {
  142. notification_message(notifications, &sequence_success);
  143. }
  144. else if (vibration_allowed && !sound_allowed)
  145. {
  146. notification_message(notifications, &sequence_single_vibro);
  147. }
  148. else if (!vibration_allowed && sound_allowed)
  149. {
  150. // change this to sound later
  151. notification_message(notifications, &sequence_blink_blue_100);
  152. }
  153. else
  154. {
  155. notification_message(notifications, &sequence_blink_blue_100);
  156. }
  157. FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
  158. }
  159. else
  160. {
  161. if (vibration_allowed && sound_allowed)
  162. {
  163. notification_message(notifications, &sequence_error);
  164. }
  165. else if (vibration_allowed && !sound_allowed)
  166. {
  167. notification_message(notifications, &sequence_single_vibro);
  168. }
  169. else if (!vibration_allowed && sound_allowed)
  170. {
  171. // change this to sound later
  172. notification_message(notifications, &sequence_blink_red_100);
  173. }
  174. else
  175. {
  176. notification_message(notifications, &sequence_blink_red_100);
  177. }
  178. FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
  179. }
  180. // close the notifications
  181. furi_record_close(RECORD_NOTIFICATION);
  182. }
  183. // Enemy collision function
  184. static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  185. {
  186. if (!self || !other || !context || !manager)
  187. {
  188. FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
  189. return;
  190. }
  191. EntityContext *enemy_context = (EntityContext *)context;
  192. furi_check(enemy_context, "Enemy collision: EntityContext is NULL");
  193. GameContext *game_context = game_manager_game_context_get(manager);
  194. furi_check(game_context, "Enemy collision: GameContext is NULL");
  195. PlayerContext *player_context = entity_context_get(game_context->player);
  196. if (game_context->game_mode == GAME_MODE_STORY && game_context->tutorial_step != 4)
  197. {
  198. // FURI_LOG_I("Game", "Enemy collision: No enemies in story mode");
  199. return;
  200. }
  201. // Check if the enemy collided with the player
  202. if (entity_description_get(other) == &player_desc)
  203. {
  204. // Get positions of the enemy and the player
  205. Vector enemy_pos = entity_pos_get(self);
  206. Vector player_pos = entity_pos_get(other);
  207. // Determine if the enemy is facing the player or player is facing the enemy
  208. bool enemy_is_facing_player = false;
  209. bool player_is_facing_enemy = false;
  210. // Determine if the enemy is facing the player
  211. if ((enemy_context->direction == ENTITY_LEFT && player_pos.x < enemy_pos.x) ||
  212. (enemy_context->direction == ENTITY_RIGHT && player_pos.x > enemy_pos.x) ||
  213. (enemy_context->direction == ENTITY_UP && player_pos.y < enemy_pos.y) ||
  214. (enemy_context->direction == ENTITY_DOWN && player_pos.y > enemy_pos.y))
  215. {
  216. enemy_is_facing_player = true;
  217. }
  218. // Determine if the player is facing the enemy
  219. if ((player_context->direction == ENTITY_LEFT && enemy_pos.x < player_pos.x) ||
  220. (player_context->direction == ENTITY_RIGHT && enemy_pos.x > player_pos.x) ||
  221. (player_context->direction == ENTITY_UP && enemy_pos.y < player_pos.y) ||
  222. (player_context->direction == ENTITY_DOWN && enemy_pos.y > player_pos.y))
  223. {
  224. player_is_facing_enemy = true;
  225. }
  226. // Handle Player Attacking Enemy (Press OK, facing enemy, and enemy not facing player)
  227. if (player_is_facing_enemy && game_context->last_button == GameKeyOk && !enemy_is_facing_player)
  228. {
  229. if (game_context->game_mode == GAME_MODE_STORY && game_context->tutorial_step == 4)
  230. {
  231. // FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
  232. game_context->tutorial_step++;
  233. }
  234. // Reset last button
  235. game_context->last_button = -1;
  236. if (player_context->elapsed_attack_timer >= player_context->attack_timer)
  237. {
  238. atk_notify(game_context, enemy_context, true);
  239. // Reset player's elapsed attack timer
  240. player_context->elapsed_attack_timer = 0.0f;
  241. enemy_context->elapsed_attack_timer = 0.0f; // Reset enemy's attack timer to block enemy attack
  242. // Increase XP by the enemy's strength
  243. player_context->xp += enemy_context->strength;
  244. // Increase healthy by 10% of the enemy's strength
  245. player_context->health += enemy_context->strength * 0.1f;
  246. if (player_context->health > player_context->max_health)
  247. {
  248. player_context->health = player_context->max_health;
  249. }
  250. // Decrease enemy health by player strength
  251. enemy_context->health -= player_context->strength;
  252. if (enemy_context->health <= 0)
  253. {
  254. enemy_context->state = ENTITY_DEAD;
  255. // Reset enemy position and health
  256. enemy_context->health = 100; // this needs to be set to the enemy's max health
  257. // remove from game context and set in safe zone
  258. game_context->enemies[enemy_context->index] = NULL;
  259. game_context->enemy_count--;
  260. entity_collider_remove(self);
  261. entity_pos_set(self, (Vector){-100, -100});
  262. return;
  263. }
  264. else
  265. {
  266. enemy_context->state = ENTITY_ATTACKED;
  267. // Vector old_pos = entity_pos_get(self);
  268. // Bounce the enemy back by X units opposite their last movement direction
  269. enemy_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
  270. // enemy_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
  271. entity_pos_set(self, enemy_pos);
  272. // Reset enemy's movement direction to prevent immediate re-collision
  273. player_context->dx = 0;
  274. player_context->dy = 0;
  275. }
  276. }
  277. else
  278. {
  279. FURI_LOG_I("Game", "Player attack on enemy '%s' is on cooldown: %f seconds remaining", enemy_context->id, (double)(player_context->attack_timer - player_context->elapsed_attack_timer));
  280. }
  281. }
  282. // Handle Enemy Attacking Player (enemy facing player)
  283. else if (enemy_is_facing_player)
  284. {
  285. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  286. {
  287. atk_notify(game_context, enemy_context, false);
  288. // Reset enemy's elapsed attack timer
  289. enemy_context->elapsed_attack_timer = 0.0f;
  290. // Decrease player health by enemy strength
  291. player_context->health -= enemy_context->strength;
  292. if (player_context->health <= 0)
  293. {
  294. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  295. player_context->state = ENTITY_DEAD;
  296. // Reset player position and health
  297. entity_pos_set(other, player_context->start_position);
  298. player_context->health = player_context->max_health;
  299. // subtract player's XP by the enemy's strength
  300. player_context->xp -= enemy_context->strength;
  301. if ((int)player_context->xp < 0)
  302. {
  303. player_context->xp = 0;
  304. }
  305. }
  306. else
  307. {
  308. FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
  309. player_context->state = ENTITY_ATTACKED;
  310. // Bounce the player back by X units opposite their last movement direction
  311. player_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
  312. // player_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
  313. entity_pos_set(other, player_pos);
  314. // Reset player's movement direction to prevent immediate re-collision
  315. player_context->dx = 0;
  316. player_context->dy = 0;
  317. }
  318. }
  319. }
  320. else // handle other collisions
  321. {
  322. // Set the player's old position to prevent collision
  323. entity_pos_set(other, player_context->old_position);
  324. // Reset player's movement direction to prevent immediate re-collision
  325. player_context->dx = 0;
  326. player_context->dy = 0;
  327. }
  328. if (player_context->state == ENTITY_DEAD)
  329. {
  330. // Reset player's position and health
  331. entity_pos_set(other, player_context->start_position);
  332. player_context->health = player_context->max_health;
  333. }
  334. }
  335. // if not player than must be an icon or npc; so push back
  336. else
  337. {
  338. // push enemy back
  339. Vector enemy_pos = entity_pos_get(self);
  340. switch (enemy_context->direction)
  341. {
  342. case ENTITY_LEFT:
  343. enemy_pos.x += (enemy_context->size.x + game_context->icon_offset);
  344. break;
  345. case ENTITY_RIGHT:
  346. enemy_pos.x -= (enemy_context->size.x + game_context->icon_offset);
  347. break;
  348. case ENTITY_UP:
  349. enemy_pos.y += (enemy_context->size.y + game_context->icon_offset);
  350. break;
  351. case ENTITY_DOWN:
  352. enemy_pos.y -= (enemy_context->size.y + game_context->icon_offset);
  353. break;
  354. default:
  355. break;
  356. }
  357. entity_pos_set(self, enemy_pos);
  358. }
  359. }
  360. static void pvp_position(GameContext *game_context, EntityContext *enemy)
  361. {
  362. if (!game_context || !enemy)
  363. {
  364. FURI_LOG_E("Game", "PVP position: Invalid parameters");
  365. return;
  366. }
  367. if (game_context->fhttp->last_response != NULL && strlen(game_context->fhttp->last_response) > 0)
  368. {
  369. // parse the response and set the enemy position
  370. /* expected response:
  371. {
  372. "u": "JBlanked",
  373. "xp": 37743,
  374. "h": 207,
  375. "ehr": 0.7,
  376. "eat": 127.5,
  377. "d": 2,
  378. "s": 1,
  379. "sp": {
  380. "x": 381.0,
  381. "y": 192.0
  382. }
  383. }
  384. */
  385. // FuriStrings are probably safer but we already last_response as a char*
  386. // we need the health, elapsed attack timer, direction, and position
  387. char *h = get_json_value("h", game_context->fhttp->last_response);
  388. char *eat = get_json_value("eat", game_context->fhttp->last_response);
  389. char *d = get_json_value("d", game_context->fhttp->last_response);
  390. char *sp = get_json_value("sp", game_context->fhttp->last_response);
  391. char *x = get_json_value("x", sp);
  392. char *y = get_json_value("y", sp);
  393. if (!h || !eat || !d || !sp || !x || !y)
  394. {
  395. FURI_LOG_E("Game", "PVP position: Failed to parse enemy data");
  396. if (h)
  397. free(h);
  398. if (eat)
  399. free(eat);
  400. if (d)
  401. free(d);
  402. if (sp)
  403. free(sp);
  404. if (x)
  405. free(x);
  406. if (y)
  407. free(y);
  408. return;
  409. }
  410. // set enemy info
  411. enemy->health = (float)atoi(h);
  412. enemy->elapsed_attack_timer = (float)atof_(eat);
  413. switch (atoi(d))
  414. {
  415. case 0:
  416. enemy->direction = ENTITY_LEFT;
  417. break;
  418. case 1:
  419. enemy->direction = ENTITY_RIGHT;
  420. break;
  421. case 2:
  422. enemy->direction = ENTITY_UP;
  423. break;
  424. case 3:
  425. enemy->direction = ENTITY_DOWN;
  426. break;
  427. default:
  428. enemy->direction = ENTITY_RIGHT;
  429. break;
  430. }
  431. enemy->start_position.x = (float)atoi(x);
  432. enemy->start_position.y = (float)atoi(y);
  433. enemy->end_position.x = (float)atoi(x);
  434. enemy->end_position.y = (float)atoi(y);
  435. // free the strings
  436. free(h);
  437. free(eat);
  438. free(d);
  439. free(sp);
  440. free(x);
  441. free(y);
  442. }
  443. }
  444. // Enemy update function
  445. static void enemy_update(Entity *self, GameManager *manager, void *context)
  446. {
  447. if (!self || !context || !manager)
  448. return;
  449. EntityContext *enemy_context = (EntityContext *)context;
  450. if (!enemy_context || enemy_context->state == ENTITY_DEAD)
  451. {
  452. return;
  453. }
  454. GameContext *game_context = game_manager_game_context_get(manager);
  455. if (!game_context)
  456. {
  457. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  458. return;
  459. }
  460. if (game_context->game_mode == GAME_MODE_PVP)
  461. {
  462. // update enemy position
  463. pvp_position(game_context, enemy_context);
  464. }
  465. float delta_time = 1.0f / game_context->fps;
  466. // Increment the elapsed_attack_timer for the enemy
  467. enemy_context->elapsed_attack_timer += delta_time;
  468. switch (enemy_context->state)
  469. {
  470. case ENTITY_IDLE:
  471. // Increment the elapsed_move_timer
  472. enemy_context->elapsed_move_timer += delta_time;
  473. // Check if it's time to move again
  474. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  475. {
  476. // Determine the next state based on the current position
  477. Vector current_pos = entity_pos_get(self);
  478. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
  479. fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
  480. {
  481. enemy_context->state = ENTITY_MOVING_TO_END;
  482. }
  483. else
  484. {
  485. enemy_context->state = ENTITY_MOVING_TO_START;
  486. }
  487. enemy_context->elapsed_move_timer = 0.0f;
  488. }
  489. break;
  490. case ENTITY_MOVING_TO_END:
  491. case ENTITY_MOVING_TO_START:
  492. case ENTITY_ATTACKED:
  493. {
  494. // Get current position
  495. Vector current_pos = entity_pos_get(self);
  496. if (enemy_context->state == ENTITY_ATTACKED)
  497. {
  498. // set direction again
  499. enemy_context->state = enemy_context->direction == ENTITY_LEFT ? ENTITY_MOVING_TO_START : ENTITY_MOVING_TO_END;
  500. }
  501. // Determine the target position based on the current state
  502. Vector target_position = (enemy_context->state == ENTITY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  503. Vector direction_vector = {0, 0};
  504. // Calculate direction towards the target
  505. if (current_pos.x < target_position.x)
  506. {
  507. direction_vector.x = 1.0f;
  508. enemy_context->direction = ENTITY_RIGHT;
  509. }
  510. else if (current_pos.x > target_position.x)
  511. {
  512. direction_vector.x = -1.0f;
  513. enemy_context->direction = ENTITY_LEFT;
  514. }
  515. if (current_pos.y < target_position.y)
  516. {
  517. direction_vector.y = 1.0f;
  518. enemy_context->direction = ENTITY_DOWN;
  519. }
  520. else if (current_pos.y > target_position.y)
  521. {
  522. direction_vector.y = -1.0f;
  523. enemy_context->direction = ENTITY_UP;
  524. }
  525. // Normalize direction vector
  526. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  527. if (length != 0)
  528. {
  529. direction_vector.x /= length;
  530. direction_vector.y /= length;
  531. }
  532. // Update position based on direction and speed
  533. Vector new_pos = current_pos;
  534. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  535. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  536. // Clamp the position to the target to prevent overshooting
  537. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  538. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  539. {
  540. new_pos.x = target_position.x;
  541. }
  542. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  543. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  544. {
  545. new_pos.y = target_position.y;
  546. }
  547. entity_pos_set(self, new_pos);
  548. // Check if the enemy has reached or surpassed the target_position
  549. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  550. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  551. // If reached the target position on both axes, transition to IDLE
  552. if (reached_x && reached_y)
  553. {
  554. enemy_context->state = ENTITY_IDLE;
  555. enemy_context->elapsed_move_timer = 0.0f;
  556. }
  557. }
  558. break;
  559. default:
  560. break;
  561. }
  562. }
  563. // Free function for the entity
  564. static void enemy_free(Entity *self, GameManager *manager, void *context)
  565. {
  566. UNUSED(self);
  567. UNUSED(manager);
  568. UNUSED(context);
  569. if (enemy_context_generic)
  570. {
  571. free(enemy_context_generic);
  572. enemy_context_generic = NULL;
  573. }
  574. }
  575. // Enemy behavior structure
  576. static const EntityDescription _generic_enemy = {
  577. .start = enemy_start,
  578. .stop = enemy_free,
  579. .update = enemy_update,
  580. .render = enemy_render,
  581. .collision = enemy_collision,
  582. .event = NULL,
  583. .context_size = sizeof(EntityContext),
  584. };
  585. // Enemy function to return the entity description
  586. const EntityDescription *enemy(
  587. GameManager *manager,
  588. const char *id,
  589. int index,
  590. Vector start_position,
  591. Vector end_position,
  592. float move_timer, // Wait duration before moving again
  593. float speed,
  594. float attack_timer,
  595. float strength,
  596. float health,
  597. bool is_user)
  598. {
  599. SpriteContext *sprite_context = get_sprite_context(id);
  600. if (!sprite_context)
  601. {
  602. FURI_LOG_E("Game", "Failed to get SpriteContext");
  603. return NULL;
  604. }
  605. // Allocate a new EntityContext with provided parameters
  606. enemy_context_generic = enemy_generic_alloc(
  607. id,
  608. index,
  609. (Vector){sprite_context->width, sprite_context->height},
  610. start_position,
  611. end_position,
  612. move_timer, // Set wait duration
  613. speed,
  614. attack_timer,
  615. strength,
  616. health,
  617. is_user);
  618. if (!enemy_context_generic)
  619. {
  620. FURI_LOG_E("Game", "Failed to allocate EntityContext");
  621. return NULL;
  622. }
  623. // assign sprites to the context
  624. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  625. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  626. // Set initial direction based on start and end positions
  627. if (start_position.x < end_position.x)
  628. {
  629. enemy_context_generic->direction = ENTITY_RIGHT;
  630. }
  631. else
  632. {
  633. enemy_context_generic->direction = ENTITY_LEFT;
  634. }
  635. // Set initial state based on movement
  636. if (start_position.x != end_position.x || start_position.y != end_position.y)
  637. {
  638. enemy_context_generic->state = ENTITY_MOVING_TO_END;
  639. }
  640. else
  641. {
  642. enemy_context_generic->state = ENTITY_IDLE;
  643. }
  644. free(sprite_context);
  645. return &_generic_enemy;
  646. }
  647. void spawn_enemy(Level *level, GameManager *manager, FuriString *json)
  648. {
  649. if (!level || !manager || !json)
  650. {
  651. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  652. return;
  653. }
  654. FuriString *id = get_json_value_furi("id", json);
  655. FuriString *_index = get_json_value_furi("index", json);
  656. //
  657. FuriString *start_position = get_json_value_furi("start_position", json);
  658. FuriString *start_position_x = get_json_value_furi("x", start_position);
  659. FuriString *start_position_y = get_json_value_furi("y", start_position);
  660. //
  661. FuriString *end_position = get_json_value_furi("end_position", json);
  662. FuriString *end_position_x = get_json_value_furi("x", end_position);
  663. FuriString *end_position_y = get_json_value_furi("y", end_position);
  664. //
  665. FuriString *move_timer = get_json_value_furi("move_timer", json);
  666. FuriString *speed = get_json_value_furi("speed", json);
  667. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  668. FuriString *strength = get_json_value_furi("strength", json);
  669. FuriString *health = get_json_value_furi("health", json);
  670. //
  671. 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)
  672. {
  673. FURI_LOG_E("Game", "Failed to parse JSON values");
  674. return;
  675. }
  676. FuriString *is_user = get_json_value_furi("is_user", json);
  677. bool is_user_value = false;
  678. if (is_user)
  679. {
  680. is_user_value = strstr(furi_string_get_cstr(is_user), "true") != NULL;
  681. }
  682. GameContext *game_context = game_manager_game_context_get(manager);
  683. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  684. {
  685. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  686. manager,
  687. furi_string_get_cstr(id),
  688. atoi(furi_string_get_cstr(_index)),
  689. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  690. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  691. atof_furi(move_timer),
  692. atof_furi(speed),
  693. atof_furi(attack_timer),
  694. atof_furi(strength),
  695. atof_furi(health),
  696. is_user_value));
  697. game_context->enemy_count++;
  698. }
  699. furi_string_free(id);
  700. furi_string_free(_index);
  701. furi_string_free(start_position);
  702. furi_string_free(start_position_x);
  703. furi_string_free(start_position_y);
  704. furi_string_free(end_position);
  705. furi_string_free(end_position_x);
  706. furi_string_free(end_position_y);
  707. furi_string_free(move_timer);
  708. furi_string_free(speed);
  709. furi_string_free(attack_timer);
  710. furi_string_free(strength);
  711. furi_string_free(health);
  712. if (is_user)
  713. {
  714. furi_string_free(is_user);
  715. }
  716. }