-
Notifications
You must be signed in to change notification settings - Fork 1
/
movement.asm
executable file
·177 lines (168 loc) · 2.8 KB
/
movement.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
// Handles updating player X, Y coordinates, facing
// and collision detection
//.pc = $7500 "Movement routines"
.pc = viewhandlerEnd+1 "Movement routines"
// Changes current facing
// Carry set -> turn around
// A = 1 -> turn left
// A = 2 -> turn left
// Facing goes like this:
// 1 = north, 2 = west, 3 = south, 4 = east
// So pressing left increases, until at 5 it is reset back to 1
// and pressing right decreases, until at 0 it is set to 4.
// Turning around either increases or decreases by 2.
turnPlayer:
{
bcs turnAround
cmp #01
beq turnLeft
turnRight:
dec playerFacing
bne doneTurner // Not zero yet, move along
lda #$04 // north -> east
sta playerFacing
rts
turnLeft:
inc playerFacing
clc
lda playerFacing
cmp #$05 // Man, is it five already ?
bcc doneTurner
lda #$01 // yes, change from east -> north
sta playerFacing
rts
turnAround:
lda playerFacing
cmp #$04
beq decFace // Turn west
cmp #$03 // now why the hell didn't bcc work here?
beq decFace // Turn north
inc playerFacing
inc playerFacing
rts
decFace:
dec playerFacing
dec playerFacing
doneTurner:
rts
}
// Check players facing and move to correct direction
// Returns with carry clear if moved
movePlayer:
{
lda playerFacing
cmp #01 // North, decrease Y
beq north
cmp #02 // West, decrease X
beq west
cmp #03 // South, increase Y
beq south
east: jsr moveEast // must be 04, move east
rts
north: jsr moveNorth
rts
west: jsr moveWest
rts
south: jsr moveSouth
rts
}
// Moves player north, decrease Y
moveNorth:
{
ldx playerX
ldy playerY
dey
jsr CheckCollision
// bcs northCollides // Dont update coordinates if collision detected
cmp #01
beq northCollides
dec playerY
clc
rts
northCollides:
sec
rts
}
// Moves player east, increase X
moveEast:
{
ldx playerX
ldy playerY
inx
jsr CheckCollision
// bcs eastCollides
cmp #01
beq eastCollides
inc playerX // East, increase X
clc
rts
eastCollides:
sec
rts
}
// Moves player west, decrease X
moveWest:
{
ldx playerX
ldy playerY
dex
jsr CheckCollision
// bcs westCollides
cmp #01
beq westCollides
dec playerX
clc
rts
westCollides:
sec
rts
}
// Moves player south, increase Y
moveSouth:
{
ldx playerX
ldy playerY
iny
jsr CheckCollision
// bcs southCollides
cmp #01
beq southCollides
inc playerY
clc
rts
southCollides:
sec
rts
}
// *** Takes coordinates given in X, Y registers
// Returns with carry set if collision is detected,
// otherwise returns with carry clear
// Also returns element on map in A
CheckCollision:
{
cpy #00 // Row 0 always has walls
beq collision
cpx #00 // Column 0 always has walls
beq collision
stx Xposition
lda #00
clc
getRow: adc #09
dey
bne getRow
firstRow:
adc Xposition
tax
lda mapdata,x
beq noCollision
collision:
lda mapdata,x
sec
rts
noCollision:
clc
rts
Xposition:
.byte $00
}
movementEnd: nop