player.c 16 KB

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