npc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include <game/npc.h>
  2. static NPCContext *npc_context_generic;
  3. // Allocation function
  4. static NPCContext *npc_generic_alloc(
  5. const char *id,
  6. int index,
  7. Vector size,
  8. Vector start_position,
  9. Vector end_position,
  10. float move_timer, // Wait duration before moving again
  11. float speed)
  12. {
  13. if (!npc_context_generic)
  14. {
  15. npc_context_generic = malloc(sizeof(NPCContext));
  16. }
  17. if (!npc_context_generic)
  18. {
  19. FURI_LOG_E("Game", "Failed to allocate NPCContext");
  20. return NULL;
  21. }
  22. snprintf(npc_context_generic->id, sizeof(npc_context_generic->id), "%s", id);
  23. npc_context_generic->index = index;
  24. npc_context_generic->size = size;
  25. npc_context_generic->start_position = start_position;
  26. npc_context_generic->end_position = end_position;
  27. npc_context_generic->move_timer = move_timer; // Set wait duration
  28. npc_context_generic->elapsed_move_timer = 0.0f; // Initialize elapsed timer
  29. npc_context_generic->speed = speed;
  30. // Initialize other fields as needed
  31. npc_context_generic->sprite_right = NULL; // Assign appropriate sprite
  32. npc_context_generic->sprite_left = NULL; // Assign appropriate sprite
  33. npc_context_generic->direction = ENTITY_RIGHT; // Default direction
  34. npc_context_generic->state = ENTITY_MOVING_TO_END; // Start in IDLE state
  35. // Set radius based on size, for example, average of size.x and size.y divided by 2
  36. npc_context_generic->radius = (size.x + size.y) / 4.0f;
  37. return npc_context_generic;
  38. }
  39. // Free function
  40. static void npc_generic_free(void *context)
  41. {
  42. if (context)
  43. {
  44. free(context);
  45. context = NULL;
  46. }
  47. if (npc_context_generic)
  48. {
  49. free(npc_context_generic);
  50. npc_context_generic = NULL;
  51. }
  52. }
  53. // NPC start function
  54. static void npc_start(Entity *self, GameManager *manager, void *context)
  55. {
  56. UNUSED(manager);
  57. if (!self || !context)
  58. {
  59. FURI_LOG_E("Game", "Enemy start: Invalid parameters");
  60. return;
  61. }
  62. if (!npc_context_generic)
  63. {
  64. FURI_LOG_E("Game", "NPC start: NPC context not set");
  65. return;
  66. }
  67. NPCContext *npc_context = (NPCContext *)context;
  68. // Copy fields from generic context
  69. snprintf(npc_context->id, sizeof(npc_context->id), "%s", npc_context_generic->id);
  70. npc_context->index = npc_context_generic->index;
  71. npc_context->size = npc_context_generic->size;
  72. npc_context->start_position = npc_context_generic->start_position;
  73. npc_context->end_position = npc_context_generic->end_position;
  74. npc_context->move_timer = npc_context_generic->move_timer;
  75. npc_context->elapsed_move_timer = npc_context_generic->elapsed_move_timer;
  76. npc_context->speed = npc_context_generic->speed;
  77. npc_context->sprite_right = npc_context_generic->sprite_right;
  78. npc_context->sprite_left = npc_context_generic->sprite_left;
  79. npc_context->direction = npc_context_generic->direction;
  80. npc_context->state = npc_context_generic->state;
  81. npc_context->radius = npc_context_generic->radius;
  82. // Set NPC's initial position based on start_position
  83. entity_pos_set(self, npc_context->start_position);
  84. // Add collision circle based on the NPC's radius
  85. entity_collider_add_circle(self, npc_context->radius);
  86. }
  87. // NPC render function
  88. static void npc_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  89. {
  90. if (!self || !context || !canvas || !manager)
  91. return;
  92. NPCContext *npc_context = (NPCContext *)context;
  93. // Get the position of the NPC
  94. Vector pos = entity_pos_get(self);
  95. // Choose sprite based on direction
  96. Sprite *current_sprite = NULL;
  97. if (npc_context->direction == ENTITY_LEFT)
  98. {
  99. current_sprite = npc_context->sprite_left;
  100. }
  101. else
  102. {
  103. current_sprite = npc_context->sprite_right;
  104. }
  105. // Draw NPC sprite relative to camera, centered on the NPC's position
  106. canvas_draw_sprite(
  107. canvas,
  108. current_sprite,
  109. pos.x - camera_x - (npc_context->size.x / 2),
  110. pos.y - camera_y - (npc_context->size.y / 2));
  111. }
  112. // skip collision function for now
  113. // NPC update function
  114. static void npc_update(Entity *self, GameManager *manager, void *context)
  115. {
  116. if (!self || !context || !manager)
  117. return;
  118. NPCContext *npc_context = (NPCContext *)context;
  119. if (!npc_context || npc_context->state == ENTITY_DEAD)
  120. {
  121. return;
  122. }
  123. GameContext *game_context = game_manager_game_context_get(manager);
  124. if (!game_context)
  125. {
  126. FURI_LOG_E("Game", "NPC update: Failed to get GameContext");
  127. return;
  128. }
  129. float delta_time = 1.0f / game_context->fps;
  130. switch (npc_context->state)
  131. {
  132. case ENTITY_IDLE:
  133. // Increment the elapsed_move_timer
  134. npc_context->elapsed_move_timer += delta_time;
  135. // Check if it's time to move again
  136. if (npc_context->elapsed_move_timer >= npc_context->move_timer)
  137. {
  138. // Determine the next state based on the current position
  139. Vector current_pos = entity_pos_get(self);
  140. if (fabs(current_pos.x - npc_context->start_position.x) < (double)1.0 &&
  141. fabs(current_pos.y - npc_context->start_position.y) < (double)1.0)
  142. {
  143. npc_context->state = ENTITY_MOVING_TO_END;
  144. }
  145. else
  146. {
  147. npc_context->state = ENTITY_MOVING_TO_START;
  148. }
  149. npc_context->elapsed_move_timer = 0.0f;
  150. }
  151. break;
  152. case ENTITY_MOVING_TO_END:
  153. case ENTITY_MOVING_TO_START:
  154. {
  155. // Determine the target position based on the current state
  156. Vector target_position = (npc_context->state == ENTITY_MOVING_TO_END) ? npc_context->end_position : npc_context->start_position;
  157. // Get current position
  158. Vector current_pos = entity_pos_get(self);
  159. Vector direction_vector = {0, 0};
  160. // Calculate direction towards the target
  161. if (current_pos.x < target_position.x)
  162. {
  163. direction_vector.x = 1.0f;
  164. npc_context->direction = ENTITY_RIGHT;
  165. }
  166. else if (current_pos.x > target_position.x)
  167. {
  168. direction_vector.x = -1.0f;
  169. npc_context->direction = ENTITY_LEFT;
  170. }
  171. if (current_pos.y < target_position.y)
  172. {
  173. direction_vector.y = 1.0f;
  174. npc_context->direction = ENTITY_DOWN;
  175. }
  176. else if (current_pos.y > target_position.y)
  177. {
  178. direction_vector.y = -1.0f;
  179. npc_context->direction = ENTITY_UP;
  180. }
  181. // Normalize direction vector
  182. float length = sqrt(direction_vector.x * direction_vector.x + direction_vector.y * direction_vector.y);
  183. if (length != 0)
  184. {
  185. direction_vector.x /= length;
  186. direction_vector.y /= length;
  187. }
  188. // Update position based on direction and speed
  189. Vector new_pos = current_pos;
  190. new_pos.x += direction_vector.x * npc_context->speed * delta_time;
  191. new_pos.y += direction_vector.y * npc_context->speed * delta_time;
  192. // Clamp the position to the target to prevent overshooting
  193. if ((direction_vector.x > 0.0f && new_pos.x > target_position.x) ||
  194. (direction_vector.x < 0.0f && new_pos.x < target_position.x))
  195. {
  196. new_pos.x = target_position.x;
  197. }
  198. if ((direction_vector.y > 0.0f && new_pos.y > target_position.y) ||
  199. (direction_vector.y < 0.0f && new_pos.y < target_position.y))
  200. {
  201. new_pos.y = target_position.y;
  202. }
  203. entity_pos_set(self, new_pos);
  204. // Check if the nPC has reached or surpassed the target_position
  205. bool reached_x = fabs(new_pos.x - target_position.x) < (double)1.0;
  206. bool reached_y = fabs(new_pos.y - target_position.y) < (double)1.0;
  207. // If reached the target position on both axes, transition to IDLE
  208. if (reached_x && reached_y)
  209. {
  210. npc_context->state = ENTITY_IDLE;
  211. npc_context->elapsed_move_timer = 0.0f;
  212. }
  213. }
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219. // Free function for the entity
  220. static void npc_free(Entity *self, GameManager *manager, void *context)
  221. {
  222. UNUSED(self);
  223. UNUSED(manager);
  224. if (context)
  225. npc_generic_free(context);
  226. }
  227. // NPC Behavior structure
  228. static const EntityDescription _generic_npc = {
  229. .start = npc_start,
  230. .stop = npc_free,
  231. .update = npc_update,
  232. .render = npc_render,
  233. .collision = NULL,
  234. .event = NULL,
  235. .context_size = sizeof(NPCContext),
  236. };
  237. // Spawn function to return the entity description
  238. const EntityDescription *npc(
  239. GameManager *manager,
  240. const char *id,
  241. int index,
  242. Vector start_position,
  243. Vector end_position,
  244. float move_timer, // Wait duration before moving again
  245. float speed)
  246. {
  247. SpriteContext *sprite_context = get_sprite_context(id);
  248. if (!sprite_context)
  249. {
  250. FURI_LOG_E("Game", "Failed to get SpriteContext");
  251. return NULL;
  252. }
  253. // Allocate a new NPCContext with provided parameters
  254. npc_context_generic = npc_generic_alloc(
  255. id,
  256. index,
  257. (Vector){sprite_context->width, sprite_context->height},
  258. start_position,
  259. end_position,
  260. move_timer,
  261. speed);
  262. if (!npc_context_generic)
  263. {
  264. FURI_LOG_E("Game", "Failed to allocate NPCContext");
  265. return NULL;
  266. }
  267. npc_context_generic->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  268. npc_context_generic->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  269. // Set initial direction based on start and end positions
  270. if (start_position.x < end_position.x)
  271. {
  272. npc_context_generic->direction = ENTITY_RIGHT;
  273. }
  274. else
  275. {
  276. npc_context_generic->direction = ENTITY_LEFT;
  277. }
  278. // Set initial state based on movement
  279. if (start_position.x != end_position.x || start_position.y != end_position.y)
  280. {
  281. npc_context_generic->state = ENTITY_MOVING_TO_END;
  282. }
  283. else
  284. {
  285. npc_context_generic->state = ENTITY_IDLE;
  286. }
  287. return &_generic_npc;
  288. }
  289. void spawn_npc(Level *level, GameManager *manager, FuriString *json)
  290. {
  291. if (!level || !manager || !json)
  292. {
  293. FURI_LOG_E("Game", "Level, GameManager, or JSON is NULL");
  294. return;
  295. }
  296. FuriString *id = get_json_value_furi("id", json);
  297. FuriString *_index = get_json_value_furi("index", json);
  298. //
  299. FuriString *start_position = get_json_value_furi("start_position", json);
  300. FuriString *start_position_x = get_json_value_furi("x", start_position);
  301. FuriString *start_position_y = get_json_value_furi("y", start_position);
  302. //
  303. FuriString *end_position = get_json_value_furi("end_position", json);
  304. FuriString *end_position_x = get_json_value_furi("x", end_position);
  305. FuriString *end_position_y = get_json_value_furi("y", end_position);
  306. //
  307. FuriString *move_timer = get_json_value_furi("move_timer", json);
  308. FuriString *speed = get_json_value_furi("speed", json);
  309. //
  310. if (!id || !_index || !start_position || !start_position_x || !start_position_y || !end_position || !end_position_x || !end_position_y || !move_timer || !speed)
  311. {
  312. FURI_LOG_E("Game", "Failed to get JSON values");
  313. return;
  314. }
  315. GameContext *game_context = game_manager_game_context_get(manager);
  316. if (game_context && game_context->npc_count < MAX_NPCS && !game_context->npcs[game_context->npc_count])
  317. {
  318. game_context->npcs[game_context->npc_count] = level_add_entity(level, npc(
  319. manager,
  320. furi_string_get_cstr(id),
  321. atoi(furi_string_get_cstr(_index)),
  322. (Vector){atof_furi(start_position_x), atof_furi(start_position_y)},
  323. (Vector){atof_furi(end_position_x), atof_furi(end_position_y)},
  324. atof_furi(move_timer),
  325. atof_furi(speed)));
  326. game_context->npc_count++;
  327. }
  328. furi_string_free(id);
  329. furi_string_free(_index);
  330. furi_string_free(start_position);
  331. furi_string_free(start_position_x);
  332. furi_string_free(start_position_y);
  333. furi_string_free(end_position);
  334. furi_string_free(end_position_x);
  335. furi_string_free(end_position_y);
  336. furi_string_free(move_timer);
  337. furi_string_free(speed);
  338. }