player.c 16 KB

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