enemy.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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)
  423. {
  424. FURI_LOG_E("Game", "enemy_pvp_position: Failed to get GameContext");
  425. return;
  426. }
  427. if (!game_context->ws_info || furi_string_size(game_context->ws_info) == 0)
  428. {
  429. return;
  430. }
  431. // parse the response and set the enemy position
  432. /* expected response:
  433. {
  434. "u": "JBlanked",
  435. "xp": 37743,
  436. "h": 207,
  437. "ehr": 0.7,
  438. "eat": 127.5,
  439. "d": 2,
  440. "s": 1,
  441. "sp": {
  442. "x": 381.0,
  443. "y": 192.0
  444. }
  445. }
  446. */
  447. // match username
  448. FuriString *u = get_json_value_furi("u", game_context->ws_info);
  449. if (!u)
  450. {
  451. FURI_LOG_E("Game", "enemy_pvp_position: Failed to get username");
  452. return;
  453. }
  454. // check if the username matches
  455. const char *u_str = furi_string_get_cstr(u);
  456. furi_string_free(u);
  457. if (!is_str(u_str, enemy->username))
  458. {
  459. if (strlen(u_str) == 0 || enemy_is_game_enemy(manager, u_str))
  460. {
  461. return;
  462. }
  463. PlayerContext *player_context = entity_context_get(game_context->player);
  464. if (!player_context)
  465. return;
  466. if (is_str(player_context->username, u_str))
  467. {
  468. return;
  469. }
  470. FuriString *h = get_json_value_furi("h", game_context->ws_info);
  471. if (!h)
  472. {
  473. return;
  474. }
  475. FuriString *enemy_data = furi_string_alloc();
  476. furi_string_printf(
  477. enemy_data,
  478. "{\"enemy_data\":[{\"id\":\"sword\",\"is_user\":\"true\",\"username\":\"%s\","
  479. "\"index\":0,\"start_position\":{\"x\":350,\"y\":210},\"end_position\":{\"x\":350,\"y\":210},"
  480. "\"move_timer\":1,\"speed\":1,\"attack_timer\":\"0.1\",\"strength\":\"100\",\"health\":%f}]}",
  481. u_str,
  482. (double)atoi(furi_string_get_cstr(h)) // h is an int
  483. );
  484. furi_string_free(h); // free health
  485. enemy_spawn(game_context->levels[game_context->current_level], manager, enemy_data); // add the enemy to the game context
  486. furi_string_free(enemy_data); // free enemy data
  487. return;
  488. }
  489. // we need the health, elapsed attack timer, direction, xp, and position
  490. FuriString *h = get_json_value_furi("h", game_context->ws_info);
  491. FuriString *eat = get_json_value_furi("eat", game_context->ws_info);
  492. FuriString *d = get_json_value_furi("d", game_context->ws_info);
  493. FuriString *xp = get_json_value_furi("xp", game_context->ws_info);
  494. FuriString *sp = get_json_value_furi("sp", game_context->ws_info);
  495. FuriString *x = get_json_value_furi("x", sp);
  496. FuriString *y = get_json_value_furi("y", sp);
  497. if (!h || !eat || !d || !sp || !x || !y || !xp)
  498. {
  499. if (h)
  500. furi_string_free(h);
  501. if (eat)
  502. furi_string_free(eat);
  503. if (d)
  504. furi_string_free(d);
  505. if (sp)
  506. furi_string_free(sp);
  507. if (x)
  508. furi_string_free(x);
  509. if (y)
  510. furi_string_free(y);
  511. if (xp)
  512. furi_string_free(xp);
  513. return;
  514. }
  515. // set enemy info
  516. enemy->health = (float)atoi(furi_string_get_cstr(h)); // h is an int
  517. if (enemy->health <= 0)
  518. {
  519. enemy->health = 0;
  520. enemy->state = ENTITY_DEAD;
  521. entity_pos_set(self, (Vector){-100, -100});
  522. furi_string_free(h);
  523. furi_string_free(eat);
  524. furi_string_free(d);
  525. furi_string_free(sp);
  526. furi_string_free(x);
  527. furi_string_free(y);
  528. furi_string_free(xp);
  529. PlayerContext *player_context = entity_context_get(game_context->player);
  530. if (player_context)
  531. {
  532. save_player_context(player_context);
  533. furi_delay_ms(100);
  534. game_manager_game_stop(manager);
  535. }
  536. return;
  537. }
  538. enemy->elapsed_attack_timer = atof_furi(eat);
  539. switch (atoi(furi_string_get_cstr(d)))
  540. {
  541. case 0:
  542. enemy->direction = ENTITY_LEFT;
  543. break;
  544. case 1:
  545. enemy->direction = ENTITY_RIGHT;
  546. break;
  547. case 2:
  548. enemy->direction = ENTITY_UP;
  549. break;
  550. case 3:
  551. enemy->direction = ENTITY_DOWN;
  552. break;
  553. default:
  554. enemy->direction = ENTITY_RIGHT;
  555. break;
  556. }
  557. enemy->xp = (atoi)(furi_string_get_cstr(xp)); // xp is an int
  558. enemy->level = player_level_iterative_get(enemy->xp);
  559. Vector new_pos = (Vector){
  560. .x = atof_furi(x),
  561. .y = atof_furi(y),
  562. };
  563. // set enemy position
  564. entity_pos_set(self, new_pos);
  565. // free the strings
  566. furi_string_free(h);
  567. furi_string_free(eat);
  568. furi_string_free(d);
  569. furi_string_free(sp);
  570. furi_string_free(x);
  571. furi_string_free(y);
  572. furi_string_free(xp);
  573. }
  574. // Enemy update function
  575. static void enemy_update(Entity *self, GameManager *manager, void *context)
  576. {
  577. if (!self || !context || !manager)
  578. return;
  579. EntityContext *enemy_context = (EntityContext *)context;
  580. if (!enemy_context || enemy_context->state == ENTITY_DEAD)
  581. {
  582. return;
  583. }
  584. GameContext *game_context = game_manager_game_context_get(manager);
  585. if (!game_context)
  586. {
  587. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  588. return;
  589. }
  590. const float delta_time = 1.0f / game_context->fps;
  591. if (game_context->game_mode == GAME_MODE_PVP)
  592. {
  593. // update enemy position
  594. enemy_pvp_position(manager, enemy_context, self);
  595. }
  596. else
  597. {
  598. // update enemy position for pve mode
  599. if (game_context->game_mode == GAME_MODE_PVE)
  600. {
  601. enemy_pvp_position(manager, enemy_context, self);
  602. }
  603. // Increment the elapsed_attack_timer for the enemy
  604. enemy_context->elapsed_attack_timer += delta_time;
  605. switch (enemy_context->state)
  606. {
  607. case ENTITY_IDLE:
  608. // Increment the elapsed_move_timer
  609. enemy_context->elapsed_move_timer += delta_time;
  610. // Check if it's time to move again
  611. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  612. {
  613. // Determine the next state based on the current position
  614. Vector current_pos = entity_pos_get(self);
  615. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)1.0 &&
  616. fabs(current_pos.y - enemy_context->start_position.y) < (double)1.0)
  617. {
  618. enemy_context->state = ENTITY_MOVING_TO_END;
  619. }
  620. else
  621. {
  622. enemy_context->state = ENTITY_MOVING_TO_START;
  623. }
  624. enemy_context->elapsed_move_timer = 0.0f;
  625. }
  626. break;
  627. case ENTITY_MOVING_TO_END:
  628. case ENTITY_MOVING_TO_START:
  629. case ENTITY_ATTACKED:
  630. {
  631. // Get current position
  632. Vector current_pos = entity_pos_get(self);
  633. if (enemy_context->state == ENTITY_ATTACKED)
  634. {
  635. // set direction again
  636. enemy_context->state = enemy_context->direction == ENTITY_LEFT ? ENTITY_MOVING_TO_START : ENTITY_MOVING_TO_END;
  637. }
  638. // Determine the target position based on the current state
  639. Vector target_position = (enemy_context->state == ENTITY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  640. Vector direction_vector = {0, 0};
  641. // Calculate direction towards the target
  642. if (current_pos.x < target_position.x)
  643. {
  644. direction_vector.x = 1.0f;
  645. enemy_context->direction = ENTITY_RIGHT;
  646. }
  647. else if (current_pos.x > target_position.x)
  648. {
  649. direction_vector.x = -1.0f;
  650. enemy_context->direction = ENTITY_LEFT;
  651. }
  652. if (current_pos.y < target_position.y)
  653. {
  654. direction_vector.y = 1.0f;
  655. enemy_context->direction = ENTITY_DOWN;
  656. }
  657. else if (current_pos.y > target_position.y)
  658. {
  659. direction_vector.y = -1.0f;
  660. enemy_context->direction = ENTITY_UP;
  661. }
  662. // Normalize direction vector
  663. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  664. if (length != 0)
  665. {
  666. direction_vector.x /= length;
  667. direction_vector.y /= length;
  668. }
  669. // Update position based on direction and speed
  670. Vector new_pos = current_pos;
  671. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  672. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  673. // Clamp the position to the target to prevent overshooting
  674. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  675. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  676. {
  677. new_pos.x = target_position.x;
  678. }
  679. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  680. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  681. {
  682. new_pos.y = target_position.y;
  683. }
  684. // Set the new position
  685. entity_pos_set(self, new_pos);
  686. // Check if the enemy has reached or surpassed the target_position
  687. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  688. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  689. // If reached the target position on both axes, transition to IDLE
  690. if (reached_x && reached_y)
  691. {
  692. enemy_context->state = ENTITY_IDLE;
  693. enemy_context->elapsed_move_timer = 0.0f;
  694. }
  695. }
  696. break;
  697. default:
  698. break;
  699. }
  700. }
  701. }
  702. // Free function for the entity
  703. static void enemy_free(Entity *self, GameManager *manager, void *context)
  704. {
  705. UNUSED(self);
  706. UNUSED(manager);
  707. UNUSED(context);
  708. if (enemy_context_generic)
  709. {
  710. free(enemy_context_generic);
  711. enemy_context_generic = NULL;
  712. }
  713. }
  714. // Enemy behavior structure
  715. static const EntityDescription _generic_enemy = {
  716. .start = enemy_start,
  717. .stop = enemy_free,
  718. .update = enemy_update,
  719. .render = enemy_render,
  720. .collision = enemy_collision,
  721. .event = NULL,
  722. .context_size = sizeof(EntityContext),
  723. };
  724. // Enemy function to return the entity description
  725. static const EntityDescription *enemy(
  726. GameManager *manager,
  727. const char *id,
  728. int index,
  729. Vector start_position,
  730. Vector end_position,
  731. float move_timer, // Wait duration before moving again
  732. float speed,
  733. float attack_timer,
  734. float strength,
  735. float health,
  736. bool is_user,
  737. FuriString *username)
  738. {
  739. SpriteContext *sprite_context = sprite_context_get(id);
  740. if (!sprite_context)
  741. {
  742. FURI_LOG_E("Game", "Failed to get SpriteContext");
  743. return NULL;
  744. }
  745. // Allocate a new EntityContext with provided parameters
  746. enemy_context_generic = enemy_generic_alloc(
  747. sprite_context->id,
  748. index,
  749. (Vector){sprite_context->width, sprite_context->height},
  750. start_position,
  751. end_position,
  752. move_timer, // Set wait duration
  753. speed,
  754. attack_timer,
  755. strength,
  756. health,
  757. is_user, username);
  758. if (!enemy_context_generic)
  759. {
  760. FURI_LOG_E("Game", "Failed to allocate EntityContext");
  761. return NULL;
  762. }
  763. // assign sprites to the context
  764. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  765. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  766. // Set initial direction based on start and end positions
  767. if (start_position.x < end_position.x)
  768. {
  769. enemy_context_generic->direction = ENTITY_RIGHT;
  770. }
  771. else
  772. {
  773. enemy_context_generic->direction = ENTITY_LEFT;
  774. }
  775. // Set initial state based on movement
  776. if (start_position.x != end_position.x || start_position.y != end_position.y)
  777. {
  778. enemy_context_generic->state = ENTITY_MOVING_TO_END;
  779. }
  780. else
  781. {
  782. enemy_context_generic->state = ENTITY_IDLE;
  783. }
  784. free(sprite_context);
  785. return &_generic_enemy;
  786. }
  787. void enemy_spawn(Level *level, GameManager *manager, FuriString *json)
  788. {
  789. if (!level || !manager || !json)
  790. {
  791. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  792. return;
  793. }
  794. FuriString *id = get_json_value_furi("id", json);
  795. FuriString *_index = get_json_value_furi("index", json);
  796. //
  797. FuriString *start_position = get_json_value_furi("start_position", json);
  798. FuriString *start_position_x = get_json_value_furi("x", start_position);
  799. FuriString *start_position_y = get_json_value_furi("y", start_position);
  800. //
  801. FuriString *end_position = get_json_value_furi("end_position", json);
  802. FuriString *end_position_x = get_json_value_furi("x", end_position);
  803. FuriString *end_position_y = get_json_value_furi("y", end_position);
  804. //
  805. FuriString *move_timer = get_json_value_furi("move_timer", json);
  806. FuriString *speed = get_json_value_furi("speed", json);
  807. FuriString *attack_timer = get_json_value_furi("attack_timer", json);
  808. FuriString *strength = get_json_value_furi("strength", json);
  809. FuriString *health = get_json_value_furi("health", json);
  810. //
  811. 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)
  812. {
  813. FURI_LOG_E("Game", "Failed to parse JSON values");
  814. return;
  815. }
  816. FuriString *is_user = get_json_value_furi("is_user", json);
  817. bool is_user_value = false;
  818. if (is_user)
  819. {
  820. is_user_value = strstr(furi_string_get_cstr(is_user), "true") != NULL;
  821. }
  822. FuriString *username = get_json_value_furi("username", json);
  823. // no need to check for username, it is optional
  824. GameContext *game_context = game_manager_game_context_get(manager);
  825. if (game_context && game_context->enemy_count < MAX_ENEMIES && !game_context->enemies[game_context->enemy_count])
  826. {
  827. game_context->enemies[game_context->enemy_count] = level_add_entity(level, enemy(
  828. manager,
  829. furi_string_get_cstr(id),
  830. atoi(furi_string_get_cstr(_index)),
  831. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  832. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  833. atof_furi(move_timer),
  834. atof_furi(speed),
  835. atof_furi(attack_timer),
  836. atof_furi(strength),
  837. atof_furi(health),
  838. is_user_value, username));
  839. game_context->enemy_count++;
  840. }
  841. furi_string_free(id);
  842. furi_string_free(_index);
  843. furi_string_free(start_position);
  844. furi_string_free(start_position_x);
  845. furi_string_free(start_position_y);
  846. furi_string_free(end_position);
  847. furi_string_free(end_position_x);
  848. furi_string_free(end_position_y);
  849. furi_string_free(move_timer);
  850. furi_string_free(speed);
  851. furi_string_free(attack_timer);
  852. furi_string_free(strength);
  853. furi_string_free(health);
  854. if (is_user)
  855. {
  856. furi_string_free(is_user);
  857. }
  858. if (username)
  859. {
  860. furi_string_free(username);
  861. }
  862. }