enemy.c 12 KB

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