enemy.c 31 KB

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