player.c 17 KB

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