;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Creating/Eating Block, by Davros.
;;
;; Description: A creating/eating path block sprite, depending on it's X position. It uses
;; one of two paths, determined by whether or not the overworld level is on the main map 
;; or a submap.
;;
;; NOTE1: Place a block tile underneath the creating block, as it won't create the first
;;        one by itself.
;;
;; NOTE2: Do not place a block tile underneath the eating block, as it won't be eaten.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

incsrc subroutinedefs_trasm.asm

		    BLOCK_TILE = $2E
                    SOUND_TO_GEN = $04

                    MAP16_NUM1 = $0132      ; map16 value of the block in hex
                    MAP16_NUM2 = $0025      ; map16 value of the block in hex

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Creating/Eating Block Directional Data
;  
; Format: One byte per command, XY, where: 
; X = Number of times to go in direction Y. 
; Y = 0,1,2,3 (Right, left, down, up) 
; FF terminates the sprite.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


SUBMAP_PATH         dcb $80,$33,$50,$73,$E0,$82,$70,$43,$90,$42,$F0,$20,$43,$20,$52,$51
		    dcb $22,$90,$33,$30,$FF		
                    
OVERWORLD_PATH      dcb $80,$33,$50,$73,$E0,$82,$70,$43,$90,$42,$F0,$20,$43,$20,$52,$51
		    dcb $22,$90,$33,$30,$FF 


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; sprite init JSL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                    dcb "INIT"
		    LDA #$FF                ; \ Mario is standing on the brown "used" blocks
		    STA $1909               ; /
		    LDA $E4,x               ; \ x position
		    AND #$10                ; /
		    STA $151C,x
                    RTL
                    
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; sprite code JSL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

                    dcb "MAIN"                                    
                    PHB                     ; \
                    PHK                     ;  | main sprite function, just calls local subroutine
                    PLB                     ;  |
                    JSR SPRITE_CODE_START   ;  |
                    PLB                     ;  |
                    RTL                     ; /

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; sprite main code 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


X_SPEED		    dcb $10,$F0,$00,$00,$00
Y_SPEED		    dcb $00,$00,$10,$F0,$00
DIRECTION	    dcb $00,$00,$01,$00,$02,$00,$00,$00,$03,$00,$00


SPRITE_CODE_START   JSL $8190B2             ; draw sprite using the routine for sprites

		    LDY $15EA,x             
		    LDA $0301,y             ; \ load tile y position 
		    DEC A                   ;  |
		    STA $0301,y             ; / store tile y position

		    LDA #BLOCK_TILE         ; \ store tile
		    STA $0302,y             ; /

		    LDA $0303,y             ; \ load tile properties
		    AND #$3F                ;  |
		    STA $0303,y             ; / store tile properties

		    LDY #$02                ; \ 02, because we didn't write to 460 yet
		    LDA #$00                ;  | A = number of tiles drawn - 1
		    JSL $81B7B3             ; / don't draw if offscreen

		    LDY #$04
		    LDA $1909               ; \ if Mario is standing on the brown "used" blocks
		    CMP #$FF                ;  |
		    BEQ LOCK                ; /
		    LDA $13                 ; \ if frame counter is 03 
		    AND #$03                ;  |
		    ORA $9D                 ;  | ...or sprite is locked...
		    BNE SPRITE_DIRECTION    ; / go to SPRITE_DIRECTION

		    LDA #SOUND_TO_GEN       ; \ sound effect
		    STA $1DFA               ; /

SPRITE_DIRECTION    LDY $157C,x             ; \ lock sprites based on direction
LOCK		    LDA $9D                 ;  | if sprites locked, return
		    BNE RETURN              ; /

		    LDA X_SPEED,y           ; \ set x speed
		    STA $B6,x               ; /

		    LDA Y_SPEED,y           ; \ set y speed
		    STA $AA,x               ; /

		    JSL $81801A
		    JSL $818022
		    STZ $1528,x
		    JSL $81B44F             ; interact with sprites

		    LDA $1909               ; \ if Mario is standing on the brown "used" blocks
		    CMP #$FF                ;  |
		    BEQ RETURN              ; /
		    LDA $D8,x
		    ORA $E4,x
		    AND #$0F
		    BNE RETURN
		    LDA $151C,x
		    BNE BLOCK_SETUP2
		    DEC $1570,x
		    BMI CHARACTER
		    BNE BLOCK_SETUP1

CHARACTER	    LDY $0DB3

SUBMAP		    LDA $1F11,y             ; \ if the sprite's location is on the overworld or submap...
		    CMP #$01                ; /
		    LDY $1534,x             
		    INC $1534,x             
		    LDA SUBMAP_PATH,y       ; ...take the submap path
		    BCS PATH_POSITION       ;
		    LDA OVERWORLD_PATH,y    ; ...take the overworld path
PATH_POSITION	    STA $1602,x             ; store position
		    PHA
		    LSR A
		    LSR A
		    LSR A
		    LSR A
		    STA $1570,x
		    PLA
		    AND #$03
		    STA $157C,x
BLOCK_SETUP1	    JSR BLOCK1
		    LDA $1602,x             ; \  if it's position is FF...
		    CMP #$FF                ;  |
		    BEQ DISAPPEAR           ; /  ...go to DISAPPEAR
RETURN		    RTS                     ; return

BLOCK_SETUP2	    JSR BLOCK2
		    LDA #$01
		    STA $B6,x
		    STA $AA,x
		    JSL $819138
		    LDA $1588,x
		    PHA
		    LDA #$FF
		    STA $B6,x
		    STA $AA,x
		    LDA $E4,x
		    PHA
		    SEC
		    SBC #$01
		    STA $E4,x
		    LDA $14E0,x
		    PHA
		    SBC #$00
		    STA $14E0,x
		    LDA $D8,x
		    PHA
		    SEC
		    SBC #$01
		    STA $D8,x
		    LDA $14D4,x
		    PHA
		    SBC #$00
		    STA $14D4,x
		    JSL $819138
		    PLA
		    STA $14D4,x
		    PLA
		    STA $D8,x
		    PLA
		    STA $14E0,x
		    PLA
		    STA $E4,x
		    PLA
		    ORA $1588,x
		    BEQ DISAPPEAR
		    TAY
		    LDA DIRECTION,y         ; \ set direction
		    STA $157C,x             ; /
		    RTS                     ; return
DISAPPEAR	    STZ $14C8,x             ; disappear the sprite
		    RTS

BLOCK1              LDA $E4,x               ; \ setup block properties
                    STA $9A                 ;  |
                    LDA $14E0,x             ;  |
                    STA $9B                 ;  |
                    LDA $D8,x               ;  |
                    STA $98                 ;  |
                    LDA $14D4,x             ;  |
                    STA $99                 ; /

                    PHP
                    REP #$30                ; \ change sprite to block 
                    LDA.W #MAP16_NUM1       ;  |
                    STA $03                 ;  |
                    JSL SubSetMap16      ;  |
                    PLP                     ; / 
                    RTS     

BLOCK2              LDA $E4,x               ; \ setup block properties
                    STA $9A                 ;  |
                    LDA $14E0,x             ;  |
                    STA $9B                 ;  |
                    LDA $D8,x               ;  |
                    STA $98                 ;  |
                    LDA $14D4,x             ;  |
                    STA $99                 ; /

                    PHP
                    REP #$30                ; \ change sprite to block 
                    LDA.W #MAP16_NUM2       ;  |
                    STA $03                 ;  |
                    JSL SubSetMap16      ;  |
                    PLP                     ; / 
                    RTS     



                    
