player.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_axe_15x11px.fxbm");
  59. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_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_axe_15x11px.fxbm");
  87. player_context->sprite_left = game_manager_sprite_load(manager, "player_left_axe_15x11px.fxbm");
  88. player_context->start_position = entity_pos_get(game_context->player);
  89. // Assign loaded player context to game context
  90. game_context->player_context = player_context;
  91. }
  92. // code from Derek Jamison
  93. // eventually we'll add dynamic positioning based on how much pitch/roll is detected
  94. // instead of assigning a fixed value
  95. static int player_x_from_pitch(float pitch)
  96. {
  97. if (pitch > 5.0) // was 9.0
  98. {
  99. return 1;
  100. }
  101. else if (pitch < -5.0) // was -9.0
  102. {
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. static int player_y_from_roll(float roll)
  108. {
  109. if (roll > 5.0)
  110. {
  111. return 1;
  112. }
  113. else if (roll < -10.0) // was -20
  114. {
  115. return -1;
  116. }
  117. return 0;
  118. }
  119. static bool is_new_level = false;
  120. // Modify player_update to track direction
  121. static void player_update(Entity *self, GameManager *manager, void *context)
  122. {
  123. PlayerContext *player = (PlayerContext *)context;
  124. InputState input = game_manager_input_get(manager);
  125. Vector pos = entity_pos_get(self);
  126. GameContext *game_context = game_manager_game_context_get(manager);
  127. // Store previous direction
  128. int prev_dx = player->dx;
  129. int prev_dy = player->dy;
  130. // Reset movement deltas each frame
  131. player->dx = 0;
  132. player->dy = 0;
  133. if (game_context->imu_present)
  134. {
  135. player->dx = player_x_from_pitch(-imu_pitch_get(game_context->imu));
  136. player->dy = player_y_from_roll(-imu_roll_get(game_context->imu));
  137. switch (player_x_from_pitch(-imu_pitch_get(game_context->imu)))
  138. {
  139. case -1:
  140. player->direction = PLAYER_LEFT;
  141. player->dx = -1;
  142. pos.x -= 1;
  143. break;
  144. case 1:
  145. player->direction = PLAYER_RIGHT;
  146. player->dx = 1;
  147. pos.x += 1;
  148. break;
  149. default:
  150. break;
  151. };
  152. switch (player_y_from_roll(-imu_roll_get(game_context->imu)))
  153. {
  154. case -1:
  155. player->direction = PLAYER_UP;
  156. player->dy = -1;
  157. pos.y -= 1;
  158. break;
  159. case 1:
  160. player->direction = PLAYER_DOWN;
  161. player->dy = 1;
  162. pos.y += 1;
  163. break;
  164. default:
  165. break;
  166. };
  167. }
  168. if (is_new_level)
  169. {
  170. // remove previous level if it exists
  171. free_last_level(manager);
  172. is_new_level = false;
  173. }
  174. // apply health regeneration
  175. player->elapsed_health_regen += 1.0f / game_context->fps;
  176. if (player->elapsed_health_regen >= 1.0f && player->health < player->max_health)
  177. {
  178. player->health += (player->health_regen + player->health > player->max_health) ? player->max_health - player->health : player->health_regen;
  179. player->elapsed_health_regen = 0;
  180. }
  181. // Increment the elapsed_attack_timer for the player
  182. player->elapsed_attack_timer += 1.0f / game_context->fps;
  183. // Handle movement input
  184. if (input.held & GameKeyUp)
  185. {
  186. pos.y -= 2;
  187. player->dy = -1;
  188. player->direction = PLAYER_UP;
  189. game_context->user_input = GameKeyUp;
  190. }
  191. if (input.held & GameKeyDown)
  192. {
  193. pos.y += 2;
  194. player->dy = 1;
  195. player->direction = PLAYER_DOWN;
  196. game_context->user_input = GameKeyDown;
  197. }
  198. if (input.held & GameKeyLeft)
  199. {
  200. pos.x -= 2;
  201. player->dx = -1;
  202. player->direction = PLAYER_LEFT;
  203. game_context->user_input = GameKeyLeft;
  204. }
  205. if (input.held & GameKeyRight)
  206. {
  207. pos.x += 2;
  208. player->dx = 1;
  209. player->direction = PLAYER_RIGHT;
  210. game_context->user_input = GameKeyRight;
  211. }
  212. // Clamp the player's position to stay within world bounds
  213. pos.x = CLAMP(pos.x, WORLD_WIDTH - 5, 5);
  214. pos.y = CLAMP(pos.y, WORLD_HEIGHT - 5, 5);
  215. // Update player position
  216. entity_pos_set(self, pos);
  217. // switch levels if holding OK
  218. if (input.held & GameKeyOk)
  219. {
  220. // if all enemies are dead, allow the "OK" button to switch levels
  221. // otherwise the "OK" button will be used to attack
  222. if (game_context->enemy_count == 0)
  223. {
  224. FURI_LOG_I(TAG, "Switching levels");
  225. save_player_context(player);
  226. game_manager_next_level_set(manager, get_next_level(manager));
  227. furi_delay_ms(500);
  228. is_new_level = true;
  229. }
  230. else
  231. {
  232. game_context->user_input = GameKeyOk;
  233. // furi_delay_ms(100);
  234. }
  235. return;
  236. }
  237. // If the player is not moving, retain the last movement direction
  238. if (player->dx == 0 && player->dy == 0)
  239. {
  240. player->dx = prev_dx;
  241. player->dy = prev_dy;
  242. player->state = PLAYER_IDLE;
  243. game_context->user_input = -1; // reset user input
  244. }
  245. else
  246. {
  247. player->state = PLAYER_MOVING;
  248. }
  249. // Handle back button to stop the game
  250. if (input.pressed & GameKeyBack)
  251. {
  252. game_manager_game_stop(manager);
  253. }
  254. }
  255. static void player_render(Entity *self, GameManager *manager, Canvas *canvas, void *context)
  256. {
  257. // Get player context
  258. UNUSED(manager);
  259. PlayerContext *player = context;
  260. // Get player position
  261. Vector pos = entity_pos_get(self);
  262. // Draw background (updates camera_x and camera_y)
  263. draw_background(canvas, pos);
  264. // Draw player sprite relative to camera, centered on the player's position
  265. canvas_draw_sprite(
  266. canvas,
  267. player->direction == PLAYER_RIGHT ? player->sprite_right : player->sprite_left,
  268. pos.x - camera_x - 5, // Center the sprite horizontally
  269. pos.y - camera_y - 5 // Center the sprite vertically
  270. );
  271. // draw username over player's head
  272. // draw_username(canvas, pos, player->username);
  273. }
  274. const EntityDescription player_desc = {
  275. .start = NULL, // called when entity is added to the level
  276. .stop = NULL, // called when entity is removed from the level
  277. .update = player_update, // called every frame
  278. .render = player_render, // called every frame, after update
  279. .collision = NULL, // called when entity collides with another entity
  280. .event = NULL, // called when entity receives an event
  281. .context_size = sizeof(PlayerContext), // size of entity context, will be automatically allocated and freed
  282. };
  283. static SpriteContext *sprite_generic_alloc(const char *id, uint8_t width, uint8_t height)
  284. {
  285. SpriteContext *ctx = malloc(sizeof(SpriteContext));
  286. if (!ctx)
  287. {
  288. FURI_LOG_E("Game", "Failed to allocate SpriteContext");
  289. return NULL;
  290. }
  291. snprintf(ctx->id, sizeof(ctx->id), "%s", id);
  292. ctx->width = width;
  293. ctx->height = height;
  294. snprintf(ctx->right_file_name, sizeof(ctx->right_file_name), "player_right_%s_%dx%dpx.fxbm", id, width, height);
  295. snprintf(ctx->left_file_name, sizeof(ctx->left_file_name), "player_left_%s_%dx%dpx.fxbm", id, width, height);
  296. return ctx;
  297. }
  298. SpriteContext *get_sprite_context(const char *name)
  299. {
  300. if (strcmp(name, "axe") == 0)
  301. {
  302. return sprite_generic_alloc("axe", 15, 11);
  303. }
  304. else if (strcmp(name, "bow") == 0)
  305. {
  306. return sprite_generic_alloc("bow", 13, 11);
  307. }
  308. else if (strcmp(name, "naked") == 0)
  309. {
  310. return sprite_generic_alloc("naked", 10, 10);
  311. }
  312. else if (strcmp(name, "sword") == 0)
  313. {
  314. return sprite_generic_alloc("sword", 15, 11);
  315. }
  316. // If no match is found
  317. FURI_LOG_E("Game", "Sprite not found: %s", name);
  318. return NULL;
  319. }