-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_collision.asm
194 lines (157 loc) · 2.86 KB
/
detect_collision.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
.export DetectCollisionInit
.export DetectCollisionWithGround
.export DetectCollisionWithWallLeft
.export DetectCollisionWithWallRight
.include "include.branch-macros.asm"
.include "include.mov-macros.asm"
.import collision_map
.importzp values
pos_v = values + $00
pos_h = values + $01
pos_screen = values + $02
row = values + $03
corner = values + $04
upper = values + $05
collide_type = values + $06
COLLIDE_TYPE_GROUND = $55
COLLIDE_TYPE_WALL = $aa
; DEBUGGING ONLY
debug_00_row = $600 ; Row number
debug_01_upper = $601 ; Upper byte (left or right half)
debug_02_col = $602 ; Column number
debug_03_index = $603 ; Index into collison map (row | column | upper)
debug_04_collide = $604 ; Byte at the collison map
.segment "CODE"
.proc DetectCollisionInit
rts
.endproc
; Carry set = standing on ground
; Carry clear = in air
.proc DetectCollisionWithGround
sta pos_screen
sty pos_v
stx pos_h
mov collide_type, #COLLIDE_TYPE_GROUND
; (Y + 0x20) = bottom of player
; (Y + 0x18) = offset by a single tile
; (Y + 0x18) / 16 = block_y at bottom of player
; (Y + 0x18) / 16 * 16 = row of collision_map
lda pos_v
clc
adc #$1e
and #$f0
sta row
sta debug_00_row
; Check lower-left corner.
lda pos_h
clc
adc #$3
sta corner
jsr CheckCollisionCorner
bcs Success
; Check lower-right corner.
lda pos_h
clc
adc #$9
sta corner
jsr CheckCollisionCorner
bcs Success
Failure:
clc
rts
Success:
lda row
sec
sbc #$18
sec
rts
.endproc
.proc DetectCollisionWithWallLeft
sta pos_screen
sty pos_v
stx pos_h
mov collide_type, #COLLIDE_TYPE_WALL
; (Y + 0x20) = bottom of player
lda pos_v
clc
adc #$10
and #$f0
sta row
; Check lower-left corner.
lda pos_h
sta corner
jsr CheckCollisionCorner
bcs Success
Failure:
clc
rts
Success:
sec
rts
.endproc
.proc DetectCollisionWithWallRight
sta pos_screen
sty pos_v
stx pos_h
mov collide_type, #COLLIDE_TYPE_WALL
; (Y + 0x20) = bottom of player
lda pos_v
clc
adc #$10
and #$f0
sta row
; Check lower-left corner.
lda pos_h
clc
adc #$10
sta corner
jsr CheckCollisionCorner
bcs Success
Failure:
clc
rts
Success:
sec
rts
.endproc
.proc CheckCollisionCorner
lda pos_screen
adc #0
and #$01
.repeat 3
asl a
.endrepeat
sta upper
sta debug_01_upper
; X / 0x20 = column, which byte to lookup in the row (0..7)
lda corner
.repeat 5
lsr a
.endrepeat
sta debug_02_col
; Combine with upper (left or right half) and row number.
ora upper
ora row
sta debug_03_index
tax
; (X / 8) % 4 = Offset into that byte
lda corner
.repeat 3
lsr a
.endrepeat
and #$03
tay
lda collision_map,x
sta debug_04_collide
and bit_mask,y
and collide_type
beq Failure
Success:
sec
rts
Failure:
clc
rts
.endproc
bit_mask:
.byte $03,$0c,$30,$c0