million-perfect-letters/src/ui.select.world.a

269 lines
6.6 KiB
Plaintext

;license:MIT
;(c) 2020-2 by 4am
;
; select world screen
;
; Public functions:
; - SelectWorldEventLoop
;
counter = $F2
selectedworld = $F3
; Codes returned by event handlers
kStayOnSelectWorld = 0
kReturnToMainMenu = 1
kSelectWorldKeys ; must keep in sync with kSelectWorldKeyHandlersLo/Hi arrays
; except for last byte ($00) which doesn't need an associated handler
!byte $8D ; Return
!byte $8B ; up arrow
!byte $88 ; left arrow
!byte $8A ; down arrow
!byte $95 ; right arrow
!byte $9B ; Esc
!byte $00
kSelectWorldKeyHandlersLo
!byte <SelectWorldEventReturn
!byte <SelectWorldEventUp
!byte <SelectWorldEventUp
!byte <SelectWorldEventDown
!byte <SelectWorldEventDown
!byte <SelectWorldEventEsc
kSelectWorldKeyHandlersHi
!byte >SelectWorldEventReturn
!byte >SelectWorldEventUp
!byte >SelectWorldEventUp
!byte >SelectWorldEventDown
!byte >SelectWorldEventDown
!byte >SelectWorldEventEsc
;------------------------------------------------------------------------------
; SelectWorldEventLoop
; allow user to select world for playing
;
; in: C clear if screen is already drawn
; (will happen if key was dispatched and handled its own redraw)
; C set if full screen clear & redraw is required
; (will happen if key is pressed during title screen, or
; if user returns to main menu from any other screen)
; out: Z = 0
;------------------------------------------------------------------------------
SelectWorldEventLoop
bcc +
jsr DrawSelectWorldText
+ bit CLEARKBD
@keyloop
lda KBD
bpl @keyloop
bit CLEARKBD
sta gLastKeyPressed
ldx #0
- ldy kSelectWorldKeys, x
beq @keyloop ; key has no associated handler, so ignore it
cpy gLastKeyPressed
beq @dispatch
inx
bne - ; always branches
@dispatch
lda kSelectWorldKeyHandlersLo, x
sta @j+1
lda kSelectWorldKeyHandlersHi, x
sta @j+2
@j jsr $FDFD ; SMC
beq SelectWorldEventLoop
rts
SelectWorldEventReturn
jsr Home
lda selectedworld
sta gWorldID
jsr MaybeLoadWorldFromDisk
bcs SelectWorldEventEsc
jsr LoadProgressFromMemory
; A = next puzzle ID
@Play
sta gPuzzleID
ldx gWorldID
lda kWorldLeftMargins, x
sta GlobalLeftMargin
lda kPuzzleWidths, x
jsr InitPuzzleStorage
jsr InitPuzzleSound
ldx gPuzzleID
jsr LoadPuzzleFromMemory
lda #0
sta gSelectedLogicalColumn
jsr DrawPuzzleChrome
jsr DrawPuzzle
jsr AnimatePuzzleIntoPlace
jsr DrawColumnSelectionIndicator
jsr PlayEventLoop
cpx #kCompletedPuzzle
bne +
jsr AnimatePuzzleCompleted
ldx gPuzzleID
jsr MarkPuzzleCompleted
; A = next puzzle ID
; X = number of puzzles completed
jsr MaybeShowInterstitial
bcc @Play
bcs @DonePlay
+
cpx #kRequestedSkip
bne +
ldx gPuzzleID
jsr MarkPuzzleSkipped
jmp @Play
+
cpx #kRequestedRestart
bne @DonePlay
jsr Home
lda gPuzzleID
jmp @Play
@DonePlay
ldx #kStayOnSelectWorld
sec
rts
SelectWorldEventEsc
ldx #kReturnToMainMenu
sec
rts
SelectWorldEventUp
jsr RedrawPreviouslySelectedWorld
ldx selectedworld
bne +
ldx #12
+ dex
stx selectedworld
jsr DrawNewlySelectedWorld
ldx #kStayOnSelectWorld
clc
rts
SelectWorldEventDown
jsr RedrawPreviouslySelectedWorld
ldx selectedworld
cpx #11
bne +
ldx #$FF
+ inx
stx selectedworld
jsr DrawNewlySelectedWorld
ldx #kStayOnSelectWorld
clc
rts
DrawSelectWorldText
jsr Home
jsr UpdateWorldPercents
+LDADDR kWorldDescriptions
+ST16 $FE
lda #0
sta counter
sta selectedworld
lda #6
sta VTAB
- lda #10
sta HTAB
+LD16 $FE
ldx counter
cpx selectedworld
beq +
ldx #20
jsr DrawHeavySilkBuffer
jmp ++
+ ldx #20
jsr DrawHeavySilkBufferInverse
++ inc VTAB
lda $FE
clc
adc #20
sta $FE
bcc +
inc $FF
+ inc counter
lda counter
cmp #12
bcc -
+PRINT_AT sWorldHelp, 23, 0
rts
RedrawPreviouslySelectedWorld
lda selectedworld
tax
clc
adc #6
sta VTAB
lda #10
sta HTAB
jsr GetWorldDescription
+LD16 $FE
ldx #20
jmp DrawHeavySilkBuffer
DrawNewlySelectedWorld
lda selectedworld
tax
clc
adc #6
sta VTAB
lda #10
sta HTAB
jsr GetWorldDescription
+LD16 $FE
ldx #20
jmp DrawHeavySilkBufferInverse
GetWorldDescription
; in: X = 0-based world ID
; out: $FE/$FF points to world description buffer
+LDADDR kWorldDescriptions
+ST16 $FE
jmp +
- lda $FE
clc
adc #20
sta $FE
bcc +
inc $FF
+ dex
bpl -
rts
worldindex=$FB
UpdateWorldPercents
+LDADDR kWorldDescriptions
+ST16 $FC
lda #0
sta worldindex
-- jsr FindPackedProgress
ldy #$0E
lda ($FE), y
tax
lda #$20
jsr ToASCIIString
ldx #2
ldy #17
- lda $F2, x
sta ($FC), y
dey
dex
bpl -
lda $FC
clc
adc #20
sta $FC
bcc +
inc $FD
+ inc worldindex
lda worldindex
cmp #12
bne --
rts