enemy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // enemy.c
  2. #include <game/enemy.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6. #define EPSILON 0.1f
  7. static EnemyContext *enemy_context_generic;
  8. // Allocation function
  9. static EnemyContext *enemy_generic_alloc(
  10. const char *id,
  11. int index,
  12. Vector size,
  13. Vector start_position,
  14. Vector end_position,
  15. float move_timer, // Wait duration before moving again
  16. float speed,
  17. float attack_timer,
  18. float strength,
  19. float health)
  20. {
  21. if (!enemy_context_generic)
  22. {
  23. enemy_context_generic = malloc(sizeof(EnemyContext));
  24. }
  25. if (!enemy_context_generic)
  26. {
  27. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  28. return NULL;
  29. }
  30. snprintf(enemy_context_generic->id, sizeof(enemy_context_generic->id), "%s", id);
  31. enemy_context_generic->index = index;
  32. enemy_context_generic->size = size;
  33. enemy_context_generic->start_position = start_position;
  34. enemy_context_generic->end_position = end_position;
  35. enemy_context_generic->move_timer = move_timer; // Set wait duration
  36. enemy_context_generic->elapsed_move_timer = 0.0f; // Initialize elapsed timer
  37. enemy_context_generic->speed = speed;
  38. enemy_context_generic->attack_timer = attack_timer;
  39. enemy_context_generic->strength = strength;
  40. enemy_context_generic->health = health;
  41. // Initialize other fields as needed
  42. enemy_context_generic->sprite_right = NULL; // Assign appropriate sprite
  43. enemy_context_generic->sprite_left = NULL; // Assign appropriate sprite
  44. enemy_context_generic->direction = ENEMY_RIGHT; // Default direction
  45. enemy_context_generic->state = ENEMY_MOVING_TO_END; // Start in IDLE state
  46. // Set radius based on size, for example, average of size.x and size.y divided by 2
  47. enemy_context_generic->radius = (size.x + size.y) / 4.0f;
  48. return enemy_context_generic;
  49. }
  50. // Free function
  51. static void enemy_generic_free(void *context)
  52. {
  53. if (!context)
  54. {
  55. FURI_LOG_E("Game", "Enemy generic free: Invalid context");
  56. return;
  57. }
  58. free(context);
  59. context = NULL;
  60. if (enemy_context_generic)
  61. {
  62. free(enemy_context_generic);
  63. enemy_context_generic = NULL;
  64. }
  65. }
  66. // Enemy start function
  67. static void enemy_start(Entity *self, GameManager *manager, void *context)
  68. {
  69. UNUSED(manager);
  70. if (!self || !context)
  71. {
  72. FURI_LOG_E("Game", "Enemy start: Invalid parameters");
  73. return;
  74. }
  75. if (!enemy_context_generic)
  76. {
  77. FURI_LOG_E("Game", "Enemy start: Enemy context not set");
  78. return;
  79. }
  80. EnemyContext *enemy_context = (EnemyContext *)context;
  81. // Copy fields from generic context
  82. snprintf(enemy_context->id, sizeof(enemy_context->id), "%s", enemy_context_generic->id);
  83. enemy_context->index = enemy_context_generic->index;
  84. enemy_context->size = enemy_context_generic->size;
  85. enemy_context->start_position = enemy_context_generic->start_position;
  86. enemy_context->end_position = enemy_context_generic->end_position;
  87. enemy_context->move_timer = enemy_context_generic->move_timer;
  88. enemy_context->elapsed_move_timer = enemy_context_generic->elapsed_move_timer;
  89. enemy_context->speed = enemy_context_generic->speed;
  90. enemy_context->attack_timer = enemy_context_generic->attack_timer;
  91. enemy_context->strength = enemy_context_generic->strength;
  92. enemy_context->health = enemy_context_generic->health;
  93. enemy_context->sprite_right = enemy_context_generic->sprite_right;
  94. enemy_context->sprite_left = enemy_context_generic->sprite_left;
  95. enemy_context->direction = enemy_context_generic->direction;
  96. enemy_context->state = enemy_context_generic->state;
  97. enemy_context->radius = enemy_context_generic->radius;
  98. // Set enemy's initial position based on start_position
  99. entity_pos_set(self, enemy_context->start_position);
  100. // Add collision circle based on the enemy's radius
  101. entity_collider_add_circle(self, enemy_context->radius);
  102. }
  103. // Enemy render function
  104. static void enemy_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  105. {
  106. UNUSED(manager);
  107. if (!self || !context || !canvas)
  108. return;
  109. EnemyContext *enemy_context = (EnemyContext *)context;
  110. // Get the position of the enemy
  111. Vector pos = entity_pos_get(self);
  112. // Choose sprite based on direction
  113. Sprite *current_sprite = NULL;
  114. if (enemy_context->direction == ENEMY_LEFT)
  115. {
  116. current_sprite = enemy_context->sprite_left;
  117. }
  118. else
  119. {
  120. current_sprite = enemy_context->sprite_right;
  121. }
  122. // Draw enemy sprite relative to camera, centered on the enemy's position
  123. canvas_draw_sprite(
  124. canvas,
  125. current_sprite,
  126. pos.x - camera_x - (enemy_context->size.x / 2),
  127. pos.y - camera_y - (enemy_context->size.y / 2));
  128. // Draw user stats (this has to be done for all enemies)
  129. draw_user_stats(canvas, (Vector){0, 7}, manager);
  130. }
  131. // Enemy collision function
  132. static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  133. {
  134. if (!self || !other || !context)
  135. {
  136. FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
  137. return;
  138. }
  139. // Check if the enemy collided with the player
  140. if (entity_description_get(other) == &player_desc)
  141. {
  142. // Retrieve enemy context
  143. EnemyContext *enemy_context = (EnemyContext *)context;
  144. GameContext *game_context = game_manager_game_context_get(manager);
  145. if (!enemy_context)
  146. {
  147. FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
  148. return;
  149. }
  150. if (!game_context)
  151. {
  152. FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
  153. return;
  154. }
  155. // Get positions of the enemy and the player
  156. Vector enemy_pos = entity_pos_get(self);
  157. Vector player_pos = entity_pos_get(other);
  158. // Determine if the enemy is facing the player or player is facing the enemy
  159. bool enemy_is_facing_player = false;
  160. bool player_is_facing_enemy = false;
  161. // Determine if the enemy is facing the player
  162. if ((enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x) ||
  163. (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x) ||
  164. (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y) ||
  165. (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y))
  166. {
  167. enemy_is_facing_player = true;
  168. }
  169. // Determine if the player is facing the enemy
  170. if ((game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x) ||
  171. (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x) ||
  172. (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y) ||
  173. (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y))
  174. {
  175. player_is_facing_enemy = true;
  176. }
  177. // Handle Player Attacking Enemy
  178. if (player_is_facing_enemy && game_context->user_input == GameKeyOk)
  179. {
  180. if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
  181. {
  182. FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
  183. // Reset player's elapsed attack timer
  184. game_context->player_context->elapsed_attack_timer = 0.0f;
  185. enemy_context->elapsed_attack_timer = 0.0f; // Reset enemy's attack timer to block enemy attack
  186. // Increase XP by the enemy's strength
  187. game_context->player_context->xp += enemy_context->strength;
  188. // Increase healthy by 10% of the enemy's strength
  189. game_context->player_context->health += enemy_context->strength * 0.1f;
  190. // Decrease enemy health by player strength
  191. enemy_context->health -= game_context->player_context->strength;
  192. if (enemy_context->health <= 0)
  193. {
  194. FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
  195. enemy_context->state = ENEMY_DEAD;
  196. // Reset enemy position and health
  197. entity_pos_set(self, enemy_context->start_position);
  198. enemy_context->health = 100;
  199. }
  200. else
  201. {
  202. FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
  203. enemy_context->state = ENEMY_ATTACKED;
  204. // Bounce the enemy back by X units opposite their last movement direction
  205. enemy_pos.x -= game_context->player_context->dx * enemy_context->radius;
  206. enemy_pos.y -= game_context->player_context->dy * enemy_context->radius;
  207. entity_pos_set(self, enemy_pos);
  208. // Reset enemy's movement direction to prevent immediate re-collision
  209. game_context->player_context->dx = 0;
  210. game_context->player_context->dy = 0;
  211. }
  212. }
  213. else
  214. {
  215. 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));
  216. }
  217. }
  218. // Handle Enemy Attacking Player
  219. else if (enemy_is_facing_player)
  220. {
  221. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  222. {
  223. FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
  224. // Reset enemy's elapsed attack timer
  225. enemy_context->elapsed_attack_timer = 0.0f;
  226. // Decrease player health by enemy strength
  227. game_context->player_context->health -= enemy_context->strength;
  228. if (game_context->player_context->health <= 0)
  229. {
  230. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  231. game_context->player_context->state = PLAYER_DEAD;
  232. // Reset player position and health
  233. entity_pos_set(other, game_context->player_context->start_position);
  234. game_context->player_context->health = 100;
  235. // subtract player's XP by the enemy's strength
  236. game_context->player_context->xp -= enemy_context->strength;
  237. if ((int)game_context->player_context->xp < 0)
  238. {
  239. game_context->player_context->xp = 0;
  240. }
  241. }
  242. else
  243. {
  244. FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
  245. game_context->player_context->state = PLAYER_ATTACKED;
  246. // Bounce the player back by X units opposite their last movement direction
  247. player_pos.x -= game_context->player_context->dx * enemy_context->radius;
  248. player_pos.y -= game_context->player_context->dy * enemy_context->radius;
  249. entity_pos_set(other, player_pos);
  250. // Reset player's movement direction to prevent immediate re-collision
  251. game_context->player_context->dx = 0;
  252. game_context->player_context->dy = 0;
  253. }
  254. }
  255. else
  256. {
  257. 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));
  258. }
  259. }
  260. else // handle other collisions
  261. {
  262. // bounce player and enemy away from each other
  263. Vector player_pos = entity_pos_get(other);
  264. Vector enemy_pos = entity_pos_get(self);
  265. // Calculate the direction vector from player to enemy
  266. Vector direction_vector = {
  267. enemy_pos.x - player_pos.x,
  268. enemy_pos.y - player_pos.y};
  269. // Normalize the direction vector
  270. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  271. if (length != 0)
  272. {
  273. direction_vector.x /= length;
  274. direction_vector.y /= length;
  275. }
  276. // Move the player and enemy away from each other
  277. player_pos.x -= direction_vector.x * enemy_context->radius;
  278. player_pos.y -= direction_vector.y * enemy_context->radius;
  279. entity_pos_set(other, player_pos);
  280. enemy_pos.x += direction_vector.x * enemy_context->radius;
  281. enemy_pos.y += direction_vector.y * enemy_context->radius;
  282. entity_pos_set(self, enemy_pos);
  283. // Reset player's movement direction to prevent immediate re-collision
  284. game_context->player_context->dx = 0;
  285. game_context->player_context->dy = 0;
  286. }
  287. // Reset enemy's position and state
  288. entity_pos_set(self, enemy_context->start_position);
  289. enemy_context->state = ENEMY_IDLE;
  290. enemy_context->elapsed_move_timer = 0.0f;
  291. if (game_context->player_context->state == PLAYER_DEAD)
  292. {
  293. // Reset player's position and health
  294. entity_pos_set(other, game_context->player_context->start_position);
  295. game_context->player_context->health = 100;
  296. }
  297. }
  298. }
  299. // Enemy update function
  300. static void enemy_update(Entity *self, GameManager *manager, void *context)
  301. {
  302. if (!self || !context || !manager)
  303. return;
  304. EnemyContext *enemy_context = (EnemyContext *)context;
  305. GameContext *game_context = game_manager_game_context_get(manager);
  306. if (!game_context)
  307. {
  308. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  309. return;
  310. }
  311. float delta_time = 1.0f / game_context->fps;
  312. // Increment the elapsed_attack_timer for the enemy
  313. enemy_context->elapsed_attack_timer += delta_time;
  314. switch (enemy_context->state)
  315. {
  316. case ENEMY_IDLE:
  317. // Increment the elapsed_move_timer
  318. enemy_context->elapsed_move_timer += delta_time;
  319. // Check if it's time to move again
  320. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  321. {
  322. // Determine the next state based on the current position
  323. Vector current_pos = entity_pos_get(self);
  324. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)EPSILON &&
  325. fabs(current_pos.y - enemy_context->start_position.y) < (double)EPSILON)
  326. {
  327. enemy_context->state = ENEMY_MOVING_TO_END;
  328. }
  329. else
  330. {
  331. enemy_context->state = ENEMY_MOVING_TO_START;
  332. }
  333. enemy_context->elapsed_move_timer = 0.0f;
  334. }
  335. break;
  336. case ENEMY_MOVING_TO_END:
  337. case ENEMY_MOVING_TO_START:
  338. {
  339. // Determine the target position based on the current state
  340. Vector target_position = (enemy_context->state == ENEMY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  341. // Get current position
  342. Vector current_pos = entity_pos_get(self);
  343. Vector direction_vector = {0, 0};
  344. // Calculate direction towards the target
  345. if (current_pos.x < target_position.x)
  346. {
  347. direction_vector.x = 1.0f;
  348. enemy_context->direction = ENEMY_RIGHT;
  349. }
  350. else if (current_pos.x > target_position.x)
  351. {
  352. direction_vector.x = -1.0f;
  353. enemy_context->direction = ENEMY_LEFT;
  354. }
  355. if (current_pos.y < target_position.y)
  356. {
  357. direction_vector.y = 1.0f;
  358. enemy_context->direction = ENEMY_DOWN;
  359. }
  360. else if (current_pos.y > target_position.y)
  361. {
  362. direction_vector.y = -1.0f;
  363. enemy_context->direction = ENEMY_UP;
  364. }
  365. // Normalize direction vector
  366. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  367. if (length != 0)
  368. {
  369. direction_vector.x /= length;
  370. direction_vector.y /= length;
  371. }
  372. // Update position based on direction and speed
  373. Vector new_pos = current_pos;
  374. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  375. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  376. // Clamp the position to the target to prevent overshooting
  377. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  378. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  379. {
  380. new_pos.x = target_position.x;
  381. }
  382. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  383. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  384. {
  385. new_pos.y = target_position.y;
  386. }
  387. entity_pos_set(self, new_pos);
  388. // Check if the enemy has reached or surpassed the target_position
  389. bool reached_x = fabs(new_pos.x - target_position.x) < (double)EPSILON;
  390. bool reached_y = fabs(new_pos.y - target_position.y) < (double)EPSILON;
  391. // If reached the target position on both axes, transition to IDLE
  392. if (reached_x && reached_y)
  393. {
  394. enemy_context->state = ENEMY_IDLE;
  395. enemy_context->elapsed_move_timer = 0.0f;
  396. }
  397. }
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. // Free function for the entity
  404. static void enemy_free(Entity *self, GameManager *manager, void *context)
  405. {
  406. UNUSED(self);
  407. UNUSED(manager);
  408. enemy_generic_free(context);
  409. }
  410. // Enemy behavior structure
  411. static const EntityDescription _generic_enemy = {
  412. .start = enemy_start,
  413. .stop = enemy_free,
  414. .update = enemy_update,
  415. .render = enemy_render,
  416. .collision = enemy_collision,
  417. .event = NULL,
  418. .context_size = sizeof(EnemyContext),
  419. };
  420. // Enemy function to return the entity description
  421. const EntityDescription *enemy(
  422. GameManager *manager,
  423. const char *id,
  424. int index,
  425. Vector size,
  426. Vector start_position,
  427. Vector end_position,
  428. float move_timer, // Wait duration before moving again
  429. float speed,
  430. float attack_timer,
  431. float strength,
  432. float health)
  433. {
  434. // Allocate a new EnemyContext with provided parameters
  435. enemy_context_generic = enemy_generic_alloc(
  436. id,
  437. index,
  438. size,
  439. start_position,
  440. end_position,
  441. move_timer, // Set wait duration
  442. speed,
  443. attack_timer,
  444. strength,
  445. health);
  446. if (!enemy_context_generic)
  447. {
  448. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  449. return NULL;
  450. }
  451. char right_edited[128];
  452. char left_edited[128];
  453. // Convert the float values to integers and use them in the filename
  454. int size_x_int = (int)size.x;
  455. int size_y_int = (int)size.y;
  456. // Format the strings without the decimal points
  457. snprintf(right_edited, sizeof(right_edited), "player_right_%s_%dx%dpx.fxbm", id, size_x_int, size_y_int);
  458. snprintf(left_edited, sizeof(left_edited), "player_left_%s_%dx%dpx.fxbm", id, size_x_int, size_y_int);
  459. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, right_edited);
  460. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, left_edited);
  461. // Set initial direction based on start and end positions
  462. if (start_position.x < end_position.x)
  463. {
  464. enemy_context_generic->direction = ENEMY_RIGHT;
  465. }
  466. else
  467. {
  468. enemy_context_generic->direction = ENEMY_LEFT;
  469. }
  470. // Set initial state based on movement
  471. if (start_position.x != end_position.x || start_position.y != end_position.y)
  472. {
  473. enemy_context_generic->state = ENEMY_MOVING_TO_END;
  474. }
  475. else
  476. {
  477. enemy_context_generic->state = ENEMY_IDLE;
  478. }
  479. return &_generic_enemy;
  480. }