player.c 12 KB

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