;rideable_balloon
;by smkdan

;DOESN'T USE EXTRA BIT

;balloon that you can step on for a ride

;Tilemaps for SP4

incsrc subroutinedefs_trasm.asm

DIR = $03
PROPRAM = $04
PROPINDEX = $05
XDISPINDEX = $06
YDISPINDEX = $07
FRAMEINDEX = $08
TEMP = $09

XSPD	dcb $FC,$00,$04,$00
COUNT	dcb $40,$10,$40,$10
XADJUST	dcb $FF,$00,$01,$00
PAL	dcb $04,$06,$08,$0A

SPDCMP	dcb $F8,$FB
INCDEC	dcb $FF,$01

;C2: riding bit
;1570: behaviour count
;1602: X scroll counter


HeightTbl dcw $FFE6,$FFD6,$FFD6
ClipTbl	dcw $0016,$0026,$0026

dcb "INIT"

	;LDA $7FAB10,x		;only use custom color if made from generator
	;AND #$04
	;BEQ NormPal

	;JSL $81ACF9		;get random
	;AND #$03		;only low 2 bits
	;TAY			;to be used as index

	;LDA $15F6,x		;load properties
	;AND #$F1		;all but pal bits

	;PHB
	;PHK
	;PLB
	;ORA PAL,y		;set palette bits
	;PLB

	;STA $15F6,x		;new palette

;NormPal
	;LDA #$FA		;fixed Yspd
	;STA $AA,x		;initial Yspd

	RTL

dcb "MAIN"
	PHB
	PHK
	PLB
	JSR Run
	PLB
	RTL

Return_l
	RTS

Run
	JSL SubOffscreenX0
        JSL GetDrawInfo
	JSR GFX			;draw sprite

	LDA $14C8,x
	CMP #$08          	 
	BNE Return_l           
	LDA $9D			;locked sprites?
	BNE Return_l
	LDA $15D0,x		;yoshi eating?
	BNE Return_l

	STZ $C2,x		;reset riding sprite, may get set later

	JSL $81A7DC		;mario interact
	BCC NoInteraction	;do nothing if there's no contact

        LDA $7D			;don't interact on rising speed
        BMI NoInteraction


	LDA $187A		;yoshi flag
	CLC
	ASL A
	TAY			;into Y

;control height required to be on top of sprite

	LDA $14D4,x		;sprite high Y
	XBA
	LDA $D8,x		;sprite low Y
	REP #$20		;16bit math
	SEC
	SBC $96			;sub mario Y pos.  Mario is OVER the sprite.  Mario Y < sprite Y
	CMP ClipTbl,y		;compare depending on mario or yoshi mario
	SEP #$20
	BCC Return_I		;return if he's too low (underflows)

;interacting, set positions

	LDA #$01
	STA $C2,x		;set riding sprite for GFX routine

	LDA #$01
	STA $1471		;'riding sprite'
	STZ $7D			;no Y speed

	LDA $187A		;yoshi flag
	CLC
	ASL A			;2 bytes
	TAY			;into Y reg

	LDA $14D4,x		;sprite high Y
	XBA
	LDA $D8,x		;sprite low Y
	REP #$20
	CLC
	ADC HeightTbl,y		;stepped on height
	STA $96			;new mario Y position
	SEP #$20

NoInteraction
	INC $1602,x		;advance counter
	LDA $1602,x
	LDY $1570,x		;load behaviour as index
	CMP COUNT,y		;X frames
	BCC NoChange		;if count not yet reached, maintain C2

;advance behaviour since count was reached

	LDA $1570,x		;load scroll behaviour...
	INC A
	AND #$03		;4 variables
	STA $1570,x		;...new scroll behaviour
	STZ $1602,x		;reset counter

;apply speed

NoChange
	LDY $1570,x		;load as index
	LDA XSPD,y		;speed value
	STA $B6,x		;new Xspd

	LDA $C2,x		;load riding bit
	BEQ LeaveMario

;adjust Mario position
	
	LDA $1602,x		;load counter...
	AND #$03
	BNE LeaveMario		;every 2nd frame

	LDY $1570,x		;load balloon behaviour as index
	LDA XADJUST,y		;load adjustment
	CLC
	ADC $94			;MarioX low added
	STA $94			;store

;DO HIGH

LeaveMario
Return_I
	LDA $AA,x		;load Yspd
	LDY $C2,x		;load ride bit as index
	CMP SPDCMP,y		;load targetspeed
	BEQ StoreY

	CLC
	ADC INCDEC,y		;add or dec to Yspd

StoreY
	STA $AA,x		;store Yspd

UpdateXPosNoGrvty
	JSL $818022
	JSL $81801A		;update Y pos no gravity
	;JSL $81802A		;update speed

;-----------------------------------------------
;This bit of code will cause the sprite to "pop"
;-----------------------------------------------
	LDA $14D4,x
	XBA
	LDA $D8,x
	SEC
	SBC $1C
	CMP #$30
	BCC EraseSprite
	LDA $1588,x	; If not, check object interaction.
	AND #%00001000	; If the sprite is touching walls..
	BNE EraseSprite	; Explode.
	RTS

EraseSprite	
	STZ $14C8,x
	LDA #$19
	STA $1DFC

;==================================================================
;Draw Smoke
;==================================================================

;DrawSmoke:
		    LDY #$03                ; \ find a free slot to display effect
FINDFREE:           LDA $17C0,y             ;  |
                    BEQ FOUNDONE            ;  |
                    DEY                     ;  |
                    BPL FINDFREE            ;  |
                    RTS                     ; / return if no slots open

FOUNDONE:           LDA #$01                ; \ set effect graphic to smoke graphic
                    STA $17C0,y             ; /
                    LDA #$1B                ; \ set time to show smoke
                    STA $17CC,y             ; /
                    LDA $D8,x               ; \ smoke y position = generator y position
		    CLC
		    ADC #$08
                    STA $17C4,y             ; /
                    LDA $E4,x               ; \ load generator x position and store it for later
		    CLC
		    ADC #$08
                    STA $17C8,y             ; /
;                   RTS
;RTS

Return
	RTS
			
;=====

TILEMAP	dcb $E0,$E2,$A8,$AA

XDISP	dcb $00,$10,$00,$10

YDISP	dcb $FE,$FE,$0E,$0E
	dcb $00,$00,$0E,$0E

GFX
	LDA $C2,x	;stepped on?
	ASL A		;x4
	ASL A
	STA FRAMEINDEX

	LDA $157C,x	;direction...
	STA DIR

	;LDA $15F6,x	;properties...
	;STA PROPRAM

	LDX #$00	;loop index

OAM_Loop
	LDA $00
	CLC
	ADC XDISP,x
	STA $0300,y	;xpos

	TXA	
	CLC
	ADC FRAMEINDEX	;variable YDISP
	PHX
	TAX
	LDA $01
	CLC
	ADC YDISP,x
	STA $0301,y	;ypos
	PLX

;LDA $01
;CMP #$40
;BNE Continue
;STZ $14C8,x
;Continue

	LDA TILEMAP,x
	STA $0302,y	;chr

	;LDA PROPRAM
	LDA #$09  ;was 39
	ORA $64
	STA $0303,y	;properties

	INY
	INY
	INY
	INY
	INX
	CPX #$04	;4 tiles
	BNE OAM_Loop

	LDX $15E9		;restore sprite index

	LDY #$02		;16x16 tiles
	LDA #$03		;4 tiles
	JSL $81B7B3

	RTS			;return

