Timer   = $15	; how long until the sprite pops while mario stands on it
HowSlow = $03	; how slow the timer will increase, only these values apply;
;; (Fastest) $00 $01 $03 $07 $0F $1F $3F $7F $FF (slowest)
;rideable_balloon
;by smkdan

incsrc subroutinedefs_trasm.asm

;DOESN'T USE EXTRA BIT

;balloon that you can step on for a ride

;Tilemaps for SP4

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

XSPD	dcb $00,$02,$04,$03,$00,$FE,$FC,$FD
XMAR1	dcb $00,$00,$01,$01,$00,$00,$FF,$FF
XMAR2	dcb $00,$00,$00,$00,$00,$00,$FF,$FF

COUNT	dcb $40,$10,$40,$10
PAL	dcb $04,$06,$08,$0A

SPDCMP	dcb $FA,$FD
INCDEC	dcb $FF,$01

;C2: riding bit


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

dcb "INIT"
	LDA $E4,x	; save
	STA $1510,x	; initial
	LDA $14E0,x	; positions
	STA $151C,x	; to
	LDA $D8,x	; some
	STA $1570,x	; sprite
	LDA $14D4,x	; ram so it
	STA $157C,x	; can respawn


	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
Respawn
	STZ $1540,x
	LDA $1510,x
	STA $E4,x
	LDA $151C,x
	STA $14E0,x
	LDA $1570,x
	STA $D8,x
	LDA $157C,x
	STA $14D4,x
	STZ $187B,x
	STZ $1528,x

	LDA $15A0,x
	ORA $186C,x
	BNE Return_l
	JSR SUB_SMOKE
	LDA #$1E
	STA $1DF9
Return_l
	RTS
Run	JSL SubOffscreenX0
	LDA $1540,x
	BEQ Normal
	CMP #$02
	BCC Respawn
	RTS
Normal	JSR GFX			;draw sprite
	STZ $15EA,x
	LDA $14C8,x
	CMP #$08          	 
	BNE Return_l           
	LDA $9D			;locked sprites?
	BNE Return_l
	LDA $15D0,x		;yoshi eating?
	BNE Return_l
	JSL $818022
	JSL $81801A

	LDA $7FAB10,x
	AND #$04
	BEQ nocon
	LDA #$09
	STA $1686,x
	JSL $819138
	LDA $18A7
	CMP #$30		;;;;;;;;;; this is the type of type it checks for!!
	BEQ pop
nocon	LDA $1528,x
	CMP #Timer
	BCC NoPop
	JSR NoPop
pop	LDA #$19
	STA $1DFC
spop	STZ $18A7
	JSR SUB_SMOKE
	LDA #$20
	STA $1540,x
	RTS
NoPop
	STZ $C2,x		;reset riding sprite, may get set later

	JSL $81A7DC		;mario interact
	BCC NoInteraction	;do nothing if there's no contact
	LDA $1558,x
	BNE NoInteraction

	JSL SubVertPos
	LDA $0E
	CMP #$E8
	BMI NoInteraction
        LDA $7D			;don't interact on rising speed
        BMI NoInteraction

;interacting, set positions
	LDA $187B,x
	AND #HowSlow
	BNE NoInc
	INC $1528,x
NoInc
	LDA #$01
	STA $C2,x		;set riding sprite for GFX routine

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

	LDA $77
	AND #$08
	BEQ NormInt
	LDA #$10
	STA $1558,x

NormInt	LDA #$E8
	LDY $187A
	BEQ NOYOSHI
	LDA #$E8-$10
NOYOSHI	CLC
	ADC $D8,x
	STA $96
	LDA $14D4,x
	ADC #$FF
	STA $97
NoInteraction
;apply speed
	INC $187B,x

NoCnt	LDA $187B,x
	LSR : LSR : LSR : LSR
	AND #$07
	TAY
	LDA XSPD,y
	STA $B6,x
	LDA $C2,x
	BEQ NoMarMov
	LDA $187B,x
	AND #$03
	BNE NoMarMov
	LDA $94
	CLC
	ADC XMAR1,y
	STA $94
	LDA $95
	ADC XMAR2,y
	STA $95
NoMarMov
	LDY $C2,x		;load ride bit as index
	LDA SPDCMP,y
	STA $AA,x

Return
	RTS        

SUB_SMOKE
	LDA $15A0,x
	ORA $186C,x
	BNE NoSmoke
	LDY #$03
LoopSmk	LDA $17C0,y
	BEQ Found
	DEY
	BPL LoopSmk
	RTS
Found	LDA #$01
	STA $17C0,y
	LDA $E4,x
	CLC
	ADC #$08
	STA $17C8,y
	LDA $D8,x
	STA $17C4,y
	LDA #$1B
	STA $17CC,y
NoSmoke	RTS
			
;=====

TILEMAP	dcb $E0,$E2,$C4,$C6

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

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

GFX	JSL GetDrawInfo
	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 TILEMAP,x
	STA $0302,y	;chr

	LDA PROPRAM
	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

