lol its in c
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "player.h"
  2. #include <string.h>
  3. #include "actor.h"
  4. #include "game.h"
  5. #include "sdk/assets.h"
  6. #include "sdk/joypad.h"
  7. #define PLAYER_SPEED 2
  8. #define GRAVITY 0x002e //!< 0.18
  9. #define PLAYER_INIT_JUMP_VY 0x0399 //!< 3.6
  10. #define PLAYER_INIT_FALL_VY 0xff1a //!< -1.1015625
  11. #define MAX_VY 0xf900 //!< -7.0
  12. #define SWITCH_ANIM_STATE(state) \
  13. do { \
  14. PLAYER_ACTOR->anim = (state); \
  15. PLAYER_ACTOR->frame_counter = 0; \
  16. PLAYER_ACTOR->frame_idx = 0; \
  17. } while (0)
  18. player_t PLAYER;
  19. ASSET(player_tiles, "player.2bpp");
  20. ASSET(player_map, "player.map");
  21. void player_init(void) {
  22. PLAYER.state = PLAYER_STATE_STAND;
  23. memcpy(VRAM_TILE_PTR(TILE_INDEX_PLAYER), &player_tiles[0],
  24. player_tiles_end - player_tiles);
  25. }
  26. void player_update(void) {
  27. if ((joypad_pressed & PAD_UP) && PLAYER.state != PLAYER_STATE_JUMP) {
  28. SWITCH_ANIM_STATE(ANIM_JUMP);
  29. PLAYER.state = PLAYER_STATE_JUMP;
  30. PLAYER.vy = PLAYER_INIT_JUMP_VY;
  31. }
  32. if (PLAYER.state == PLAYER_STATE_JUMP) {
  33. PLAYER.vy -= GRAVITY;
  34. *((uint16_t*)&PLAYER.y_lo) -= PLAYER.vy;
  35. if (player_bg_collides()) {
  36. if (PLAYER.vy & (1 << 15)) {
  37. PLAYER.y_hi--;
  38. while (player_bg_collides()) {
  39. PLAYER.y_hi--;
  40. }
  41. SWITCH_ANIM_STATE(ANIM_STAND);
  42. PLAYER.state = PLAYER_STATE_STAND;
  43. } else {
  44. PLAYER.y_hi++;
  45. while (player_bg_collides()) {
  46. PLAYER.y_hi++;
  47. }
  48. }
  49. PLAYER.vy = 0;
  50. }
  51. }
  52. if ((joypad_state & PAD_RIGHT) && PLAYER.x + TILE_WIDTH * 2 < SCRN_X) {
  53. PLAYER.dir = DIR_RIGHT;
  54. PLAYER.x += PLAYER_SPEED;
  55. if (player_bg_collides()) {
  56. PLAYER.x -= PLAYER_SPEED;
  57. }
  58. } else if ((joypad_state & PAD_LEFT) && PLAYER.x > 0) {
  59. PLAYER.dir = DIR_LEFT;
  60. PLAYER.x -= PLAYER_SPEED;
  61. if (player_bg_collides()) {
  62. PLAYER.x += PLAYER_SPEED;
  63. }
  64. }
  65. if (player_in_air() && PLAYER.state != PLAYER_STATE_JUMP) {
  66. PLAYER.state = PLAYER_STATE_JUMP;
  67. PLAYER.vy = PLAYER_INIT_FALL_VY;
  68. }
  69. PLAYER_ACTOR->flip = PLAYER.dir == DIR_LEFT;
  70. PLAYER_ACTOR->x = PLAYER.x;
  71. PLAYER_ACTOR->y = PLAYER.y_hi;
  72. }