enemy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. }
  129. // Enemy collision function
  130. static void enemy_collision(Entity *self, Entity *other, GameManager *manager, void *context)
  131. {
  132. if (!self || !other || !context)
  133. {
  134. FURI_LOG_E("Game", "Enemy collision: Invalid parameters");
  135. return;
  136. }
  137. // Check if the enemy collided with the player
  138. if (entity_description_get(other) == &player_desc)
  139. {
  140. // Retrieve enemy context
  141. EnemyContext *enemy_context = (EnemyContext *)context;
  142. GameContext *game_context = game_manager_game_context_get(manager);
  143. if (!enemy_context)
  144. {
  145. FURI_LOG_E("Game", "Enemy collision: EnemyContext is NULL");
  146. return;
  147. }
  148. if (!game_context)
  149. {
  150. FURI_LOG_E("Game", "Enemy collision: GameContext is NULL");
  151. return;
  152. }
  153. // Get positions of the enemy and the player
  154. Vector enemy_pos = entity_pos_get(self);
  155. Vector player_pos = entity_pos_get(other);
  156. // Determine if the enemy is facing the player or player is facing the enemy
  157. bool enemy_is_facing_player = false;
  158. bool player_is_facing_enemy = false;
  159. // Determine if the enemy is facing the player
  160. if ((enemy_context->direction == ENEMY_LEFT && player_pos.x < enemy_pos.x) ||
  161. (enemy_context->direction == ENEMY_RIGHT && player_pos.x > enemy_pos.x) ||
  162. (enemy_context->direction == ENEMY_UP && player_pos.y < enemy_pos.y) ||
  163. (enemy_context->direction == ENEMY_DOWN && player_pos.y > enemy_pos.y))
  164. {
  165. enemy_is_facing_player = true;
  166. }
  167. // Determine if the player is facing the enemy
  168. if ((game_context->player_context->direction == PLAYER_LEFT && enemy_pos.x < player_pos.x) ||
  169. (game_context->player_context->direction == PLAYER_RIGHT && enemy_pos.x > player_pos.x) ||
  170. (game_context->player_context->direction == PLAYER_UP && enemy_pos.y < player_pos.y) ||
  171. (game_context->player_context->direction == PLAYER_DOWN && enemy_pos.y > player_pos.y))
  172. {
  173. player_is_facing_enemy = true;
  174. }
  175. // Handle Player Attacking Enemy
  176. if (player_is_facing_enemy && game_context->user_input == GameKeyOk)
  177. {
  178. if (game_context->player_context->elapsed_attack_timer >= game_context->player_context->attack_timer)
  179. {
  180. FURI_LOG_I("Game", "Player attacked enemy '%s'!", enemy_context->id);
  181. // Reset player's elapsed attack timer
  182. game_context->player_context->elapsed_attack_timer = 0.0f;
  183. // Increase XP by the enemy's strength
  184. game_context->player_context->xp += enemy_context->strength;
  185. // Decrease enemy health by player strength
  186. enemy_context->health -= game_context->player_context->strength;
  187. if (enemy_context->health <= 0)
  188. {
  189. FURI_LOG_I("Game", "Enemy '%s' is dead.. resetting enemy position and health", enemy_context->id);
  190. enemy_context->state = ENEMY_DEAD;
  191. // Reset enemy position and health
  192. entity_pos_set(self, enemy_context->start_position);
  193. enemy_context->health = 100;
  194. }
  195. else
  196. {
  197. FURI_LOG_I("Game", "Enemy '%s' took %f damage from player", enemy_context->id, (double)game_context->player_context->strength);
  198. enemy_context->state = ENEMY_ATTACKED;
  199. }
  200. }
  201. else
  202. {
  203. 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));
  204. }
  205. }
  206. // Handle Enemy Attacking Player
  207. if (enemy_is_facing_player)
  208. {
  209. if (enemy_context->elapsed_attack_timer >= enemy_context->attack_timer)
  210. {
  211. FURI_LOG_I("Game", "Enemy '%s' attacked the player!", enemy_context->id);
  212. // Reset enemy's elapsed attack timer
  213. enemy_context->elapsed_attack_timer = 0.0f;
  214. // Decrease player health by enemy strength
  215. game_context->player_context->health -= enemy_context->strength;
  216. if (game_context->player_context->health <= 0)
  217. {
  218. FURI_LOG_I("Game", "Player is dead.. resetting player position and health");
  219. game_context->player_context->state = PLAYER_DEAD;
  220. // Reset player position and health
  221. entity_pos_set(other, game_context->player_context->start_position);
  222. game_context->player_context->health = 100;
  223. }
  224. else
  225. {
  226. FURI_LOG_I("Game", "Player took %f damage from enemy '%s'", (double)enemy_context->strength, enemy_context->id);
  227. game_context->player_context->state = PLAYER_ATTACKED;
  228. }
  229. }
  230. else
  231. {
  232. 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));
  233. }
  234. }
  235. }
  236. }
  237. // Enemy update function
  238. static void enemy_update(Entity *self, GameManager *manager, void *context)
  239. {
  240. if (!self || !context || !manager)
  241. return;
  242. EnemyContext *enemy_context = (EnemyContext *)context;
  243. GameContext *game_context = game_manager_game_context_get(manager);
  244. if (!game_context)
  245. {
  246. FURI_LOG_E("Game", "Enemy update: Failed to get GameContext");
  247. return;
  248. }
  249. float delta_time = 1.0f / game_context->fps;
  250. // Increment the elapsed_attack_timer for the enemy
  251. enemy_context->elapsed_attack_timer += delta_time;
  252. switch (enemy_context->state)
  253. {
  254. case ENEMY_IDLE:
  255. // Increment the elapsed_move_timer
  256. enemy_context->elapsed_move_timer += delta_time;
  257. // Check if it's time to move again
  258. if (enemy_context->elapsed_move_timer >= enemy_context->move_timer)
  259. {
  260. // Determine the next state based on the current position
  261. Vector current_pos = entity_pos_get(self);
  262. if (fabs(current_pos.x - enemy_context->start_position.x) < (double)EPSILON &&
  263. fabs(current_pos.y - enemy_context->start_position.y) < (double)EPSILON)
  264. {
  265. enemy_context->state = ENEMY_MOVING_TO_END;
  266. }
  267. else
  268. {
  269. enemy_context->state = ENEMY_MOVING_TO_START;
  270. }
  271. enemy_context->elapsed_move_timer = 0.0f;
  272. }
  273. break;
  274. case ENEMY_MOVING_TO_END:
  275. case ENEMY_MOVING_TO_START:
  276. {
  277. // Determine the target position based on the current state
  278. Vector target_position = (enemy_context->state == ENEMY_MOVING_TO_END) ? enemy_context->end_position : enemy_context->start_position;
  279. // Get current position
  280. Vector current_pos = entity_pos_get(self);
  281. Vector direction_vector = {0, 0};
  282. // Calculate direction towards the target
  283. if (current_pos.x < target_position.x)
  284. {
  285. direction_vector.x = 1.0f;
  286. enemy_context->direction = ENEMY_RIGHT;
  287. }
  288. else if (current_pos.x > target_position.x)
  289. {
  290. direction_vector.x = -1.0f;
  291. enemy_context->direction = ENEMY_LEFT;
  292. }
  293. if (current_pos.y < target_position.y)
  294. {
  295. direction_vector.y = 1.0f;
  296. enemy_context->direction = ENEMY_DOWN;
  297. }
  298. else if (current_pos.y > target_position.y)
  299. {
  300. direction_vector.y = -1.0f;
  301. enemy_context->direction = ENEMY_UP;
  302. }
  303. // Normalize direction vector
  304. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  305. if (length != 0)
  306. {
  307. direction_vector.x /= length;
  308. direction_vector.y /= length;
  309. }
  310. // Update position based on direction and speed
  311. Vector new_pos = current_pos;
  312. new_pos.x += direction_vector.x * enemy_context->speed * delta_time;
  313. new_pos.y += direction_vector.y * enemy_context->speed * delta_time;
  314. // Clamp the position to the target to prevent overshooting
  315. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  316. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  317. {
  318. new_pos.x = target_position.x;
  319. }
  320. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  321. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  322. {
  323. new_pos.y = target_position.y;
  324. }
  325. entity_pos_set(self, new_pos);
  326. // Check if the enemy has reached or surpassed the target_position
  327. bool reached_x = fabs(new_pos.x - target_position.x) < (double)EPSILON;
  328. bool reached_y = fabs(new_pos.y - target_position.y) < (double)EPSILON;
  329. // If reached the target position on both axes, transition to IDLE
  330. if (reached_x && reached_y)
  331. {
  332. enemy_context->state = ENEMY_IDLE;
  333. enemy_context->elapsed_move_timer = 0.0f;
  334. }
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. }
  341. // Free function for the entity
  342. static void enemy_free(Entity *self, GameManager *manager, void *context)
  343. {
  344. UNUSED(self);
  345. UNUSED(manager);
  346. enemy_generic_free(context);
  347. }
  348. // Enemy behavior structure
  349. static const EntityDescription _generic_enemy = {
  350. .start = enemy_start,
  351. .stop = enemy_free,
  352. .update = enemy_update,
  353. .render = enemy_render,
  354. .collision = enemy_collision,
  355. .event = NULL,
  356. .context_size = sizeof(EnemyContext),
  357. };
  358. // Enemy function to return the entity description
  359. const EntityDescription *enemy(
  360. GameManager *manager,
  361. const char *id,
  362. int index,
  363. Vector size,
  364. Vector start_position,
  365. Vector end_position,
  366. float move_timer, // Wait duration before moving again
  367. float speed,
  368. float attack_timer,
  369. float strength,
  370. float health)
  371. {
  372. // Allocate a new EnemyContext with provided parameters
  373. enemy_context_generic = enemy_generic_alloc(
  374. id,
  375. index,
  376. size,
  377. start_position,
  378. end_position,
  379. move_timer, // Set wait duration
  380. speed,
  381. attack_timer,
  382. strength,
  383. health);
  384. if (!enemy_context_generic)
  385. {
  386. FURI_LOG_E("Game", "Failed to allocate EnemyContext");
  387. return NULL;
  388. }
  389. char right_edited[64];
  390. char left_edited[64];
  391. snprintf(right_edited, sizeof(right_edited), "%s_right.fxbm", id);
  392. snprintf(left_edited, sizeof(left_edited), "%s_left.fxbm", id);
  393. enemy_context_generic->sprite_right = game_manager_sprite_load(manager, right_edited);
  394. enemy_context_generic->sprite_left = game_manager_sprite_load(manager, left_edited);
  395. // Set initial direction based on start and end positions
  396. if (start_position.x < end_position.x)
  397. {
  398. enemy_context_generic->direction = ENEMY_RIGHT;
  399. }
  400. else
  401. {
  402. enemy_context_generic->direction = ENEMY_LEFT;
  403. }
  404. // Set initial state based on movement
  405. if (start_position.x != end_position.x || start_position.y != end_position.y)
  406. {
  407. enemy_context_generic->state = ENEMY_MOVING_TO_END;
  408. }
  409. else
  410. {
  411. enemy_context_generic->state = ENEMY_IDLE;
  412. }
  413. return &_generic_enemy;
  414. }