npc.c 15 KB

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