-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine.asm
392 lines (345 loc) · 5.53 KB
/
statemachine.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
.enum { TITLE, WAITING, FALLING, WALKING_RIGHT, WALKING_LEFT, ZIP_UP, ZIP_DOWN }
jumptable:
.word Title, Waiting, Falling, WalkingRight, WalkingLeft, ZipUp, ZipDown
statemachine:
lda currentstate
clc
rol
tax
lda jumptable, x
sta j+1
lda jumptable+1, x
sta j+2
j: jmp $0000
////////////////////////////////////////
Title: {
lda #WAITING
sta currentstate
rts
}
////////////////////////////////////////
//
// ZIP UP
//
.label zipwait = 14
zipdelay: .byte 25
ziptarget: .byte 0
ZipUp: {
dec zipdelay
bne wait
lda ziptarget
sta player_y
lda #WAITING
sta currentstate
jmp statemachine
wait:
rts
Start:
lda get_collision_result_x
sta find_portal_x
lda get_collision_result_y
sta find_portal_y
lda #$ff
sta find_portal_direction
jsr find_portal
lda find_portal_result
cmp #$ff
beq cancel
clc
rol
rol
rol
adc #$25
sta ziptarget
lda #$6
jsr start_animation
lda #ZIP_UP
sta currentstate
lda #zipwait
sta zipdelay
rts
cancel:
lda #WAITING
sta currentstate
rts
}
////////////////////////////////////////
//
// ZIP DOWN
//
ZipDown: {
dec zipdelay
bne wait
lda ziptarget
sta player_y
lda #WAITING
sta currentstate
jmp statemachine
wait:
rts
Start:
lda get_collision_result_x
sta find_portal_x
lda get_collision_result_y
sta find_portal_y
lda #$1
sta find_portal_direction
jsr find_portal
lda find_portal_result
cmp #$ff
beq cancel
clc
rol
rol
rol
adc #$25
sta ziptarget
lda #$6
jsr start_animation
lda #ZIP_DOWN
sta currentstate
lda #zipwait
sta zipdelay
rts
cancel:
lda #WAITING
sta currentstate
rts
}
////////////////////////////////////////
//
// WALKING RIGHT
//
WalkingRight: {
lda #$3
jsr start_animation
lda $dc00
and #%1000
beq walking
lda #WAITING
sta currentstate
jmp statemachine
walking:
jsr checkwin
beq notr
// would collide?
ldx #$08
ldy #$00
jsr get_player_collision
jsr pickup_loot
lda get_collision_result
cmp #$10
beq notr
// move right
lda player_x
clc
adc #$01
sta player_x
lda player_x+1
adc #$00
sta player_x+1
// are we not on ground?
ldx #$00
ldy #$08
jsr get_player_collision
cmp #$10
beq nofall
cmp #$60
beq nofall
ldx #$07
ldy #$08
jsr get_player_collision
cmp #$10
beq nofall
cmp #$60
beq nofall
lda #$5
jsr start_animation
lda #FALLING
sta currentstate
jmp statemachine
nofall:
notr:
rts
}
////////////////////////////////////////
//
// WALKING LEFT
//
WalkingLeft: {
lda #$2
jsr start_animation
lda $dc00
and #%100
beq walking
lda #WAITING
sta currentstate
jmp statemachine
walking:
jsr checkwin
beq notl
// would collide?
ldx #$01
ldy #$00
jsr get_player_collision_left
jsr pickup_loot
lda get_collision_result
cmp #$10
beq notl
// move left
lda player_x
sec
sbc #$01
sta player_x
lda player_x+1
sbc #$00
sta player_x+1
// are we not on ground?
ldx #$00
ldy #$08
jsr get_player_collision
cmp #$10
beq nofall
cmp #$60
beq nofall
ldx #$07
ldy #$08
jsr get_player_collision
cmp #$10
beq nofall
cmp #$60
beq nofall
lda #$4
jsr start_animation
lda #FALLING
sta currentstate
jmp statemachine
nofall:
notl:
rts
}
////////////////////////////////////////
//
// FALLING
//
fall_table: .byte 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2
.label fall_table_length = *-fall_table-1
Falling: {
// did we hit ground?
ldx #$00
ldy #$08
jsr get_player_collision
jsr pickup_loot
cmp #$10
beq nofall
ldx #$07
ldy #$08
jsr get_player_collision
jsr pickup_loot
cmp #$10
beq nofall
// do the fall
ldx fall_index
cpx #fall_table_length
beq !+
inx
stx fall_index
!: lda fall_table, x
clc
adc player_y
sta player_y
jmp endfall
nofall:
lda #0
sta fall_index
// floor
lda get_collision_result_y
clc
rol
rol
rol
adc #$25-8
sta player_y
lda #WAITING
sta currentstate
jmp statemachine
endfall:
rts
}
////////////////////////////////////////
//
// WAITING
//
Waiting: {
lda player_anim
and #$01
jsr start_animation
// hold right?
lda $dc00
and #%1000
bne notr
lda #WALKING_RIGHT
sta currentstate
jmp statemachine
notr:
// hold left?
lda $dc00
and #%100
bne notl
lda #WALKING_LEFT
sta currentstate
jmp statemachine
notl:
// hold up?
lda $dc00
and #%0001
bne notu
ldx #$03
ldy #$04
jsr get_player_collision
cmp #$20 // up zip
bne notu
jmp ZipUp.Start
notu:
// hold down?
lda $dc00
and #%0010
bne notd
ldx #$03
ldy #$04
jsr get_player_collision
cmp #$20 // down zip
bne notd
jmp ZipDown.Start
notd:
rts
Start:
lda #WAITING
sta currentstate
jmp statemachine
}
checkwin: {
ldx #$03
ldy #$03
jsr get_player_collision
cmp #$50
bne nowin
hide: {
ldx #0
reveal:
stx reload_x+1
jsr render_blinds
reload_x:
ldx #00
inx
cpx #11+16
bne reveal
}
inc current_level
jsr show_level
lda #WAITING
sta currentstate
lda #$00
nowin:
rts
}