player.c 17 KB

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