enemy.c 31 KB

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