player.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include <game/player.h>
  2. #include <game/storage.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. /****** Entities: Player ******/
  7. static Level *next_level(GameManager *manager)
  8. {
  9. GameContext *game_context = game_manager_game_context_get(manager);
  10. if (!game_context)
  11. {
  12. FURI_LOG_E(TAG, "Failed to get game context");
  13. return NULL;
  14. }
  15. // check if there are more levels to load
  16. if (game_context->current_level + 1 >= game_context->level_count)
  17. {
  18. game_context->current_level = 0;
  19. if (!game_context->levels[game_context->current_level])
  20. {
  21. if (!allocate_level(manager, game_context->current_level))
  22. {
  23. FURI_LOG_E(TAG, "Failed to allocate level %d", game_context->current_level);
  24. return NULL;
  25. }
  26. }
  27. return game_context->levels[game_context->current_level];
  28. }
  29. for (int i = game_context->current_level + 1; i < game_context->level_count; i++)
  30. {
  31. if (!game_context->levels[i])
  32. {
  33. if (!allocate_level(manager, i))
  34. {
  35. FURI_LOG_E(TAG, "Failed to allocate level %d", i);
  36. return NULL;
  37. }
  38. }
  39. game_context->current_level = i;
  40. return game_context->levels[i];
  41. }
  42. return NULL;
  43. }
  44. void player_spawn(Level *level, GameManager *manager)
  45. {
  46. if (!level || !manager)
  47. {
  48. FURI_LOG_E(TAG, "Invalid arguments to player_spawn");
  49. return;
  50. }
  51. GameContext *game_context = game_manager_game_context_get(manager);
  52. if (!game_context)
  53. {
  54. FURI_LOG_E(TAG, "Failed to get game context");
  55. return;
  56. }
  57. game_context->player = level_add_entity(level, &player_desc);
  58. if (!game_context->player)
  59. {
  60. FURI_LOG_E(TAG, "Failed to add player entity to level");
  61. return;
  62. }
  63. // Set player position.
  64. entity_pos_set(game_context->player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  65. // Box is centered in player x and y, and its size
  66. entity_collider_add_rect(game_context->player, 13, 11);
  67. // Get player context
  68. PlayerContext *pctx = entity_context_get(game_context->player);
  69. if (!pctx)
  70. {
  71. FURI_LOG_E(TAG, "Failed to get player context");
  72. return;
  73. }
  74. SpriteContext *sprite_context = get_sprite_context(player_sprite_choices[player_sprite_index]);
  75. if (!sprite_context)
  76. {
  77. FURI_LOG_E(TAG, "Failed to get sprite context");
  78. return;
  79. }
  80. // player context must be set each level or NULL pointer will be dereferenced
  81. if (!load_player_context(pctx))
  82. {
  83. FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
  84. // Initialize default player context
  85. pctx->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  86. pctx->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  87. pctx->direction = PLAYER_RIGHT; // default direction
  88. pctx->health = 100;
  89. pctx->strength = 10;
  90. pctx->level = 1;
  91. pctx->xp = 0;
  92. pctx->start_position = entity_pos_get(game_context->player);
  93. pctx->attack_timer = 0.1f;
  94. pctx->elapsed_attack_timer = pctx->attack_timer;
  95. pctx->health_regen = 1; // 1 health per second
  96. pctx->elapsed_health_regen = 0;
  97. pctx->max_health = 100 + ((pctx->level - 1) * 10); // 10 health per level
  98. // Set player username
  99. if (!load_char("Flip-Social-Username", pctx->username, sizeof(pctx->username)))
  100. {
  101. // check if data/player/username
  102. if (!load_char("player/username", pctx->username, sizeof(pctx->username)))
  103. {
  104. // If loading username fails, default to "Player"
  105. snprintf(pctx->username, sizeof(pctx->username), "Player");
  106. }
  107. }
  108. game_context->player_context = pctx;
  109. // Save the initialized context
  110. if (!save_player_context(pctx))
  111. {
  112. FURI_LOG_E(TAG, "Failed to save player context after initialization");
  113. }
  114. return;
  115. }
  116. // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
  117. pctx->sprite_right = game_manager_sprite_load(manager, sprite_context->right_file_name);
  118. pctx->sprite_left = game_manager_sprite_load(manager, sprite_context->left_file_name);
  119. pctx->start_position = entity_pos_get(game_context->player);
  120. // Update player stats based on XP using iterative method
  121. int get_player_level_iterative(uint32_t xp)
  122. {
  123. int level = 1;
  124. uint32_t xp_required = 100; // Base XP for level 2
  125. while (level < 100 && xp >= xp_required) // Maximum level supported
  126. {
  127. level++;
  128. xp_required = (uint32_t)(xp_required * 1.5); // 1.5 growth factor per level
  129. }
  130. return level;
  131. }
  132. // Determine the player's level based on XP
  133. pctx->level = get_player_level_iterative(pctx->xp);
  134. // Update strength and max health based on the new level
  135. pctx->strength = 10 + (pctx->level * 1); // 1 strength per level
  136. pctx->max_health = 100 + ((pctx->level - 1) * 10); // 10 health per level
  137. // Assign loaded player context to game context
  138. game_context->player_context = pctx;
  139. }
  140. static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
  141. {
  142. const float pitch = -imu_pitch_get(imu);
  143. const float roll = -imu_roll_get(imu);
  144. const float min_x = atof_(vgm_levels[vgm_x_index]) + 5.0; // minimum of 3
  145. const float min_y = atof_(vgm_levels[vgm_y_index]) + 5.0; // minimum of 3
  146. if (pitch > min_x)
  147. {
  148. pos->x += abs((int)round(pitch / min_x));
  149. player->dx = 1;
  150. player->direction = PLAYER_RIGHT;
  151. }
  152. else if (pitch < -min_x)
  153. {
  154. pos->x += -abs((int)round(pitch / min_x));
  155. player->dx = -1;
  156. player->direction = PLAYER_LEFT;
  157. }
  158. if (roll > min_y)
  159. {
  160. pos->y += abs((int)round(roll / min_y));
  161. player->dy = 1;
  162. player->direction = PLAYER_DOWN;
  163. }
  164. else if (roll < -min_y)
  165. {
  166. pos->y += -abs((int)round(roll / min_y));
  167. player->dy = -1;
  168. player->direction = PLAYER_UP;
  169. }
  170. }
  171. static void player_update(Entity *self, GameManager *manager, void *context)
  172. {
  173. if (!self || !manager || !context)
  174. return;
  175. PlayerContext *player = (PlayerContext *)context;
  176. InputState input = game_manager_input_get(manager);
  177. Vector pos = entity_pos_get(self);
  178. GameContext *game_context = game_manager_game_context_get(manager);
  179. // Store previous direction
  180. int prev_dx = player->dx;
  181. int prev_dy = player->dy;
  182. // Reset movement deltas each frame
  183. player->dx = 0;
  184. player->dy = 0;
  185. if (game_context->imu_present)
  186. {
  187. // update position using the IMU
  188. vgm_direction(game_context->imu, player, &pos);
  189. }
  190. // Apply health regeneration
  191. player->elapsed_health_regen += 1.0f / game_context->fps;
  192. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  193. {
  194. player->health += (player->health_regen + player->health > player->max_health)
  195. ? (player->max_health - player->health)
  196. : player->health_regen;
  197. player->elapsed_health_regen = 0;
  198. }
  199. // Increment the elapsed_attack_timer for the player
  200. player->elapsed_attack_timer += 1.0f / game_context->fps;
  201. // Handle movement input
  202. if (input.held & GameKeyUp)
  203. {
  204. if (game_context->last_button == GameKeyUp)
  205. game_context->elapsed_button_timer += 1;
  206. else
  207. game_context->elapsed_button_timer = 0;
  208. if (!game_context->is_menu_open)
  209. {
  210. pos.y -= (2 + game_context->icon_offset);
  211. player->dy = -1;
  212. player->direction = PLAYER_UP;
  213. }
  214. else
  215. {
  216. // next menu view
  217. // we can only go up to info from settings
  218. game_context->menu_screen = GAME_MENU_INFO;
  219. }
  220. game_context->last_button = GameKeyUp;
  221. }
  222. if (input.held & GameKeyDown)
  223. {
  224. if (game_context->last_button == GameKeyDown)
  225. game_context->elapsed_button_timer += 1;
  226. else
  227. game_context->elapsed_button_timer = 0;
  228. if (!game_context->is_menu_open)
  229. {
  230. pos.y += (2 + game_context->icon_offset);
  231. player->dy = 1;
  232. player->direction = PLAYER_DOWN;
  233. }
  234. else
  235. {
  236. // next menu view
  237. // we can only go down to more from info
  238. game_context->menu_screen = GAME_MENU_MORE;
  239. }
  240. game_context->last_button = GameKeyDown;
  241. }
  242. if (input.held & GameKeyLeft)
  243. {
  244. if (game_context->last_button == GameKeyLeft)
  245. game_context->elapsed_button_timer += 1;
  246. else
  247. game_context->elapsed_button_timer = 0;
  248. if (!game_context->is_menu_open)
  249. {
  250. pos.x -= (2 + game_context->icon_offset);
  251. player->dx = -1;
  252. player->direction = PLAYER_LEFT;
  253. }
  254. else
  255. {
  256. // if the menu is open, move the selection left
  257. if (game_context->menu_selection < 1)
  258. {
  259. game_context->menu_selection += 1;
  260. }
  261. }
  262. game_context->last_button = GameKeyLeft;
  263. }
  264. if (input.held & GameKeyRight)
  265. {
  266. if (game_context->last_button == GameKeyRight)
  267. game_context->elapsed_button_timer += 1;
  268. else
  269. game_context->elapsed_button_timer = 0;
  270. if (!game_context->is_menu_open)
  271. {
  272. pos.x += (2 + game_context->icon_offset);
  273. player->dx = 1;
  274. player->direction = PLAYER_RIGHT;
  275. }
  276. else
  277. {
  278. // if the menu is open, move the selection right
  279. if (game_context->menu_selection < 1)
  280. {
  281. game_context->menu_selection += 1;
  282. }
  283. }
  284. game_context->last_button = GameKeyRight;
  285. }
  286. if (input.held & GameKeyOk)
  287. {
  288. if (game_context->last_button == GameKeyOk)
  289. game_context->elapsed_button_timer += 1;
  290. else
  291. game_context->elapsed_button_timer = 0;
  292. game_context->last_button = GameKeyOk;
  293. // if all enemies are dead, allow the "OK" button to switch levels
  294. // otherwise the "OK" button will be used to attack
  295. if (game_context->enemy_count == 0 && !game_context->is_switching_level)
  296. {
  297. game_context->is_switching_level = true;
  298. save_player_context(player);
  299. game_manager_next_level_set(manager, next_level(manager));
  300. return;
  301. }
  302. // if the OK button is held for 1 seconds,show the menu
  303. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  304. {
  305. // open up menu on the INFO screen
  306. game_context->menu_screen = GAME_MENU_INFO;
  307. game_context->menu_selection = 0;
  308. game_context->is_menu_open = true;
  309. }
  310. }
  311. if (input.held & GameKeyBack)
  312. {
  313. if (game_context->last_button == GameKeyBack)
  314. game_context->elapsed_button_timer += 1;
  315. else
  316. game_context->elapsed_button_timer = 0;
  317. game_context->last_button = GameKeyBack;
  318. if (game_context->is_menu_open)
  319. {
  320. game_context->is_menu_open = false;
  321. }
  322. // if the back button is held for 1 seconds, stop the game
  323. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  324. {
  325. if (!game_context->is_menu_open)
  326. {
  327. game_manager_game_stop(manager);
  328. return;
  329. }
  330. }
  331. }
  332. // Clamp the player's position to stay within world bounds
  333. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  334. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  335. // Update player position
  336. entity_pos_set(self, pos);
  337. // If the player is not moving, retain the last movement direction
  338. if (player->dx == 0 && player->dy == 0)
  339. {
  340. player->dx = prev_dx;
  341. player->dy = prev_dy;
  342. player->state = PLAYER_IDLE;
  343. }
  344. else
  345. player->state = PLAYER_MOVING;
  346. }
  347. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  348. {
  349. if (!self || !context || !canvas || !manager)
  350. return;
  351. // Get player context
  352. PlayerContext *player = context;
  353. // Get player position
  354. Vector pos = entity_pos_get(self);
  355. // Calculate camera offset to center the player
  356. camera_x = pos.x - (SCREEN_WIDTH / 2);
  357. camera_y = pos.y - (SCREEN_HEIGHT / 2);
  358. // Clamp camera position to prevent showing areas outside the world
  359. camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
  360. camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
  361. // Draw player sprite relative to camera, centered on the player's position
  362. canvas_draw_sprite(
  363. canvas,
  364. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  365. pos.x - camera_x - 5, // Center the sprite horizontally
  366. pos.y - camera_y - 5 // Center the sprite vertically
  367. );
  368. // Draw the outer bounds adjusted by camera offset
  369. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  370. // Draw the user stats (health, xp, and level)
  371. background_render(canvas, manager);
  372. }
  373. const EntityDescription player_desc = {
  374. .start = NULL, // called when entity is added to the level
  375. .stop = NULL, // called when entity is removed from the level
  376. .update = player_update, // called every frame
  377. .render = player_render, // called every frame, after update
  378. .collision = NULL, // called when entity collides with another entity
  379. .event = NULL, // called when entity receives an event
  380. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  381. };
  382. static SpriteContext *sprite_generic_alloc(const char *id, bool is_enemy, uint8_t width, uint8_t height)
  383. {
  384. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  385. if (!ctx)
  386. {
  387. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  388. return NULL;
  389. }
  390. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  391. ctx->width = width;
  392. ctx->height = height;
  393. if (!is_enemy)
  394. {
  395. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  396. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  397. }
  398. else
  399. {
  400. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  401. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  402. }
  403. return ctx;
  404. }
  405. SpriteContext *get_sprite_context(const char *name)
  406. {
  407. if (is_str(name, "axe"))
  408. {
  409. return sprite_generic_alloc("axe", false, 15, 11);
  410. }
  411. else if (is_str(name, "bow"))
  412. {
  413. return sprite_generic_alloc("bow", false, 13, 11);
  414. }
  415. else if (is_str(name, "naked"))
  416. {
  417. return sprite_generic_alloc("naked", false, 10, 10);
  418. }
  419. else if (is_str(name, "sword"))
  420. {
  421. return sprite_generic_alloc("sword", false, 15, 11);
  422. }
  423. else if (is_str(name, "cyclops"))
  424. {
  425. return sprite_generic_alloc("cyclops", true, 10, 11);
  426. }
  427. else if (is_str(name, "ghost"))
  428. {
  429. return sprite_generic_alloc("ghost", true, 15, 15);
  430. }
  431. else if (is_str(name, "ogre"))
  432. {
  433. return sprite_generic_alloc("ogre", true, 10, 13);
  434. }
  435. // If no match is found
  436. FURI_LOG_E("Game", "Sprite not found: %s", name);
  437. return NULL;
  438. }