enemy.c 13 KB

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