player.c 15 KB

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