-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgame.asm
2074 lines (1728 loc) · 36.5 KB
/
game.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; asmsyntax=ca65
; TODO:
; Fall into pitfall.
FIRE_SPAWN_DELAY = 8
; Game Meta Tiles
.enum MetaTileTypes
BACKGROUND
GROUND
GROUND2
PIT_LEFT
PIT_RIGHT
PIT_LEFT_BOTTOM
PIT_RIGHT_BOTTOM
NOTHIN
OBS
PIT
FIREHYDRANT
TRUCK00
TRUCK01
TRUCK02
TRUCK03
GARBO00
GARBO01
.endenum
Game_Init:
jsr MMC1_Setup_Vert
lda #PPU_CTRL_HORIZ
sta $2000
lda #<PalBG3
sta TmpAddr
lda #>PalBG3
sta TmpAddr+1
jsr LoadPaletteBG3
; Initialize a bunch of variables
lda #0
sta column_ready
sta StatusDrawn
;sta meta_column_offset
sta meta_tile_addr
sta TmpAttr
;sta meta_cols_to_buffer
;sta meta_last_buffer
sta sfxStart_Fall
sta sfxStart_Jump
sta SkyScroll
sta SkyScroll+1
sta game_paused
lda #SKYLINE_SCROLL_SPEED
sta SScrollNext
; Status bar stuff
; Generate and draw first screen of columns (plus a few)
lda #PPU_CTRL_VERT
sta $2000
bit GameFullInit
bvc @skip_fullinit
lda #0
sta GameFullInit
jsr game_FullInit
@skip_fullinit:
; Scroll to the start of the first screen
lda #0
sta $2005
sta $2005
ldx #PPU_CTRL_VERT
stx $2000
; More init stuff
sta PlayerScore0
sta PlayerScore1
sta PlayerScore2
sta PlayerScore3
sta rngFlipFlop
sta calc_scroll
lda #$F0
ldx #7
: sta PlayerScoreText, x
dex
bpl :-
@scoredone:
; prepare sprite zero
lda #88
sta spritezero
lda #$0F
sta spritezero+1
lda #%00100001
sta spritezero+2
lda #88
sta spritezero+3
; Setup the player sprite
; Y, idx, attr, X
lda #<PlayerSprite_Frame0
sta TmpAddr
lda #>PlayerSprite_Frame0
sta TmpAddr+1
lda #$5E
sta TmpY ; Y coordinate
lda #$10
sta TmpX ; X coordinate
ldx #0 ; byte offset in sprite memory
ldy #0 ; Tile index for sprite definition
sty TmpCounter
@spLoop:
; y
lda TmpY
sta sprites, x
inx
clc
adc #8
sta TmpY
; idx
lda (TmpAddr), y
sta sprites, x
inx
inc TmpAddr
; Handle overflow
bne :+
inc TmpAddr+1
:
inc TmpCounter
; attr
lda #$00
sta sprites, x
inx
; X
lda TmpX
sta sprites, x
inx
; test for second column
lda TmpCounter
cmp #$04
bne @noTmpWrap
lda #$04
sta TmpCounter
lda #$5E
sta TmpY
lda TmpX
clc
adc #$08
sta TmpX
@noTmpWrap:
cpx #32
bne @spLoop
; end of player sprite
lda #PlayerSprite_AnimSpeed
sta PlayerNextFrameIn
lda #$FF
sta PlayerSpriteFrame
; Prepare "Paused" sprites. Position them and give them attributes,
; but set the tiles to a blank space.
ldx #0
ldy #0
lda #PAUSED_X
@pyloop:
sta sprites+35, x
pha ; save A
lda #PAUSED_ATTR
sta sprites+34, x
lda #PAUSED_Y
sta sprites+32, x
lda #$00
sta sprites+33, x
pla ; load A
clc
adc #8
inx
inx
inx
inx
iny
cpx #4*5
bne @pyloop
; initial value for fading paused palette
lda #$20
sta PAUSED_PAL
; Init the first 16 columns
; TODO: move these somewhere else
ldx #0
:
lda #$00
sta meta_columns, x
inx
sta meta_columns, x
inx
lda #$01
sta meta_columns, x
inx
sta meta_columns, x
inx
cpx #16*4
bne :-
; Clear the buffer and buffer addresses for the attributes
lda #0
sta attr_buffer
sta attr_buffer+1
sta attr_address
sta attr_address+1
sta attr_odd
; Clear the new fire metasprite countdown
lda #$FF
sta NewFireIn
lda #0
sta FireAnimIdx
sta FireAnimCount
sta calc_scroll
sta meta_column_offset
sta last_meta_offset
lda #FIRE_ANIM_SPEED
sta FireAnimNext
; Clear drawing old fire sprites
lda #$FF
ldx #0
:
sta FireAnimFrame, x
inx
cpx #8
bne :-
; Set frame and NMI pointers
lda #<Game_Frame
sta DoFramePointer
lda #>Game_Frame
sta DoFramePointer+1
lda #<Game_NMI
sta DoNMIPointer
lda #>Game_NMI
sta DoNMIPointer+1
lda rng_seed
and #1
clc
adc #1
.ifdef NTSC
ldx #0
.else
ldx #1
.endif
jsr snd_LOAD
rts
game_FullInit:
inc SkipNMI
lda #PPU_MASK_OFF
sta $2001
lda #<GamePalette
sta PaletteAddr
lda #>GamePalette
sta PaletteAddr+1
jsr LoadPalettes
lda #BackgroundThemes::City
sta BGTheme
lda #$20
sta BGNametable
lda #$00
sta BGYStart
jsr DrawBackground
lda #BackgroundThemes::City
sta BGTheme
lda #$24
sta BGNametable
lda #$00
sta BGYStart
jsr DrawBackground
; Update attribute data
lda #$23
sta $2006
lda #$D8
sta $2006
lda #$AA
jsr game_DrawAttributeRow
lda #$55
jsr game_DrawAttributeRow
lda #$AA
jsr game_DrawAttributeRow
jsr game_DrawAttributeRow
; Attributes for the first two columns on
; the second nametable
lda #$27
sta $2006
lda #$D8
sta $2006
lda #$AA
sta $2007
lda #$27
sta $2006
lda #$E0
sta $2006
lda #$55
sta $2007
lda #$FF
sta meta_last_gen
lda #$00
sta meta_cols_to_buffer
sta meta_last_buffer
sta meta_column_offset
sta column_ready
lda #18
sta obs_countdown
sta TmpZ
@drawWholeMap:
lda meta_cols_to_buffer
bne @buffer
jsr generate_column
@buffer:
jsr Buffer_Column
jsr Draw_Column
dec TmpZ
bne @drawWholeMap
lda #PPU_CTRL_HORIZ
sta $2000
lda #0
sta column_ready
jmp game_ClearStatusBar
;rts
;; End of Game_Init
game_ClearStatusBar:
lda #$22
sta $2006
lda #$80
sta $2006
ldx #10
ldy #$00
@loop:
.repeat 32
sty $2007
.endrepeat
dex
bne @loop
lda #$26
sta $2006
lda #$80
sta $2006
.repeat 32
sty $2007
.endrepeat
rts
game_DrawAttributeRow:
.repeat 8
sta $2007
.endrepeat
rts
Game_NMI:
jsr WritePalettes
jsr WriteSprites
bit StatusDrawn
bmi :+
ldx #PPU_CTRL_HORIZ
stx $2000
jsr WriteScoreLabel
jsr WriteSeedLabel
lda #$FF
sta StatusDrawn
:
; Check to see if a column is buffered. If so, draw it
; to the screen.
bit column_ready
bvc @noDraw ; check if $40 is set
jsr Draw_Column
@noDraw:
bit column_ready
bpl @noAttr
jsr Draw_Attribute
@noAttr:
lda #0
sta column_ready
jsr Draw_Score
jsr WriteSkylineScroll
jmp NMI_Finished
Game_Frame:
jsr UpdateFire
jsr BufferSkylineScroll
; Check for start button presse. Pause if it has been.
lda #BUTTON_START
jsr ButtonPressedP1
beq @nostart
; If already paused, unpause
bit game_paused
bvs @game_is_paused
; Pause game, display "Paused" sprites
jsr g_PausedSprites_On
; TmpX and TmpY are used for cycling the palette for the
; "Paused" text.
lda #0
sta TmpX ; Color index
lda #2
sta TmpY ; Frames until next update
dec game_paused ; set pause state
jmp WaitSpriteZero
; Unpause the game, hide "Paused" sprites
@game_is_paused:
lda #0
sta game_paused
jsr g_PausedSprites_Off
; Pause not pressed
@nostart:
bit game_paused
bvc @game_not_paused
; Game is paused
; "Paused" fade thing
dec TmpY ; Update palette when TmpY is zero.
bne @noColor
lda #8
sta TmpY ; Update platte once every eight frames.
ldx TmpX
lda PausedPalTable, x
sta PAUSED_PAL
inx
cpx #4
; Wrap color index if needed
bne @noWrap
ldx #0
@noWrap:
stx TmpX
@noColor:
jmp WaitSpriteZero
@game_not_paused:
; Is it time for the next frame?
dec PlayerNextFrameIn
bne @noAnimFrame
; Prep the countdown to the next frame
lda #PlayerSprite_AnimSpeed
sta PlayerNextFrameIn
; Inc frame id index
inc PlayerSpriteFrame
; Check for wrap around
lda PlayerSpriteFrame
cmp #PlayerSprite_FrameCount
bne :+
; Wrapped around, load first frame
lda #0
sta PlayerSpriteFrame
:
; Load the frame
jsr UpdatePlayerAnimation
@noAnimFrame:
; increment the screen position
inc calc_scroll
jsr UpdatePlayer
; store previous meta column offset
lda meta_column_offset
sta last_meta_offset
; update meta column offset and generate
; next column if changed
lda calc_scroll
lsr a
lsr a
lsr a
sta meta_column_offset
; Increment the score and generate a new column when
; a meta column boundary is crossed by the player.
cmp last_meta_offset
beq @waitFrame
; Chcek if there is something to buffer
; if so, skip generation
lda meta_cols_to_buffer
bne @needBuffer
lda #1
jsr IncScore
jsr generate_column
@needBuffer:
; If last drawn column is not the same as last generated
; a buffer and draw are needed. This will trigger if the
; last generated thing is more than two columns wide.
jsr Buffer_Column
@waitFrame:
jsr CheckCollide
beq @pit
; Collision == ded
lda #STATES::GS_DED
sta current_gamestate
inc gamestate_changed
@pit:
jsr CheckPit
beq @safe
; TODO: fall in pit
@safe:
jmp WaitSpriteZero
; FIXME: Improve this (again). Allow the player sprite to overlap
; the obstacle metatile by ~2 or so pixels. In other words, give
; each obstacle metatile a ~2 pixel no-kill border.
CheckCollide:
; Find byte offset for current metacolumn to check for collide
lda meta_column_offset
clc
adc #1 ; offeset from left side of screen to check for collide
and #$1F ; i forget why this is here, but i'm keeping it.
asl a
asl a
tax
; Load meta columns into ColCache
ldy #0
@loop:
lda meta_columns, x
sta ColCache, y
inx
iny
lda meta_column_offset
cmp #30 ; check for nametable wrap. not 31 cuz we add 1 above.
bne @nocheck
cpy #4
bne @loop
jmp @Wrapped
@nocheck:
cpy #8
bne @loop
jmp @NoWrap
@Wrapped:
ldx #0
@WLoop:
lda meta_columns, x
sta ColCache, y
inx
iny
cpy #8
bne @WLoop
@NoWrap:
; Check collisions against cache
lda SP_COLLIDE_Y
cmp #$77
bcc @layer2
; Layer 1: the Pit
lda ColCache+3
cmp #MetaTileTypes::OBS
bcc @l1Right
jmp @collide
@l1Right:
lda ColCache+7
cmp #MetaTileTypes::OBS
bcc @layer2
jmp @collide
; Lower obstacle on the ground
@layer2:
lda SP_COLLIDE_Y
cmp #$6A
bcc @layer3
lda ColCache+1
beq @l2Right
jmp @collide
@l2Right:
lda ColCache+5
beq @layer3
jmp @collide
; Upper obstacle on the ground
@layer3:
lda SP_COLLIDE_Y
cmp #$5A
bcc @done
lda ColCache+0
beq @l3Right
jmp @collide
@l3Right:
lda ColCache+4
beq @done
@collide:
lda #1
rts
@done:
lda #0
rts
CheckPit:
rts
; Adds register A to score. Keep it under 100 at a time.
IncScore:
clc
adc PlayerScore0
sta PlayerScore0
cmp #100
bcc BufferScoreDisplay
sec
sbc #100
sta PlayerScore0
inc PlayerScore1
lda PlayerScore1
cmp #100
bcc BufferScoreDisplay
sbc #100
sta PlayerScore1
inc PlayerScore2
lda PlayerScore2
cmp #100
bcc BufferScoreDisplay
sbc #100
sta PlayerScore2
inc PlayerScore3
; Buffer tile changes for score
BufferScoreDisplay: ; this label is used in scores.asm
; Clear text buffer and put the player score in the ones'
; spot for now
ldx #0
ldy #0
@updateLoop:
lda #0
sta PlayerScoreText, y
iny
lda PlayerScoreBase100, x
sta PlayerScoreText, y
iny
inx
cpx #4
bne @updateLoop
; Separate the tens from the ones
ldx #0
@splitBase100:
lda PlayerScoreText+1, x ; load the ones var
cmp #10
bcc @nextNumber
sbc #10
sta PlayerScoreText+1, x
inc PlayerScoreText, x ; inc tens place
jmp @splitBase100
@nextNumber:
inx
inx
cpx #8
bne @splitBase100
; Make the numbers ASCII
@exitLoop:
ldx #0
@ascii: ; not quite ASCII anymore
lda PlayerScoreText, x
ora #$F0
sta PlayerScoreText, x
inx
cpx #8
bne @ascii
rts
; Draw the score to the screen in the status bar thing.
Draw_Score:
lda #PPU_CTRL_HORIZ
sta $2000
lda #$22
sta $2006
lda #$F0
sta $2006
lda PlayerScoreText+0
sta $2007
lda PlayerScoreText+1
sta $2007
lda #$2D
sta $2007
lda PlayerScoreText+2
sta $2007
lda PlayerScoreText+3
sta $2007
lda PlayerScoreText+4
sta $2007
lda #$2D
sta $2007
lda PlayerScoreText+5
sta $2007
lda PlayerScoreText+6
sta $2007
lda PlayerScoreText+7
sta $2007
rts
UpdatePlayer:
lda controller1
and #BUTTON_A
beq @noJump
lda JumpPeak
bne @noJump
lda sfxStart_Jump
bne :+
lda #$FF
sta sfxStart_Jump
lda #0
ldy #4
;jsr LoadSequence
:
; Has the player hit the peak of the jump? (have we run out of jump frames?)
lda PlayerJumpFrame
cmp JumpFrameLength
bcs @setPeak
; Load the frame's address
lda #<PlayerSprite_Frame0
sta TmpAddr
lda #>PlayerSprite_Frame0
sta TmpAddr+1
jsr UpdatePlayerAnimationFrame
; Load the next jump frame
lda PlayerJumpFrame
clc
adc #1
sta PlayerJumpFrame
tax
lda JumpFrames, x
jmp @done
@setPeak:
lda #1
sta JumpPeak
@noJump: ; falling back to ground
lda #1
sta JumpPeak
; Are we on the first jump frame?
lda PlayerJumpFrame
beq @ground
lda sfxStart_Fall
bne :+
lda #$FF
sta sfxStart_Fall
; Start playing the fall sfx
lda #1
ldy #4
;jsr LoadSequence
:
; Load the frame's address
lda #<PlayerSprite_Frame3
sta TmpAddr
lda #>PlayerSprite_Frame3
sta TmpAddr+1
jsr UpdatePlayerAnimationFrame
; Load the previous jump frame (cuz we fallin')
lda PlayerJumpFrame
sec
sbc #1
sta PlayerJumpFrame
tax
lda JumpFrames, x
jmp @done
@ground:
lda #0
sta JumpPeak
sta sfxStart_Fall
sta sfxStart_Jump
lda JumpFrames
;lda #$76
@done:
sec
; Update all the sprite positions
sta sprites+28
sta sprites+12
sbc #8
sta sprites+24
sta sprites+8
sbc #8
sta sprites+20
sta sprites+4
sbc #8
sta sprites+16
sta sprites+0
lda PlayerJumpFrame
beq :+
:
rts
prng:
ldx #8 ; iteration count (generates 8 bits)
lda working_seed
bne @one
;lda seed_ram
@one:
asl a ; shift the register
rol working_seed+1
bcc @two
; Apply XOR feedback whenever a 1 bit is shifted out
eor #$2D
@two:
dex
bne @one ; generate another bit
sta working_seed
;sta seed_ram
lda working_seed+1
;sta seed_ram+1
cmp #0 ; reload flags
sta rng_result
rts
; Get the meta_column_addr given the current
; meta_last_gen value and increment meta_last_gen.
gc_MetaColumnAddrFromOffset:
; Increment for next time.
; Should this happen at the start? (and init to $FF?)
inc meta_last_gen
lda meta_last_gen
; Check overflow and wrap when needed.
cmp #$20
bcc @noWrap
lda #$00
sta meta_last_gen
@noWrap:
; Multiply meta_last_gen by four and add it
; to the map_column_address.
lda meta_last_gen ; reload this because A is clobbered
asl a
asl a
;tax
rts
; Entry point for generating columns
generate_column:
lda obs_countdown
beq @doRngThing
dec obs_countdown
lda #<MetaColumn_Nothin
sta TmpAddr
lda #>MetaColumn_Nothin
sta TmpAddr+1
jmp @gc_LoadMetaColumn
@doRngThing:
lda #5
sta obs_countdown
lda rngFlipFlop
bne @odd
dec rngFlipFlop
jsr prng
lda rng_result
and #$0F
asl a
jmp @idx
@odd:
inc rngFlipFlop
lda rng_result
and #$F0
lsr a
lsr a
lsr a
@idx:
tax
lda MetaColumn_Definitions, x
sta TmpAddr
lda MetaColumn_Definitions+1, x
sta TmpAddr+1
; Load the meta column that's pointed to in the TmpAddr
; into the meta column buffer (not the tile draw buffer).
@gc_LoadMetaColumn:
; load map_column_addr with the correct offset
jsr gc_MetaColumnAddrFromOffset
tax
ldy #0
; Load the width of the column thing
lda (TmpAddr), y
sta meta_cols_to_buffer
iny