enemy.c 31 KB

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