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.

129 lines
1.9 KiB

2 years ago
  1. INCLUDE "oam.inc"
  2. SECTION "Collision", ROM0
  3. ; Determines if player has hit the background
  4. ; @return NZ = false, Z = true
  5. player_bg_collides::
  6. ; c = x % 8 == 0 ? x/8 : x/8 + 1
  7. ld a, [PLAYER_X]
  8. ld c, a
  9. and %111
  10. jr z, .skip_inc_c
  11. ld a, c
  12. add 8
  13. ld c, a
  14. .skip_inc_c:
  15. srl c
  16. srl c
  17. srl c
  18. ; b = y % 8 == 0 ? y/8 : y/8 + 1
  19. ld a, [PLAYER_Y]
  20. ld b, a
  21. and %111
  22. jr z, .skip_inc_b
  23. ld a, b
  24. add 8
  25. ld b, a
  26. .skip_inc_b:
  27. srl b
  28. srl b
  29. srl b
  30. ; top left
  31. call can_move_to
  32. ret nz
  33. ; top right
  34. inc c
  35. call can_move_to
  36. ret nz
  37. ; bottom left
  38. dec c
  39. inc b
  40. call can_move_to
  41. ret nz
  42. ; bottom right
  43. inc c
  44. call can_move_to
  45. ret
  46. ; Check if the player is in mid-air
  47. ; @note NZ = false, Z = true
  48. player_in_air::
  49. ; c = x % 8 == 0 ? x/8 : x/8 + 1
  50. ld a, [PLAYER_X]
  51. ld c, a
  52. and %111
  53. jr z, .skip_inc_c
  54. ld a, c
  55. add 8
  56. ld c, a
  57. .skip_inc_c:
  58. srl c
  59. srl c
  60. srl c
  61. ; b = y % 8 == 0 ? y/8 : y/8 + 1
  62. ld a, [PLAYER_Y]
  63. ld b, a
  64. and %111
  65. jr z, .skip_inc_b
  66. ld a, b
  67. add 8
  68. ld b, a
  69. .skip_inc_b:
  70. srl b
  71. srl b
  72. srl b
  73. ; check underneath player (2 tiles down from top-left sprite)
  74. inc b
  75. inc b
  76. call can_move_to
  77. ret nz
  78. inc c
  79. call can_move_to
  80. ret
  81. ; Check whether the location can be moved to or not
  82. ; @param b y-coordinate
  83. ; @param c x-coordinate
  84. ; @destroy a, d, e
  85. ; @note NZ = false, Z = true
  86. can_move_to:
  87. ; todo: should be aware of scx/scy
  88. push bc
  89. push hl
  90. ld a, [CURRENT_MAP_COLLISION]
  91. ld h, a
  92. ld a, [CURRENT_MAP_COLLISION + 1]
  93. ld l, a
  94. ld a, [CURRENT_MAP_WIDTH]
  95. ld d, 0
  96. ld e, a
  97. ; compute map index (HL + B * (CURRENT_MAP_WIDTH + 1) + C)
  98. .mul_y_width:
  99. add hl, de
  100. dec b
  101. jr nz, .mul_y_width
  102. add hl, bc
  103. ; check [hl] = 1
  104. ld a, [hl]
  105. cp 1
  106. pop hl
  107. pop bc
  108. ret