player.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. #include <game/player.h>
  2. #include <game/storage.h>
  3. /****** Entities: Player ******/
  4. static Level *get_next_level(GameManager *manager)
  5. {
  6. GameContext *game_context = game_manager_game_context_get(manager);
  7. if (!game_context)
  8. {
  9. FURI_LOG_E(TAG, "Failed to get game context");
  10. return NULL;
  11. }
  12. game_context->current_level = game_context->current_level == 0 ? 1 : 0;
  13. if (!game_context->levels[game_context->current_level])
  14. {
  15. if (!allocate_level(manager, game_context->current_level))
  16. {
  17. FURI_LOG_E(TAG, "Failed to allocate level %d", game_context->current_level);
  18. return NULL;
  19. }
  20. }
  21. return game_context->levels[game_context->current_level];
  22. }
  23. void player_spawn(Level *level, GameManager *manager)
  24. {
  25. if (!level || !manager)
  26. {
  27. FURI_LOG_E(TAG, "Invalid arguments to player_spawn");
  28. return;
  29. }
  30. GameContext *game_context = game_manager_game_context_get(manager);
  31. if (!game_context)
  32. {
  33. FURI_LOG_E(TAG, "Failed to get game context");
  34. return;
  35. }
  36. game_context->player = level_add_entity(level, &player_desc);
  37. if (!game_context->player)
  38. {
  39. FURI_LOG_E(TAG, "Failed to add player entity to level");
  40. return;
  41. }
  42. // Set player position.
  43. entity_pos_set(game_context->player, (Vector){WORLD_WIDTH / 2, WORLD_HEIGHT / 2});
  44. // Box is centered in player x and y, and its size
  45. entity_collider_add_rect(game_context->player, 13, 11);
  46. // Get player context
  47. PlayerContext *player_context = entity_context_get(game_context->player);
  48. if (!player_context)
  49. {
  50. FURI_LOG_E(TAG, "Failed to get player context");
  51. return;
  52. }
  53. // player context must be set each level or NULL pointer will be dereferenced
  54. if (!load_player_context(player_context))
  55. {
  56. FURI_LOG_E(TAG, "Loading player context failed. Initializing default values.");
  57. // Initialize default player context
  58. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_sword_15x11px.fxbm");
  59. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_sword_15x11px.fxbm");
  60. player_context->direction = PLAYER_RIGHT; // default direction
  61. player_context->health = 100;
  62. player_context->strength = 10;
  63. player_context->level = 1;
  64. player_context->xp = 0;
  65. player_context->start_position = entity_pos_get(game_context->player);
  66. player_context->attack_timer = 0.1f;
  67. player_context->elapsed_attack_timer = player_context->attack_timer;
  68. player_context->health_regen = 1; // 1 health per second
  69. player_context->elapsed_health_regen = 0;
  70. player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
  71. // Set player username
  72. if (!load_char("Flip-Social-Username", player_context->username, sizeof(player_context->username)))
  73. {
  74. // If loading username fails, default to "Player"
  75. snprintf(player_context->username, sizeof(player_context->username), "Player");
  76. }
  77. game_context->player_context = player_context;
  78. // Save the initialized context
  79. if (!save_player_context(player_context))
  80. {
  81. FURI_LOG_E(TAG, "Failed to save player context after initialization");
  82. }
  83. return;
  84. }
  85. // Load player sprite (we'll add this to the JSON later when players can choose their sprite)
  86. player_context->sprite_right = game_manager_sprite_load(manager, "player_right_sword_15x11px.fxbm");
  87. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_sword_15x11px.fxbm");
  88. player_context->start_position = entity_pos_get(game_context->player);
  89. // Update player stats based on XP using iterative method
  90. // Function to get the current level based on XP iteratively
  91. int get_player_level_iterative(uint32_t xp)
  92. {
  93. int level = 1;
  94. uint32_t xp_required = 100; // Base XP for level 2
  95. while (level < 100 && xp >= xp_required) // Maximum level supported
  96. {
  97. level++;
  98. xp_required = (uint32_t)(xp_required * 1.5); // 1.5 growth factor per level
  99. }
  100. return level;
  101. }
  102. // Determine the player's level based on XP
  103. player_context->level = get_player_level_iterative(player_context->xp);
  104. // Update strength and max health based on the new level
  105. player_context->strength = 10 + (player_context->level * 1); // 1 strength per level
  106. player_context->max_health = 100 + ((player_context->level - 1) * 10); // 10 health per level
  107. // Assign loaded player context to game context
  108. game_context->player_context = player_context;
  109. }
  110. // code from Derek Jamison
  111. // eventually we'll add dynamic positioning based on how much pitch/roll is detected
  112. // instead of assigning a fixed value
  113. static int player_x_from_pitch(float pitch)
  114. {
  115. if (pitch > 6.0)
  116. {
  117. return 1;
  118. }
  119. else if (pitch < -8.0)
  120. {
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int player_y_from_roll(float roll)
  126. {
  127. if (roll > 9.0)
  128. {
  129. return 1;
  130. }
  131. else if (roll < -20.0)
  132. {
  133. return -1;
  134. }
  135. return 0;
  136. }
  137. static void player_update(Entity *self, GameManager *manager, void *context)
  138. {
  139. if (!self || !manager || !context)
  140. return;
  141. PlayerContext *player = (PlayerContext *)context;
  142. InputState input = game_manager_input_get(manager);
  143. Vector pos = entity_pos_get(self);
  144. GameContext *game_context = game_manager_game_context_get(manager);
  145. // Store previous direction
  146. int prev_dx = player->dx;
  147. int prev_dy = player->dy;
  148. // Reset movement deltas each frame
  149. player->dx = 0;
  150. player->dy = 0;
  151. if (game_context->imu_present)
  152. {
  153. player->dx = player_x_from_pitch(-imu_pitch_get(game_context->imu));
  154. player->dy = player_y_from_roll(-imu_roll_get(game_context->imu));
  155. switch (player->dx)
  156. {
  157. case -1:
  158. player->direction = PLAYER_LEFT;
  159. pos.x -= 1;
  160. break;
  161. case 1:
  162. player->direction = PLAYER_RIGHT;
  163. pos.x += 1;
  164. break;
  165. default:
  166. break;
  167. }
  168. switch (player->dy)
  169. {
  170. case -1:
  171. player->direction = PLAYER_UP;
  172. pos.y -= 1;
  173. break;
  174. case 1:
  175. player->direction = PLAYER_DOWN;
  176. pos.y += 1;
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. // Apply health regeneration
  183. player->elapsed_health_regen += 1.0f / game_context->fps;
  184. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  185. {
  186. player->health += (player->health_regen + player->health > player->max_health)
  187. ? (player->max_health - player->health)
  188. : player->health_regen;
  189. player->elapsed_health_regen = 0;
  190. }
  191. // Increment the elapsed_attack_timer for the player
  192. player->elapsed_attack_timer += 1.0f / game_context->fps;
  193. // Handle movement input
  194. if (input.held & GameKeyUp)
  195. {
  196. pos.y -= 2;
  197. player->dy = -1;
  198. player->direction = PLAYER_UP;
  199. game_context->user_input = GameKeyUp;
  200. }
  201. if (input.held & GameKeyDown)
  202. {
  203. pos.y += 2;
  204. player->dy = 1;
  205. player->direction = PLAYER_DOWN;
  206. game_context->user_input = GameKeyDown;
  207. }
  208. if (input.held & GameKeyLeft)
  209. {
  210. pos.x -= 2;
  211. player->dx = -1;
  212. player->direction = PLAYER_LEFT;
  213. game_context->user_input = GameKeyLeft;
  214. }
  215. if (input.held & GameKeyRight)
  216. {
  217. pos.x += 2;
  218. player->dx = 1;
  219. player->direction = PLAYER_RIGHT;
  220. game_context->user_input = GameKeyRight;
  221. }
  222. // Clamp the player's position to stay within world bounds
  223. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  224. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  225. // Update player position
  226. entity_pos_set(self, pos);
  227. // switch levels if holding OK
  228. if (input.pressed & GameKeyOk)
  229. {
  230. FURI_LOG_I(TAG, "Player is pressing OK");
  231. // if all enemies are dead, allow the "OK" button to switch levels
  232. // otherwise the "OK" button will be used to attack
  233. if (game_context->enemy_count == 0)
  234. {
  235. FURI_LOG_I(TAG, "Switching levels");
  236. save_player_context(player);
  237. game_manager_next_level_set(manager, get_next_level(manager));
  238. furi_delay_ms(500);
  239. }
  240. else
  241. {
  242. FURI_LOG_I(TAG, "Player is attacking");
  243. game_context->user_input = GameKeyOk;
  244. // furi_delay_ms(100);
  245. }
  246. FURI_LOG_I(TAG, "Player is done pressing OK");
  247. }
  248. // If the player is not moving, retain the last movement direction
  249. if (player->dx == 0 && player->dy == 0)
  250. {
  251. player->dx = prev_dx;
  252. player->dy = prev_dy;
  253. player->state = PLAYER_IDLE;
  254. game_context->user_input = -1; // reset user input
  255. }
  256. else
  257. {
  258. player->state = PLAYER_MOVING;
  259. }
  260. // Handle back button to stop the game
  261. if (input.pressed & GameKeyBack)
  262. {
  263. game_manager_game_stop(manager);
  264. }
  265. }
  266. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  267. {
  268. UNUSED(manager);
  269. if (!self || !context || !canvas)
  270. return;
  271. // Get player context
  272. PlayerContext *player = context;
  273. // Get player position
  274. Vector pos = entity_pos_get(self);
  275. // Draw background (updates camera_x and camera_y)
  276. draw_background(canvas, pos);
  277. // Draw player sprite relative to camera, centered on the player's position
  278. canvas_draw_sprite(
  279. canvas,
  280. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  281. pos.x - camera_x - 5, // Center the sprite horizontally
  282. pos.y - camera_y - 5 // Center the sprite vertically
  283. );
  284. }
  285. const EntityDescription player_desc = {
  286. .start = NULL, // called when entity is added to the level
  287. .stop = NULL, // called when entity is removed from the level
  288. .update = player_update, // called every frame
  289. .render = player_render, // called every frame, after update
  290. .collision = NULL, // called when entity collides with another entity
  291. .event = NULL, // called when entity receives an event
  292. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  293. };
  294. static SpriteContext *sprite_generic_alloc(const char *id, bool is_enemy, uint8_t width, uint8_t height)
  295. {
  296. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  297. if (!ctx)
  298. {
  299. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  300. return NULL;
  301. }
  302. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  303. ctx->width = width;
  304. ctx->height = height;
  305. if (!is_enemy)
  306. {
  307. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  308. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  309. }
  310. else
  311. {
  312. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "enemy_right_%s_%dx%dpx.fxbm", id, width, height);
  313. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "enemy_left_%s_%dx%dpx.fxbm", id, width, height);
  314. }
  315. return ctx;
  316. }
  317. SpriteContext *get_sprite_context(const char *name)
  318. {
  319. if (strcmp(name, "axe") == 0)
  320. {
  321. return sprite_generic_alloc("axe", false, 15, 11);
  322. }
  323. else if (strcmp(name, "bow") == 0)
  324. {
  325. return sprite_generic_alloc("bow", false, 13, 11);
  326. }
  327. else if (strcmp(name, "naked") == 0)
  328. {
  329. return sprite_generic_alloc("naked", false, 10, 10);
  330. }
  331. else if (strcmp(name, "sword") == 0)
  332. {
  333. return sprite_generic_alloc("sword", false, 15, 11);
  334. }
  335. else if (strcmp(name, "cyclops") == 0)
  336. {
  337. return sprite_generic_alloc("cyclops", true, 10, 11);
  338. }
  339. else if (strcmp(name, "ghost") == 0)
  340. {
  341. return sprite_generic_alloc("ghost", true, 15, 15);
  342. }
  343. else if (strcmp(name, "ogre") == 0)
  344. {
  345. return sprite_generic_alloc("ogre", true, 10, 13);
  346. }
  347. // If no match is found
  348. FURI_LOG_E("Game", "Sprite not found: %s", name);
  349. return NULL;
  350. }