npc.c 16 KB

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