enemy.c 29 KB

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