enemy.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. const char *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. snprintf(enemy_context_generic->id, sizeof(enemy_context_generic->id), "%s", 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. snprintf(enemy_context->id, sizeof(enemy_context->id), "%s", 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 - camera_x - enemy_context->size.x / 2;
  112. int y_pos = pos.y - 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 - camera_x - (enemy_context->size.x / 2),
  134. pos.y - 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 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 '%s'!", 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 '%s' 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 '%s'!", 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. 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 '%s' 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. 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 '%s'", (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 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. // Get player context
  398. PlayerContext *pctx = entity_context_get(game_context->player);
  399. if (!pctx)
  400. {
  401. FURI_LOG_E(TAG, "Failed to get player context");
  402. return;
  403. }
  404. // check username
  405. if (strlen(enemy->username) == 0 ||
  406. is_str(enemy->username, "SYSTEM_ENEMY") ||
  407. is_str(pctx->username, enemy->username))
  408. {
  409. // Invalid username
  410. return;
  411. }
  412. if (game_context->fhttp->last_response != NULL && strlen(game_context->fhttp->last_response) > 0)
  413. {
  414. // for debugging
  415. // save_char("received_pvp_position", game_context->fhttp->last_response);
  416. // parse the response and set the enemy position
  417. /* expected response:
  418. {
  419. "u": "JBlanked",
  420. "xp": 37743,
  421. "h": 207,
  422. "ehr": 0.7,
  423. "eat": 127.5,
  424. "d": 2,
  425. "s": 1,
  426. "sp": {
  427. "x": 381.0,
  428. "y": 192.0
  429. }
  430. }
  431. */
  432. // FuriStrings are probably safer but we already last_response as a char*
  433. // match username
  434. char *u = get_json_value("u", game_context->fhttp->last_response);
  435. if (!u || !is_str(u, enemy->username))
  436. {
  437. if (u)
  438. free(u);
  439. return;
  440. }
  441. // we need the health, elapsed attack timer, direction, and position
  442. char *h = get_json_value("h", game_context->fhttp->last_response);
  443. char *eat = get_json_value("eat", game_context->fhttp->last_response);
  444. char *d = get_json_value("d", game_context->fhttp->last_response);
  445. char *sp = get_json_value("sp", game_context->fhttp->last_response);
  446. char *x = get_json_value("x", sp);
  447. char *y = get_json_value("y", sp);
  448. if (!h || !eat || !d || !sp || !x || !y)
  449. {
  450. if (h)
  451. free(h);
  452. if (eat)
  453. free(eat);
  454. if (d)
  455. free(d);
  456. if (sp)
  457. free(sp);
  458. if (x)
  459. free(x);
  460. if (y)
  461. free(y);
  462. free(u);
  463. return;
  464. }
  465. // set enemy info
  466. enemy->health = (float)atoi(h);
  467. enemy->elapsed_attack_timer = (float)atof_(eat);
  468. switch (atoi(d))
  469. {
  470. case 0:
  471. enemy->direction = ENTITY_LEFT;
  472. break;
  473. case 1:
  474. enemy->direction = ENTITY_RIGHT;
  475. break;
  476. case 2:
  477. enemy->direction = ENTITY_UP;
  478. break;
  479. case 3:
  480. enemy->direction = ENTITY_DOWN;
  481. break;
  482. default:
  483. enemy->direction = ENTITY_RIGHT;
  484. break;
  485. }
  486. Vector new_pos = (Vector){
  487. .x = (float)atof_(x),
  488. .y = (float)atof_(y),
  489. };
  490. // set enemy position
  491. entity_pos_set(self, new_pos);
  492. // free the strings
  493. free(h);
  494. free(eat);
  495. free(d);
  496. free(sp);
  497. free(x);
  498. free(y);
  499. free(u);
  500. }
  501. }
  502. // Enemy update function
  503. static void enemy_update(Entity *self, GameManager *manager, void *context)
  504. {
  505. if (!self || !context || !manager)
  506. return;
  507. EntityContext *enemy_context = (EntityContext *)context;
  508. if (!enemy_context || enemy_context->state == ENTITY_DEAD)
  509. {
  510. return;
  511. }
  512. GameContext *game_context = game_manager_game_context_get(manager);
  513. if (!game_context)
  514. {
  515. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  516. return;
  517. }
  518. const float delta_time = 1.0f / game_context->fps;
  519. if (game_context->game_mode == GAME_MODE_PVP)
  520. {
  521. // update enemy position
  522. pvp_position(game_context, enemy_context, self);
  523. }
  524. else
  525. {
  526. // Increment the elapsed_attack_timer for the enemy
  527. enemy_context->elapsed_attack_timer += delta_time;
  528. switch (enemy_context->state)
  529. {
  530. case ENTITY_IDLE:
  531. // Increment the elapsed_move_timer
  532. enemy_context->elapsed_move_timer += delta_time;
  533. // Check if it's time to move again
  534. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  535. {
  536. // Determine the next state based on the current position
  537. Vector current_pos = entity_pos_get(self);
  538. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
  539. fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
  540. {
  541. enemy_context->state = ENTITY_MOVING_TO_END;
  542. }
  543. else
  544. {
  545. enemy_context->state = ENTITY_MOVING_TO_START;
  546. }
  547. enemy_context->elapsed_move_timer = 0.0f;
  548. }
  549. break;
  550. case ENTITY_MOVING_TO_END:
  551. case ENTITY_MOVING_TO_START:
  552. case ENTITY_ATTACKED:
  553. {
  554. // Get current position
  555. Vector current_pos = entity_pos_get(self);
  556. if (enemy_context->state == ENTITY_ATTACKED)
  557. {
  558. // set direction again
  559. enemy_context->state = enemy_context->direction == ENTITY_LEFT ? ENTITY_MOVING_TO_START : ENTITY_MOVING_TO_END;
  560. }
  561. // Determine the target position based on the current state
  562. Vector target_position = (enemy_context->state == ENTITY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  563. Vector direction_vector = {0, 0};
  564. // Calculate direction towards the target
  565. if (current_pos.x < target_position.x)
  566. {
  567. direction_vector.x = 1.0f;
  568. enemy_context->direction = ENTITY_RIGHT;
  569. }
  570. else if (current_pos.x > target_position.x)
  571. {
  572. direction_vector.x = -1.0f;
  573. enemy_context->direction = ENTITY_LEFT;
  574. }
  575. if (current_pos.y < target_position.y)
  576. {
  577. direction_vector.y = 1.0f;
  578. enemy_context->direction = ENTITY_DOWN;
  579. }
  580. else if (current_pos.y > target_position.y)
  581. {
  582. direction_vector.y = -1.0f;
  583. enemy_context->direction = ENTITY_UP;
  584. }
  585. // Normalize direction vector
  586. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  587. if (length != 0)
  588. {
  589. direction_vector.x /= length;
  590. direction_vector.y /= length;
  591. }
  592. // Update position based on direction and speed
  593. Vector new_pos = current_pos;
  594. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  595. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  596. // Clamp the position to the target to prevent overshooting
  597. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  598. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  599. {
  600. new_pos.x = target_position.x;
  601. }
  602. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  603. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  604. {
  605. new_pos.y = target_position.y;
  606. }
  607. // Set the new position
  608. entity_pos_set(self, new_pos);
  609. // Check if the enemy has reached or surpassed the target_position
  610. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  611. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  612. // If reached the target position on both axes, transition to IDLE
  613. if (reached_x && reached_y)
  614. {
  615. enemy_context->state = ENTITY_IDLE;
  616. enemy_context->elapsed_move_timer = 0.0f;
  617. }
  618. }
  619. break;
  620. default:
  621. break;
  622. }
  623. }
  624. }
  625. // Free function for the entity
  626. static void enemy_free(Entity *self, GameManager *manager, void *context)
  627. {
  628. UNUSED(self);
  629. UNUSED(manager);
  630. UNUSED(context);
  631. if (enemy_context_generic)
  632. {
  633. free(enemy_context_generic);
  634. enemy_context_generic = NULL;
  635. }
  636. }
  637. // Enemy behavior structure
  638. static const EntityDescription _generic_enemy = {
  639. .start = enemy_start,
  640. .stop = enemy_free,
  641. .update = enemy_update,
  642. .render = enemy_render,
  643. .collision = enemy_collision,
  644. .event = NULL,
  645. .context_size = sizeof(EntityContext),
  646. };
  647. // Enemy function to return the entity description
  648. const EntityDescription *enemy(
  649. GameManager *manager,
  650. const char *id,
  651. int index,
  652. Vector start_position,
  653. Vector end_position,
  654. float move_timer, // Wait duration before moving again
  655. float speed,
  656. float attack_timer,
  657. float strength,
  658. float health,
  659. bool is_user,
  660. FuriString *username)
  661. {
  662. SpriteContext *sprite_context = get_sprite_context(id);
  663. if (!sprite_context)
  664. {
  665. FURI_LOG_E("Game", "Failed to get SpriteContext");
  666. return NULL;
  667. }
  668. // Allocate a new EntityContext with provided parameters
  669. enemy_context_generic = enemy_generic_alloc(
  670. id,
  671. index,
  672. (Vector){sprite_context->width, sprite_context->height},
  673. start_position,
  674. end_position,
  675. move_timer, // Set wait duration
  676. speed,
  677. attack_timer,
  678. strength,
  679. health,
  680. is_user, username);
  681. if (!enemy_context_generic)
  682. {
  683. FURI_LOG_E("Game", "Failed to allocate EntityContext");
  684. return NULL;
  685. }
  686. // assign sprites to the context
  687. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  688. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  689. // Set initial direction based on start and end positions
  690. if (start_position.x < end_position.x)
  691. {
  692. enemy_context_generic->direction = ENTITY_RIGHT;
  693. }
  694. else
  695. {
  696. enemy_context_generic->direction = ENTITY_LEFT;
  697. }
  698. // Set initial state based on movement
  699. if (start_position.x != end_position.x || start_position.y != end_position.y)
  700. {
  701. enemy_context_generic->state = ENTITY_MOVING_TO_END;
  702. }
  703. else
  704. {
  705. enemy_context_generic->state = ENTITY_IDLE;
  706. }
  707. free(sprite_context);
  708. return &_generic_enemy;
  709. }
  710. void spawn_enemy(Level *level, GameManager *manager, FuriString *json)
  711. {
  712. if (!level || !manager || !json)
  713. {
  714. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  715. return;
  716. }
  717. FuriString *id = get_json_value_furi("id", json);
  718. FuriString *_index = get_json_value_furi("index", json);
  719. //
  720. FuriString *start_position = get_json_value_furi("start_position", json);
  721. FuriString *start_position_x = get_json_value_furi("x", start_position);
  722. FuriString *start_position_y = get_json_value_furi("y", start_position);
  723. //
  724. FuriString *end_position = get_json_value_furi("end_position", json);
  725. FuriString *end_position_x = get_json_value_furi("x", end_position);
  726. FuriString *end_position_y = get_json_value_furi("y", end_position);
  727. //
  728. FuriString *move_timer = get_json_value_furi("move_timer", json);
  729. FuriString *speed = get_json_value_furi("speed", json);
  730. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  731. FuriString *strength = get_json_value_furi("strength", json);
  732. FuriString *health = get_json_value_furi("health", json);
  733. //
  734. 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)
  735. {
  736. FURI_LOG_E("Game", "Failed to parse JSON values");
  737. return;
  738. }
  739. FuriString *is_user = get_json_value_furi("is_user", json);
  740. bool is_user_value = false;
  741. if (is_user)
  742. {
  743. is_user_value = strstr(furi_string_get_cstr(is_user), "true") != NULL;
  744. }
  745. FuriString *username = get_json_value_furi("username", json);
  746. // no need to check for username, it is optional
  747. GameContext *game_context = game_manager_game_context_get(manager);
  748. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  749. {
  750. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  751. manager,
  752. furi_string_get_cstr(id),
  753. atoi(furi_string_get_cstr(_index)),
  754. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  755. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  756. atof_furi(move_timer),
  757. atof_furi(speed),
  758. atof_furi(attack_timer),
  759. atof_furi(strength),
  760. atof_furi(health),
  761. is_user_value, username));
  762. game_context->enemy_count++;
  763. }
  764. furi_string_free(id);
  765. furi_string_free(_index);
  766. furi_string_free(start_position);
  767. furi_string_free(start_position_x);
  768. furi_string_free(start_position_y);
  769. furi_string_free(end_position);
  770. furi_string_free(end_position_x);
  771. furi_string_free(end_position_y);
  772. furi_string_free(move_timer);
  773. furi_string_free(speed);
  774. furi_string_free(attack_timer);
  775. furi_string_free(strength);
  776. furi_string_free(health);
  777. if (is_user)
  778. {
  779. furi_string_free(is_user);
  780. }
  781. if (username)
  782. {
  783. furi_string_free(username);
  784. }
  785. }