enemy.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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->story_step == 4 || game_context->story_step >= STORY_TUTORIAL_STEPS))
  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->story_step != 4 && game_context->story_step < STORY_TUTORIAL_STEPS)
  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->story_step == 4)
  243. {
  244. // FURI_LOG_I("Game", "Player attacked enemy '%d'!", enemy_context->id);
  245. game_context->story_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_context->end_reason = GAME_END_PVP_ENEMY_DEAD;
  275. game_manager_game_stop(manager);
  276. return;
  277. }
  278. // Reset enemy position and health
  279. enemy_context->health = 100; // this needs to be set to the enemy's max health
  280. // in PVE mode, enemies can respawn
  281. if (game_context->game_mode != GAME_MODE_PVE)
  282. {
  283. // remove from game context and set in safe zone
  284. game_context->enemies[enemy_context->index] = NULL;
  285. game_context->enemy_count--;
  286. entity_collider_remove(self);
  287. entity_pos_set(self, (Vector){-100, -100});
  288. return;
  289. }
  290. }
  291. else
  292. {
  293. enemy_context->state = ENTITY_ATTACKED;
  294. // Vector old_pos = entity_pos_get(self);
  295. // Bounce the enemy back by X units opposite their last movement direction
  296. enemy_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
  297. // enemy_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
  298. entity_pos_set(self, enemy_pos);
  299. // Reset enemy's movement direction to prevent immediate re-collision
  300. player_context->dx = 0;
  301. player_context->dy = 0;
  302. }
  303. }
  304. else
  305. {
  306. 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));
  307. }
  308. }
  309. // Handle Enemy Attacking Player (enemy facing player)
  310. else if (enemy_is_facing_player)
  311. {
  312. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  313. {
  314. enemy_atk_notify(game_context, enemy_context, false);
  315. // Reset enemy's elapsed attack timer
  316. enemy_context->elapsed_attack_timer = 0.0f;
  317. // Decrease player health by enemy strength
  318. player_context->health -= enemy_context->strength;
  319. if (player_context->health <= 0)
  320. {
  321. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  322. player_context->state = ENTITY_DEAD;
  323. // if pvp, end the game
  324. if (game_context->game_mode == GAME_MODE_PVP)
  325. {
  326. save_player_context(player_context);
  327. furi_delay_ms(100);
  328. game_context->end_reason = GAME_END_PVP_PLAYER_DEAD;
  329. game_manager_game_stop(manager);
  330. return;
  331. }
  332. // Reset player position and health
  333. entity_pos_set(other, player_context->start_position);
  334. player_context->health = player_context->max_health;
  335. // subtract player's XP by the enemy's strength
  336. player_context->xp -= enemy_context->strength;
  337. if ((int)player_context->xp < 0)
  338. {
  339. player_context->xp = 0;
  340. }
  341. }
  342. else
  343. {
  344. FURI_LOG_I("Game", "Player took %f damage from enemy '%d'", (double)enemy_context->strength, enemy_context->id);
  345. player_context->state = ENTITY_ATTACKED;
  346. // Bounce the player back by X units opposite their last movement direction
  347. player_pos.x -= player_context->dx * enemy_context->radius + game_context->icon_offset;
  348. // player_pos.y -= player_context->dy * enemy_context->radius + game_context->icon_offset;
  349. entity_pos_set(other, player_pos);
  350. // Reset player's movement direction to prevent immediate re-collision
  351. player_context->dx = 0;
  352. player_context->dy = 0;
  353. }
  354. }
  355. }
  356. else // handle other collisions
  357. {
  358. // Set the player's old position to prevent collision
  359. entity_pos_set(other, player_context->old_position);
  360. // Reset player's movement direction to prevent immediate re-collision
  361. player_context->dx = 0;
  362. player_context->dy = 0;
  363. }
  364. if (player_context->state == ENTITY_DEAD)
  365. {
  366. // Reset player's position and health
  367. entity_pos_set(other, player_context->start_position);
  368. player_context->health = player_context->max_health;
  369. }
  370. }
  371. // if not player than must be an icon or npc; so push back
  372. else
  373. {
  374. // push enemy back
  375. Vector enemy_pos = entity_pos_get(self);
  376. switch (enemy_context->direction)
  377. {
  378. case ENTITY_LEFT:
  379. enemy_pos.x += (enemy_context->size.x + game_context->icon_offset);
  380. break;
  381. case ENTITY_RIGHT:
  382. enemy_pos.x -= (enemy_context->size.x + game_context->icon_offset);
  383. break;
  384. case ENTITY_UP:
  385. enemy_pos.y += (enemy_context->size.y + game_context->icon_offset);
  386. break;
  387. case ENTITY_DOWN:
  388. enemy_pos.y -= (enemy_context->size.y + game_context->icon_offset);
  389. break;
  390. default:
  391. break;
  392. }
  393. entity_pos_set(self, enemy_pos);
  394. }
  395. }
  396. static bool enemy_is_game_enemy(GameManager *manager, const char *username)
  397. {
  398. GameContext *game_context = game_manager_game_context_get(manager);
  399. if (game_context)
  400. {
  401. for (int i = 0; i < MAX_ENEMIES; i++)
  402. {
  403. if (!game_context->enemies[i])
  404. break;
  405. EntityContext *enemy_context = entity_context_get(game_context->enemies[i]);
  406. if (enemy_context && is_str(enemy_context->username, username))
  407. {
  408. return true;
  409. }
  410. }
  411. }
  412. return false;
  413. }
  414. static void enemy_pvp_position(GameManager *manager, EntityContext *enemy, Entity *self)
  415. {
  416. if (!manager || !enemy || !self)
  417. {
  418. FURI_LOG_E("Game", "enemy_pvp_position: Invalid parameters");
  419. return;
  420. }
  421. GameContext *game_context = game_manager_game_context_get(manager);
  422. if (!game_context->fhttp->last_response || strlen(game_context->fhttp->last_response) == 0)
  423. {
  424. return;
  425. }
  426. // parse the response and set the enemy position
  427. /* expected response:
  428. {
  429. "u": "JBlanked",
  430. "xp": 37743,
  431. "h": 207,
  432. "ehr": 0.7,
  433. "eat": 127.5,
  434. "d": 2,
  435. "s": 1,
  436. "sp": {
  437. "x": 381.0,
  438. "y": 192.0
  439. }
  440. }
  441. */
  442. // match username
  443. char *u = get_json_value("u", game_context->fhttp->last_response);
  444. if (!u)
  445. {
  446. FURI_LOG_E("Game", "enemy_pvp_position: Failed to get username");
  447. return;
  448. }
  449. // check if the username matches
  450. if (!is_str(u, enemy->username))
  451. {
  452. if (strlen(u) == 0 || enemy_is_game_enemy(manager, u))
  453. {
  454. free(u);
  455. return;
  456. }
  457. PlayerContext *player_context = entity_context_get(game_context->player);
  458. if (is_str(player_context->username, u))
  459. {
  460. free(u);
  461. return;
  462. }
  463. char *h = get_json_value("h", game_context->fhttp->last_response);
  464. if (!h)
  465. {
  466. free(u);
  467. return;
  468. }
  469. FuriString *enemy_data = furi_string_alloc();
  470. furi_string_printf(
  471. enemy_data,
  472. "{\"enemy_data\":[{\"id\":\"sword\",\"is_user\":\"true\",\"username\":\"%s\","
  473. "\"index\":0,\"start_position\":{\"x\":350,\"y\":210},\"end_position\":{\"x\":350,\"y\":210},"
  474. "\"move_timer\":1,\"speed\":1,\"attack_timer\":\"0.1\",\"strength\":\"100\",\"health\":%f}]}",
  475. u,
  476. (double)atoi(h) // h is an int
  477. );
  478. free(h); // free health
  479. enemy_spawn(game_context->levels[game_context->current_level], manager, enemy_data); // add the enemy to the game context
  480. FURI_LOG_I("Game", "enemy_pvp_position: Added enemy '%s' to the game context", u);
  481. free(u);
  482. furi_string_free(enemy_data); // free enemy data
  483. return;
  484. }
  485. free(u); // free username
  486. // we need the health, elapsed attack timer, direction, xp, and position
  487. char *h = get_json_value("h", game_context->fhttp->last_response);
  488. char *eat = get_json_value("eat", game_context->fhttp->last_response);
  489. char *d = get_json_value("d", game_context->fhttp->last_response);
  490. char *xp = get_json_value("xp", game_context->fhttp->last_response);
  491. char *sp = get_json_value("sp", game_context->fhttp->last_response);
  492. char *x = get_json_value("x", sp);
  493. char *y = get_json_value("y", sp);
  494. if (!h || !eat || !d || !sp || !x || !y || !xp)
  495. {
  496. if (h)
  497. free(h);
  498. if (eat)
  499. free(eat);
  500. if (d)
  501. free(d);
  502. if (sp)
  503. free(sp);
  504. if (x)
  505. free(x);
  506. if (y)
  507. free(y);
  508. if (xp)
  509. free(xp);
  510. return;
  511. }
  512. // set enemy info
  513. enemy->health = (float)atoi(h); // h is an int
  514. if (enemy->health <= 0)
  515. {
  516. enemy->health = 0;
  517. enemy->state = ENTITY_DEAD;
  518. entity_pos_set(self, (Vector){-100, -100});
  519. free(h);
  520. free(eat);
  521. free(d);
  522. free(sp);
  523. free(x);
  524. free(y);
  525. free(xp);
  526. PlayerContext *player_context = entity_context_get(game_context->player);
  527. if (player_context)
  528. {
  529. save_player_context(player_context);
  530. furi_delay_ms(100);
  531. game_manager_game_stop(manager);
  532. }
  533. return;
  534. }
  535. enemy->elapsed_attack_timer = atof_(eat);
  536. switch (atoi(d))
  537. {
  538. case 0:
  539. enemy->direction = ENTITY_LEFT;
  540. break;
  541. case 1:
  542. enemy->direction = ENTITY_RIGHT;
  543. break;
  544. case 2:
  545. enemy->direction = ENTITY_UP;
  546. break;
  547. case 3:
  548. enemy->direction = ENTITY_DOWN;
  549. break;
  550. default:
  551. enemy->direction = ENTITY_RIGHT;
  552. break;
  553. }
  554. enemy->xp = (atoi)(xp); // xp is an int
  555. enemy->level = player_level_iterative_get(enemy->xp);
  556. Vector new_pos = (Vector){
  557. .x = atof_(x),
  558. .y = atof_(y),
  559. };
  560. // set enemy position
  561. entity_pos_set(self, new_pos);
  562. // free the strings
  563. free(h);
  564. free(eat);
  565. free(d);
  566. free(sp);
  567. free(x);
  568. free(y);
  569. free(xp);
  570. }
  571. // Enemy update function
  572. static void enemy_update(Entity *self, GameManager *manager, void *context)
  573. {
  574. if (!self || !context || !manager)
  575. return;
  576. EntityContext *enemy_context = (EntityContext *)context;
  577. if (!enemy_context || enemy_context->state == ENTITY_DEAD)
  578. {
  579. return;
  580. }
  581. GameContext *game_context = game_manager_game_context_get(manager);
  582. if (!game_context)
  583. {
  584. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  585. return;
  586. }
  587. const float delta_time = 1.0f / game_context->fps;
  588. if (game_context->game_mode == GAME_MODE_PVP)
  589. {
  590. // update enemy position
  591. enemy_pvp_position(manager, enemy_context, self);
  592. }
  593. else
  594. {
  595. // update enemy position for pve mode
  596. if (game_context->game_mode == GAME_MODE_PVE)
  597. {
  598. enemy_pvp_position(manager, enemy_context, self);
  599. }
  600. // Increment the elapsed_attack_timer for the enemy
  601. enemy_context->elapsed_attack_timer += delta_time;
  602. switch (enemy_context->state)
  603. {
  604. case ENTITY_IDLE:
  605. // Increment the elapsed_move_timer
  606. enemy_context->elapsed_move_timer += delta_time;
  607. // Check if it's time to move again
  608. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  609. {
  610. // Determine the next state based on the current position
  611. Vector current_pos = entity_pos_get(self);
  612. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
  613. fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
  614. {
  615. enemy_context->state = ENTITY_MOVING_TO_END;
  616. }
  617. else
  618. {
  619. enemy_context->state = ENTITY_MOVING_TO_START;
  620. }
  621. enemy_context->elapsed_move_timer = 0.0f;
  622. }
  623. break;
  624. case ENTITY_MOVING_TO_END:
  625. case ENTITY_MOVING_TO_START:
  626. case ENTITY_ATTACKED:
  627. {
  628. // Get current position
  629. Vector current_pos = entity_pos_get(self);
  630. if (enemy_context->state == ENTITY_ATTACKED)
  631. {
  632. // set direction again
  633. enemy_context->state = enemy_context->direction == ENTITY_LEFT ? ENTITY_MOVING_TO_START : ENTITY_MOVING_TO_END;
  634. }
  635. // Determine the target position based on the current state
  636. Vector target_position = (enemy_context->state == ENTITY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  637. Vector direction_vector = {0, 0};
  638. // Calculate direction towards the target
  639. if (current_pos.x < target_position.x)
  640. {
  641. direction_vector.x = 1.0f;
  642. enemy_context->direction = ENTITY_RIGHT;
  643. }
  644. else if (current_pos.x > target_position.x)
  645. {
  646. direction_vector.x = -1.0f;
  647. enemy_context->direction = ENTITY_LEFT;
  648. }
  649. if (current_pos.y < target_position.y)
  650. {
  651. direction_vector.y = 1.0f;
  652. enemy_context->direction = ENTITY_DOWN;
  653. }
  654. else if (current_pos.y > target_position.y)
  655. {
  656. direction_vector.y = -1.0f;
  657. enemy_context->direction = ENTITY_UP;
  658. }
  659. // Normalize direction vector
  660. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  661. if (length != 0)
  662. {
  663. direction_vector.x /= length;
  664. direction_vector.y /= length;
  665. }
  666. // Update position based on direction and speed
  667. Vector new_pos = current_pos;
  668. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  669. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  670. // Clamp the position to the target to prevent overshooting
  671. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  672. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  673. {
  674. new_pos.x = target_position.x;
  675. }
  676. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  677. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  678. {
  679. new_pos.y = target_position.y;
  680. }
  681. // Set the new position
  682. entity_pos_set(self, new_pos);
  683. // Check if the enemy has reached or surpassed the target_position
  684. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  685. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  686. // If reached the target position on both axes, transition to IDLE
  687. if (reached_x && reached_y)
  688. {
  689. enemy_context->state = ENTITY_IDLE;
  690. enemy_context->elapsed_move_timer = 0.0f;
  691. }
  692. }
  693. break;
  694. default:
  695. break;
  696. }
  697. }
  698. }
  699. // Free function for the entity
  700. static void enemy_free(Entity *self, GameManager *manager, void *context)
  701. {
  702. UNUSED(self);
  703. UNUSED(manager);
  704. UNUSED(context);
  705. if (enemy_context_generic)
  706. {
  707. free(enemy_context_generic);
  708. enemy_context_generic = NULL;
  709. }
  710. }
  711. // Enemy behavior structure
  712. static const EntityDescription _generic_enemy = {
  713. .start = enemy_start,
  714. .stop = enemy_free,
  715. .update = enemy_update,
  716. .render = enemy_render,
  717. .collision = enemy_collision,
  718. .event = NULL,
  719. .context_size = sizeof(EntityContext),
  720. };
  721. // Enemy function to return the entity description
  722. static const EntityDescription *enemy(
  723. GameManager *manager,
  724. const char *id,
  725. int index,
  726. Vector start_position,
  727. Vector end_position,
  728. float move_timer, // Wait duration before moving again
  729. float speed,
  730. float attack_timer,
  731. float strength,
  732. float health,
  733. bool is_user,
  734. FuriString *username)
  735. {
  736. SpriteContext *sprite_context = sprite_context_get(id);
  737. if (!sprite_context)
  738. {
  739. FURI_LOG_E("Game", "Failed to get SpriteContext");
  740. return NULL;
  741. }
  742. // Allocate a new EntityContext with provided parameters
  743. enemy_context_generic = enemy_generic_alloc(
  744. sprite_context->id,
  745. index,
  746. (Vector){sprite_context->width, sprite_context->height},
  747. start_position,
  748. end_position,
  749. move_timer, // Set wait duration
  750. speed,
  751. attack_timer,
  752. strength,
  753. health,
  754. is_user, username);
  755. if (!enemy_context_generic)
  756. {
  757. FURI_LOG_E("Game", "Failed to allocate EntityContext");
  758. return NULL;
  759. }
  760. // assign sprites to the context
  761. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  762. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  763. // Set initial direction based on start and end positions
  764. if (start_position.x < end_position.x)
  765. {
  766. enemy_context_generic->direction = ENTITY_RIGHT;
  767. }
  768. else
  769. {
  770. enemy_context_generic->direction = ENTITY_LEFT;
  771. }
  772. // Set initial state based on movement
  773. if (start_position.x != end_position.x || start_position.y != end_position.y)
  774. {
  775. enemy_context_generic->state = ENTITY_MOVING_TO_END;
  776. }
  777. else
  778. {
  779. enemy_context_generic->state = ENTITY_IDLE;
  780. }
  781. free(sprite_context);
  782. return &_generic_enemy;
  783. }
  784. void enemy_spawn(Level *level, GameManager *manager, FuriString *json)
  785. {
  786. if (!level || !manager || !json)
  787. {
  788. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  789. return;
  790. }
  791. FuriString *id = get_json_value_furi("id", json);
  792. FuriString *_index = get_json_value_furi("index", json);
  793. //
  794. FuriString *start_position = get_json_value_furi("start_position", json);
  795. FuriString *start_position_x = get_json_value_furi("x", start_position);
  796. FuriString *start_position_y = get_json_value_furi("y", start_position);
  797. //
  798. FuriString *end_position = get_json_value_furi("end_position", json);
  799. FuriString *end_position_x = get_json_value_furi("x", end_position);
  800. FuriString *end_position_y = get_json_value_furi("y", end_position);
  801. //
  802. FuriString *move_timer = get_json_value_furi("move_timer", json);
  803. FuriString *speed = get_json_value_furi("speed", json);
  804. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  805. FuriString *strength = get_json_value_furi("strength", json);
  806. FuriString *health = get_json_value_furi("health", json);
  807. //
  808. 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)
  809. {
  810. FURI_LOG_E("Game", "Failed to parse JSON values");
  811. return;
  812. }
  813. FuriString *is_user = get_json_value_furi("is_user", json);
  814. bool is_user_value = false;
  815. if (is_user)
  816. {
  817. is_user_value = strstr(furi_string_get_cstr(is_user), "true") != NULL;
  818. }
  819. FuriString *username = get_json_value_furi("username", json);
  820. // no need to check for username, it is optional
  821. GameContext *game_context = game_manager_game_context_get(manager);
  822. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  823. {
  824. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  825. manager,
  826. furi_string_get_cstr(id),
  827. atoi(furi_string_get_cstr(_index)),
  828. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  829. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  830. atof_furi(move_timer),
  831. atof_furi(speed),
  832. atof_furi(attack_timer),
  833. atof_furi(strength),
  834. atof_furi(health),
  835. is_user_value, username));
  836. game_context->enemy_count++;
  837. }
  838. furi_string_free(id);
  839. furi_string_free(_index);
  840. furi_string_free(start_position);
  841. furi_string_free(start_position_x);
  842. furi_string_free(start_position_y);
  843. furi_string_free(end_position);
  844. furi_string_free(end_position_x);
  845. furi_string_free(end_position_y);
  846. furi_string_free(move_timer);
  847. furi_string_free(speed);
  848. furi_string_free(attack_timer);
  849. furi_string_free(strength);
  850. furi_string_free(health);
  851. if (is_user)
  852. {
  853. furi_string_free(is_user);
  854. }
  855. if (username)
  856. {
  857. furi_string_free(username);
  858. }
  859. }