; LRupdown.asm
; 		by Maddy, allegedly

; Created for 'Late Night Terror Club' and 'Me, Reaping' in Super "Sonic Saves the World" World
; The player warps up/down when L/R are pressed (L = up, R = down)

; Feel free to reuse this code however you want. Credit is appreciated :)

; Caution: This effect will be very jarring if used in a level where the camera has room to move vertically,
; 		because it will snap instantly to the player's new position.
;		Therefore, it is recommended for use in rooms where the camera only ever moves horizontally.

; ------------------------------ 
; CONFIG OPTIONS
; ------------------------------ 

; if 1, the player's x-speed will be zeroed when they warp
!zero_x_speed = 0

; if 1, the player's y-speed will be set when they warp
; use the following two options define what the y-speed will be set to
; I find it best when the upward warp has a little bounce, and the downward warp zeroes y-speed
; However, when used with cape (like in 'Me, Reaping') I found it best to disable this entirely
!set_y_speed = 1
!up_y_speed = $C6
!down_y_speed = $00

; the amount of pixels to warp in each direction
!distance_up = $40
!distance_down = $46

; the sfx to play
!sfx_up = $18
!sfx_bank_up = $1DF9
!sfx_down = $16
!sfx_bank_down = $1DF9

; ------------------------------ 
; CODE
; ------------------------------ 

main:
	; exit if dead
	LDA $71
	CMP #$09
	BEQ +
	; exit if paused
	LDA $13D4
	BNE +
	; exit if past goal
	LDA $1493
	BNE +

	; button checks
	LDA $18	
	AND #%00100000
	BNE warp_up
	LDA $18	
	AND #%00010000
	BNE warp_down
	
	+
	RTL

warp_up:
	JSR create_smoke

	; subtract x-position
	LDA $D3
	SEC : SBC #!distance_up
	STA $96
	LDA $D4
	SBC #$00
	STA $97
	
	; sfx
	LDA #!sfx_up
	STA !sfx_bank_up
	
	; set speeds
	if !zero_x_speed
	STZ $7B
	endif
	if !set_y_speed
	LDA #!up_y_speed
	STA $7D
	endif
	
	RTL

warp_down:
	JSR create_smoke

	; add x-position
	LDA $D3
	CLC : ADC #!distance_down
	STA $96
	LDA $D4
	ADC #$00
	STA $97
	
	; sfx
	LDA #!sfx_down
	STA !sfx_bank_down
	
	; set speeds
	if !zero_x_speed
	STZ $7B
	endif
	if !set_y_speed
	LDA #!down_y_speed
	STA $7D
	endif
	
	RTL

; create a smoke effect at the player's position
create_smoke:
	LDY #$03           
	
	.find_slot
	LDA $17C0,y       
    BEQ .slot_found        
    DEY                   
    BPL .find_slot  
    RTS  

	.slot_found
	LDA #$01  	
    STA $17C0,y ; effect id
    LDA #$14           
    STA $17CC,y ; timer   
    LDA $94         
    STA $17C8,y ; x
	
	; for y, we want the smoke in a slightly different spot if you're big vs small
	LDA $19
	BEQ +
	LDA $96   
	CLC : ADC #$08
	STA $17C4,y ; y
	RTS
	+
	LDA $96    
	CLC : ADC #$10
    STA $17C4,y ; y
    RTS      