Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; whenever calling player.s functions, put the player struct address for the player we are manipulating in the
- ; ZP_ADDRESS
- BODY_MASK_INV = %11110011
- BODY_HEAD_GC = %0000
- BODY_STRT_GC = %0100
- BODY_BEND_GC = %1000
- BODY_TAIL_GC = %1100
- DIR_MASK_INV = %11111100
- DIR_RIGHT_GC = %00
- DIR_DOWN_GC = %01
- DIR_LEFT_GC = %10
- DIR_UP_GC = %11
- change_worm_tile .macro andmask, ormask
- ldy #0
- lda (ZP_ADDRESS),Y
- and #\andmask
- ora #\ormask
- sta (ZP_ADDRESS),Y
- sta gpuIndex
- jsr gpuSetIndex
- lda gpuIndex
- .endm
- .local playfield
- .local playfield_offset
- .local playerAddress
- .global tmpDirection
- playfield:
- .repeat 768
- .byte 0
- .endrepeat
- playfield_offset:
- .word $0000
- playfieldAddress:
- .word $0000
- playerAddress:
- .word $0000
- tmpDirection:
- .byte $00
- ; write the players head to playfield and also draw to screen
- writeHead:
- jsr savePlayerAddress
- ldy #$0
- lda (ZP_ADDRESS), Y ; get id
- ; form the correct sprite index
- ; in the form of 00gpbbdd where
- ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
- ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
- ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
- ; in this case, bb is 0 and we'll assume direction is left (0b10)
- ; and we just got the id, OR 0b10 with ID and save to stack
- ora tmpDirection
- sta gpuIndex
- ldy #1
- jsr calculateGPUXYandPlayfieldOffset
- jsr calculateAndSavePlayfieldAddress
- lda gpuIndex
- ldy #0
- sta (ZP_ADDRESS),Y
- jsr gpuSetIndex
- jsr loadPlayerAddress
- rts
- calculateGPUXYandPlayfieldOffset:
- jsr loadPlayerAddress
- ; send set index command with proper arguments to GPU
- lda (ZP_ADDRESS), Y ; get x
- sta gpuX
- sta playfield_offset+1 ; save x to playfield_offset
- iny
- lda (ZP_ADDRESS), Y ; get y
- sta gpuY
- ; y * 32 (or 2^5)
- ldy #32
- jsr ROM_mul8x8
- clc
- adc playfield_offset+1
- sta playfield_offset+1
- tya
- sta playfield_offset ; save 32 (width) times Y added to X in playfield offset
- rts
- writeTail:
- jsr savePlayerAddress
- ldy #$0
- lda (ZP_ADDRESS), Y ; get id
- ; form the correct sprite index
- ; in the form of 00gpbbdd where
- ; gp - is a combined player id from having gold, and player id (saved in stack in this case)
- ; bb - is body part 0 - head, 1 - straight, 2 - bend, 3 - tail
- ; dd - is forwards direction 0 - right, 1 - down, 2 - left, 3 - up
- ; in this case, bb is 0 and we'll assume direction is left (0b10)
- ; and we just got the id, OR 0b10 with ID and save to stack
- ora #%1100
- ora tmpDirection
- sta gpuIndex
- ldy #3
- jsr calculateGPUXYandPlayfieldOffset
- jsr calculateAndSavePlayfieldAddress
- lda gpuIndex
- ldy #0
- sta (ZP_ADDRESS),Y
- jsr gpuSetIndex
- jsr loadPlayerAddress
- rts
- lastPlayerDirection:
- .byte 0
- tmpPlayerDirection:
- .byte 0
- newPlayerDirection:
- .byte 0
- moveHead:
- jsr savePlayerAddress
- ; save last playerdirection
- ldy #7
- lda (ZP_ADDRESS),Y
- sta lastPlayerDirection
- ; get player data at head location
- ldy #1 ; head location
- jsr calculateGPUXYandPlayfieldOffset
- jsr calculateAndSavePlayfieldAddress
- ldy #0
- lda (ZP_ADDRESS),Y
- and #%11
- sta tmpPlayerDirection
- cmp lastPlayerDirection
- beq .straight ; if direction not changed, just set a straight
- ; if direction changed, check whether cw or ccw bend
- ; if (new dir - old dir) % 3 == 1, cw.
- ; if cw, add 1 from new dir
- sec
- sbc lastPlayerDirection
- and #%11
- cmp #1
- bne .setBend ; if !1, ccw, no modification
- ; cw, add one from new direction
- lda tmpPlayerDirection
- clc
- adc #1
- and #%11
- sta newPlayerDirection
- ldy #0
- lda (ZP_ADDRESS),Y
- and #DIR_MASK_INV
- ora newPlayerDirection
- sta (ZP_ADDRESS),Y
- .setBend:
- ; set bend
- change_worm_tile BODY_MASK_INV, BODY_BEND_GC
- jmp .saveLastDirection
- .straight:
- ; change spriteindex to be a straight part
- change_worm_tile BODY_MASK_INV, BODY_STRT_GC
- .saveLastDirection:
- ; get direction at start and save it as last direction
- jsr loadPlayerAddress
- lda tmpPlayerDirection
- and #%11
- ldy #7
- sta (ZP_ADDRESS),Y
- ; check which way the direction at start is and act accordingly
- cmp #%00
- beq moveHeadRight
- cmp #%01 ; 1 down
- beq moveHeadDown
- cmp #%10 ; 2 left
- beq moveHeadLeft
- ; move head up
- jsr loadPlayerAddress
- ldy #2
- lda (ZP_ADDRESS),Y
- sec
- sbc #1
- cmp #-1
- bne .continue
- lda #23
- .continue:
- sta (ZP_ADDRESS),Y
- lda #%11
- sta tmpDirection
- jsr writeHead
- rts
- moveHeadRight:
- jsr loadPlayerAddress
- ldy #1
- lda (ZP_ADDRESS),Y
- clc
- adc #1
- and #%11111 ; scrap carry, ie. % 32
- sta (ZP_ADDRESS),Y
- lda #%00
- sta tmpDirection
- jsr writeHead
- rts
- moveHeadDown:
- jsr loadPlayerAddress
- ldy #2
- lda (ZP_ADDRESS),Y
- clc
- adc #1
- cmp #24
- bne .continue
- lda #0
- .continue:
- sta (ZP_ADDRESS),Y
- lda #%01
- sta tmpDirection
- jsr writeHead
- rts
- moveHeadLeft:
- jsr loadPlayerAddress
- ldy #1
- lda (ZP_ADDRESS),Y ; read X
- sec
- sbc #1
- and #%11111 ; scrap carry, ie. % 32
- sta (ZP_ADDRESS),Y ; save new X
- lda #%10
- sta tmpDirection
- jsr writeHead
- rts
- moveTail:
- jsr savePlayerAddress
- ; save last player tail direction
- ldy #7
- lda (ZP_ADDRESS),Y
- sta lastPlayerDirection
- ; get player data at tail location
- ldy #3 ; tail location
- jsr calculateGPUXYandPlayfieldOffset
- jsr calculateAndSavePlayfieldAddress
- ldy #0
- lda (ZP_ADDRESS),Y
- and #%11
- sta tmpPlayerDirection
- ; erase old tail
- lda #$ff
- sta (ZP_ADDRESS), Y
- sta gpuIndex
- jsr loadPlayerAddress
- ldy #3
- lda (ZP_ADDRESS), Y ; get tail_x
- sta gpuX
- iny
- lda (ZP_ADDRESS), Y ; get tail_y
- sta gpuY
- jsr gpuSetIndex
- lda tmpPlayerDirection
- cmp #0 ; And the direction
- beq moveTailRight
- cmp #01
- beq moveTailDown
- cmp #02
- beq moveTailLeft
- ; move tail up
- ldy #4
- lda (ZP_ADDRESS), Y ; get tail_y
- sec
- sbc #1
- cmp #-1
- bne .continue
- lda #23
- .continue:
- sta (ZP_ADDRESS), Y ; save new head_x
- jmp changeToTail
- moveTailRight:
- ldy #3
- lda (ZP_ADDRESS), Y ; get tail_x
- clc
- adc #1
- and #%11111 ; scrap carry, ie. % 32
- sta (ZP_ADDRESS), Y ; save new head_x
- jmp changeToTail
- moveTailDown:
- ldy #4
- lda (ZP_ADDRESS), Y ; get tail_y
- clc
- adc #1
- cmp #24
- bne .continue
- lda #0
- .continue:
- sta (ZP_ADDRESS), Y ; save new head_x
- jmp changeToTail
- moveTailLeft:
- ldy #3
- lda (ZP_ADDRESS), Y ; get tail_x
- sec
- sbc #1
- and #%11111 ; scrap carry, ie. % 32
- sta (ZP_ADDRESS), Y ; save new head_x
- changeToTail:
- ldy #3 ; tail location
- jsr calculateGPUXYandPlayfieldOffset
- jsr calculateAndSavePlayfieldAddress
- change_worm_tile BODY_MASK_INV, BODY_TAIL_GC
- jsr loadPlayerAddress
- rts
- loadPlayerAddress:
- lda playerAddress
- sta ZP_ADDRESS
- lda playerAddress+1
- sta ZP_ADDRESS+1
- rts
- savePlayerAddress:
- lda ZP_ADDRESS
- sta playerAddress
- lda ZP_ADDRESS+1
- sta playerAddress+1
- rts
- calculateAndSavePlayfieldAddress:
- clc ; add playfield baseaddress to playfield offset to get absolute address in playfield_offset
- lda #<playfield
- adc playfield_offset+1
- sta ZP_ADDRESS
- sta playfieldAddress
- lda #>playfield
- adc playfield_offset
- sta ZP_ADDRESS+1
- sta playfieldAddress+1
- rts
- loadPlayfieldAddress:
- lda playfieldAddress
- sta ZP_ADDRESS
- lda playfieldAddress+1
- sta ZP_ADDRESS+1
- rts
- gpuX:
- .byte $00
- gpuY:
- .byte $00
- gpuIndex:
- .byte $00
- gpuSetIndex:
- lda #CF_CMD_SET_INDEX
- sta GPU_CMDQ_ADDRESS
- lda gpuX
- sta GPU_CMDQ_ADDRESS
- lda gpuY
- sta GPU_CMDQ_ADDRESS
- lda gpuIndex
- sta GPU_CMDQ_ADDRESS
- rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement