enemy.c 13 KB

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