player.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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 int vgm_increase(float value, float increase)
  141. {
  142. const int val = abs((int)(round(value + increase) / (double)1.5));
  143. return val < 1 ? 1 : val;
  144. }
  145. static void vgm_direction(Imu *imu, PlayerContext *player, Vector *pos)
  146. {
  147. const float pitch = -imu_pitch_get(imu);
  148. const float roll = -imu_roll_get(imu);
  149. const float min_x = atof_(vgm_levels[vgm_x_index]) + 5.0; // minimum of 3
  150. const float min_y = atof_(vgm_levels[vgm_y_index]) + 5.0; // minimum of 3
  151. if (pitch > min_x)
  152. {
  153. pos->x += vgm_increase(pitch, min_x);
  154. player->dx = 1;
  155. player->direction = PLAYER_RIGHT;
  156. }
  157. else if (pitch < -min_x)
  158. {
  159. pos->x += -vgm_increase(pitch, min_x);
  160. player->dx = -1;
  161. player->direction = PLAYER_LEFT;
  162. }
  163. if (roll > min_y)
  164. {
  165. pos->y += vgm_increase(roll, min_y);
  166. player->dy = 1;
  167. player->direction = PLAYER_DOWN;
  168. }
  169. else if (roll < -min_y)
  170. {
  171. pos->y += -vgm_increase(roll, min_y);
  172. player->dy = -1;
  173. player->direction = PLAYER_UP;
  174. }
  175. }
  176. static void player_update(Entity *self, GameManager *manager, void *context)
  177. {
  178. if (!self || !manager || !context)
  179. return;
  180. PlayerContext *player = (PlayerContext *)context;
  181. InputState input = game_manager_input_get(manager);
  182. Vector pos = entity_pos_get(self);
  183. GameContext *game_context = game_manager_game_context_get(manager);
  184. // Store previous direction
  185. int prev_dx = player->dx;
  186. int prev_dy = player->dy;
  187. // Reset movement deltas each frame
  188. player->dx = 0;
  189. player->dy = 0;
  190. if (game_context->imu_present)
  191. {
  192. // update position using the IMU
  193. vgm_direction(game_context->imu, player, &pos);
  194. }
  195. // Apply health regeneration
  196. player->elapsed_health_regen += 1.0f / game_context->fps;
  197. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  198. {
  199. player->health += (player->health_regen + player->health > player->max_health)
  200. ? (player->max_health - player->health)
  201. : player->health_regen;
  202. player->elapsed_health_regen = 0;
  203. }
  204. // Increment the elapsed_attack_timer for the player
  205. player->elapsed_attack_timer += 1.0f / game_context->fps;
  206. // Handle movement input
  207. if (input.held & GameKeyUp)
  208. {
  209. if (game_context->last_button == GameKeyUp)
  210. game_context->elapsed_button_timer += 1;
  211. else
  212. game_context->elapsed_button_timer = 0;
  213. if (!game_context->is_menu_open)
  214. {
  215. pos.y -= (2 + game_context->icon_offset);
  216. player->dy = -1;
  217. player->direction = PLAYER_UP;
  218. }
  219. else
  220. {
  221. // next menu view
  222. // we can only go up to info from settings
  223. game_context->menu_screen = GAME_MENU_INFO;
  224. }
  225. game_context->last_button = GameKeyUp;
  226. }
  227. if (input.held & GameKeyDown)
  228. {
  229. if (game_context->last_button == GameKeyDown)
  230. game_context->elapsed_button_timer += 1;
  231. else
  232. game_context->elapsed_button_timer = 0;
  233. if (!game_context->is_menu_open)
  234. {
  235. pos.y += (2 + game_context->icon_offset);
  236. player->dy = 1;
  237. player->direction = PLAYER_DOWN;
  238. }
  239. else
  240. {
  241. // next menu view
  242. // we can only go down to more from info
  243. game_context->menu_screen = GAME_MENU_MORE;
  244. }
  245. game_context->last_button = GameKeyDown;
  246. }
  247. if (input.held & GameKeyLeft)
  248. {
  249. if (game_context->last_button == GameKeyLeft)
  250. game_context->elapsed_button_timer += 1;
  251. else
  252. game_context->elapsed_button_timer = 0;
  253. if (!game_context->is_menu_open)
  254. {
  255. pos.x -= (2 + game_context->icon_offset);
  256. player->dx = -1;
  257. player->direction = PLAYER_LEFT;
  258. }
  259. else
  260. {
  261. // if the menu is open, move the selection left
  262. if (game_context->menu_selection < 1)
  263. {
  264. game_context->menu_selection += 1;
  265. }
  266. }
  267. game_context->last_button = GameKeyLeft;
  268. }
  269. if (input.held & GameKeyRight)
  270. {
  271. if (game_context->last_button == GameKeyRight)
  272. game_context->elapsed_button_timer += 1;
  273. else
  274. game_context->elapsed_button_timer = 0;
  275. if (!game_context->is_menu_open)
  276. {
  277. pos.x += (2 + game_context->icon_offset);
  278. player->dx = 1;
  279. player->direction = PLAYER_RIGHT;
  280. }
  281. else
  282. {
  283. // if the menu is open, move the selection right
  284. if (game_context->menu_selection < 1)
  285. {
  286. game_context->menu_selection += 1;
  287. }
  288. }
  289. game_context->last_button = GameKeyRight;
  290. }
  291. if (input.held & GameKeyOk)
  292. {
  293. if (game_context->last_button == GameKeyOk)
  294. game_context->elapsed_button_timer += 1;
  295. else
  296. game_context->elapsed_button_timer = 0;
  297. game_context->last_button = GameKeyOk;
  298. // if all enemies are dead, allow the "OK" button to switch levels
  299. // otherwise the "OK" button will be used to attack
  300. if (game_context->enemy_count == 0 && !game_context->is_switching_level)
  301. {
  302. game_context->is_switching_level = true;
  303. save_player_context(player);
  304. game_manager_next_level_set(manager, next_level(manager));
  305. return;
  306. }
  307. // if the OK button is held for 1 seconds,show the menu
  308. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  309. {
  310. // open up menu on the INFO screen
  311. game_context->menu_screen = GAME_MENU_INFO;
  312. game_context->menu_selection = 0;
  313. game_context->is_menu_open = true;
  314. }
  315. }
  316. if (input.held & GameKeyBack)
  317. {
  318. if (game_context->last_button == GameKeyBack)
  319. game_context->elapsed_button_timer += 1;
  320. else
  321. game_context->elapsed_button_timer = 0;
  322. game_context->last_button = GameKeyBack;
  323. if (game_context->is_menu_open)
  324. {
  325. game_context->is_menu_open = false;
  326. }
  327. // if the back button is held for 1 seconds, stop the game
  328. if (game_context->elapsed_button_timer > (1 * game_context->fps))
  329. {
  330. if (!game_context->is_menu_open)
  331. {
  332. game_manager_game_stop(manager);
  333. return;
  334. }
  335. }
  336. }
  337. // Clamp the player's position to stay within world bounds
  338. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  339. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  340. // Update player position
  341. entity_pos_set(self, pos);
  342. // If the player is not moving, retain the last movement direction
  343. if (player->dx == 0 && player->dy == 0)
  344. {
  345. player->dx = prev_dx;
  346. player->dy = prev_dy;
  347. player->state = PLAYER_IDLE;
  348. }
  349. else
  350. player->state = PLAYER_MOVING;
  351. }
  352. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  353. {
  354. if (!self || !context || !canvas || !manager)
  355. return;
  356. // Get player context
  357. PlayerContext *player = context;
  358. // Get player position
  359. Vector pos = entity_pos_get(self);
  360. // Calculate camera offset to center the player
  361. camera_x = pos.x - (SCREEN_WIDTH / 2);
  362. camera_y = pos.y - (SCREEN_HEIGHT / 2);
  363. // Clamp camera position to prevent showing areas outside the world
  364. camera_x = CLAMP(camera_x, WORLD_WIDTH - SCREEN_WIDTH, 0);
  365. camera_y = CLAMP(camera_y, WORLD_HEIGHT - SCREEN_HEIGHT, 0);
  366. // Draw player sprite relative to camera, centered on the player's position
  367. canvas_draw_sprite(
  368. canvas,
  369. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  370. pos.x - camera_x - 5, // Center the sprite horizontally
  371. pos.y - camera_y - 5 // Center the sprite vertically
  372. );
  373. // Draw the outer bounds adjusted by camera offset
  374. canvas_draw_frame(canvas, -camera_x, -camera_y, WORLD_WIDTH, WORLD_HEIGHT);
  375. // Draw the user stats (health, xp, and level)
  376. background_render(canvas, manager);
  377. }
  378. const EntityDescription player_desc = {
  379. .start = NULL, // called when entity is added to the level
  380. .stop = NULL, // called when entity is removed from the level
  381. .update = player_update, // called every frame
  382. .render = player_render, // called every frame, after update
  383. .collision = NULL, // called when entity collides with another entity
  384. .event = NULL, // called when entity receives an event
  385. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  386. };
  387. static SpriteContext *sprite_generic_alloc(const char *id, bool is_enemy, uint8_t width, uint8_t height)
  388. {
  389. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  390. if (!ctx)
  391. {
  392. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  393. return NULL;
  394. }
  395. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  396. ctx->width = width;
  397. ctx->height = height;
  398. if (!is_enemy)
  399. {
  400. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  401. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  402. }
  403. else
  404. {
  405. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  406. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  407. }
  408. return ctx;
  409. }
  410. SpriteContext *get_sprite_context(const char *name)
  411. {
  412. if (is_str(name, "axe"))
  413. {
  414. return sprite_generic_alloc("axe", false, 15, 11);
  415. }
  416. else if (is_str(name, "bow"))
  417. {
  418. return sprite_generic_alloc("bow", false, 13, 11);
  419. }
  420. else if (is_str(name, "naked"))
  421. {
  422. return sprite_generic_alloc("naked", false, 10, 10);
  423. }
  424. else if (is_str(name, "sword"))
  425. {
  426. return sprite_generic_alloc("sword", false, 15, 11);
  427. }
  428. else if (is_str(name, "cyclops"))
  429. {
  430. return sprite_generic_alloc("cyclops", true, 10, 11);
  431. }
  432. else if (is_str(name, "ghost"))
  433. {
  434. return sprite_generic_alloc("ghost", true, 15, 15);
  435. }
  436. else if (is_str(name, "ogre"))
  437. {
  438. return sprite_generic_alloc("ogre", true, 10, 13);
  439. }
  440. // If no match is found
  441. FURI_LOG_E("Game", "Sprite not found: %s", name);
  442. return NULL;
  443. }