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.

165 lines
2.4 KiB

2 years ago
  1. INCLUDE "oam.inc"
  2. SECTION "Collision", ROM0
  3. DEF COLLF_WALK EQU (1 << 0)
  4. DEF COLLF_LADDER EQU (1 << 1)
  5. DEF COLLF_PORTAL EQU (1 << 2)
  6. ; Determines if player has hit the background
  7. ; @return NZ = false, Z = true
  8. player_bg_collides::
  9. ; C = MAP X = PLAYER X / 8 + CAMERA X
  10. ld a, [PLAYER_X]
  11. call get_tile_coord
  12. ld c, a
  13. ld a, [CURRENT_CAMERA_X]
  14. add c
  15. ld c, a
  16. ; C < 0
  17. ld a, c
  18. cpl
  19. bit 7, a
  20. ret z
  21. ; C >= MAP WIDTH
  22. ld a, [CURRENT_MAP_WIDTH]
  23. cp c
  24. jr nz, .load_y
  25. ret
  26. .load_y:
  27. ; B = MAP Y = PLAYER Y / 8 + CAMERA Y
  28. ld a, [PLAYER_Y]
  29. call get_tile_coord
  30. ld b, a
  31. ld a, [CURRENT_CAMERA_Y]
  32. add b
  33. ld b, a
  34. ; B < 0
  35. ld a, b
  36. cpl
  37. bit 7, a
  38. ret z
  39. ; B >= MAP HEIGHT
  40. ld a, [CURRENT_MAP_HEIGHT]
  41. cp b
  42. jr nz, .check_tiles
  43. ; Set NZ
  44. or $ff
  45. ret
  46. .check_tiles:
  47. ; top left
  48. call can_move_to
  49. ret z
  50. ; top right
  51. inc c
  52. call can_move_to
  53. ret z
  54. ; bottom left
  55. dec c
  56. inc b
  57. call can_move_to
  58. ret z
  59. ; bottom right
  60. inc c
  61. call can_move_to
  62. ret
  63. ; Check if the player is in mid-air
  64. ; @note NZ = true, Z = false
  65. player_in_air::
  66. ; c = x % 8 == 0 ? x/8 : x/8 + 1
  67. ld a, [PLAYER_X]
  68. call get_tile_coord
  69. ld c, a
  70. ld a, [CURRENT_CAMERA_X]
  71. add c
  72. ld c, a
  73. ld a, [PLAYER_Y]
  74. call get_tile_coord
  75. ld b, a
  76. ld a, [CURRENT_CAMERA_Y]
  77. add b
  78. ld b, a
  79. ; check underneath player (2 tiles down from top-left sprite)
  80. inc b
  81. inc b
  82. call can_move_to
  83. ret z
  84. inc c
  85. call can_move_to
  86. ret
  87. ; Check whether the location can be moved to or not
  88. ; @param b y-coordinate
  89. ; @param c x-coordinate
  90. ; @destroy a, d, e
  91. ; @note NZ = true, Z = false
  92. can_move_to:
  93. push bc
  94. push hl
  95. ld a, [CURRENT_MAP_COLLISION]
  96. ld l, a
  97. ld a, [CURRENT_MAP_COLLISION + 1]
  98. ld h, a
  99. ld a, [CURRENT_MAP_WIDTH]
  100. ld d, 0
  101. ld e, a
  102. ; compute map index (HL + B * (CURRENT_MAP_WIDTH + 1) + C)
  103. .mul_y_width:
  104. add hl, de
  105. dec b
  106. jr nz, .mul_y_width
  107. add hl, bc
  108. ld a, [hl]
  109. and COLLF_WALK
  110. pop hl
  111. pop bc
  112. ret
  113. ; Convert from pixel space to tile space
  114. ; @param a Screen pixel coordinate
  115. ; @destroys hl
  116. ; @returns Screen tile coordinate in A
  117. get_tile_coord:
  118. ld hl, sp - 1
  119. ld [hl], a
  120. and %111
  121. ld a, [hl]
  122. jr z, .skip_inc
  123. add 8
  124. .skip_inc:
  125. srl a
  126. srl a
  127. srl a
  128. ret