; LRleftright.asm
; 		by Maddy, allegedly

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

; 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 horizontally,
; 		because it will snap instantly to the player's new position.
;		Therefore, it is recommended to use in rooms where the camera only ever moves vertically.

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

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

; if 1, the player's y-speed will be zeroed when they warp
; in 'Late Night Terror Club' this is 1, in 'Me, Reaping' it is 0
!zero_y_speed = 1

; the amount of pixels to warp in each direction
!distance_left = $50
!distance_right = $50

; the sfx to play
!sfx_left = $18
!sfx_bank_left = $1DF9
!sfx_right = $16
!sfx_bank_right = $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_left
	LDA $18	
	AND #%00010000
	BNE warp_right
	
	+
	RTL

warp_left:
	JSR create_smoke

	; warp x-position
	LDA $D1
	SEC : SBC #!distance_left
	STA $94
	LDA $D2
	SBC #$00
	STA $95
	
	; sfx
	LDA #!sfx_left
	STA !sfx_bank_left
	
	; set speeds
	if !zero_y_speed
	STZ $7D
	endif
	if !zero_x_speed
	STZ $7B
	endif
	RTL

warp_right:
	JSR create_smoke

	; warp x-position
	LDA $D1
	CLC : ADC #!distance_right
	STA $94
	LDA $D2
	ADC #$00
	STA $95
	
	; sfx
	LDA #!sfx_right
	STA !sfx_bank_right
	
	; set speeds
	if !zero_y_speed
	STZ $7D
	endif
	if !zero_x_speed
	STZ $7B
	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      