;-----------------------------------
; level_y.txt
;
; Run a vertically scrolling level.
; Load background once.
; Player jumps from moving
; platform to moving platform.
; Fall to end of screem is death.
; All platforms are sprites.
;
; level_y_load_bg - Load the background for the vertically scrolling level
; level_y_run - run the vertically scrolling level
; level_y_move_platforms - move the platforms
; level_y_mp_left - move platforms right
; level_y_mp_right - move platforms left
; level_y_detect_game_over - did the player fall off screen?
;-----------------------------------

;-----------------------------------
; level_y_load_bg
;
level_y_load_bg:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   CALL VDP_off                ; Screen off

; Initial background load
   LD b, 32                    ; Number of columns (building in middle)
   LD hl, $37FF                ; VRAM BG
 
lyls_loop_0:
   LD de, lvly_col_002         ; ROM source
   CALL level_x_load_column    ; Write a column

   INC hl                      ; Write 32 columns
   INC hl                      ;
   DEC b                       ;
   
   LD de, lvly_col_003         ; ROM source
   CALL level_x_load_column    ; Write a column  

   INC hl                      ; Write 32 columns
   INC hl                      ;
   DEC b                       ;   
   
   LD de, lvly_col_004         ; ROM source
   CALL level_x_load_column    ; Write a column

   INC hl                      ; Write 32 columns
   INC hl                      ;
   DEC b                       ;
   
   LD de, lvly_col_005         ; ROM source
   CALL level_x_load_column    ; Write a column  

   INC hl                      ; Write 32 columns
   INC hl                      ;
   DEC b                       ;     
   
   JP NZ, lyls_loop_0          ;
   
; Load sprites   
   LD hl, level_y_sp_vpos      ; Load vpos
   LD de, RAM_VPOS             ;
   LD b, 59                    ; 21+21+12+4+1
   CALL copy_to_RAM_loop       ;

   LD hl, level_y_sp_hpos      ; Load hpos
   LD de, RAM_HPOS             ;
   LD b, 117                   ; ((21+21+12+4)*2)+1
   CALL copy_to_RAM_loop       ;
   
; Go to next state   
   LD a, 0                     ;
   LD (RAM_JUMP_STATE), a      ;
   LD a, (RAM_PEDOMETER)       ; Completed 5 screens?   
   LD hl, RAM_GAME_STATE       ; Run the game      
   INC (hl)                    ;

   CALL VDP_on                 ; Screen on

   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine  

;-----------------------------------
; level_y_run
;
level_y_run:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;

; Paused?   
   LD a, (RAM_PAUSE)           ; even=run, odd=paused
   AND $01                     ;
   JP NZ, lyrun_paused         ;   
   
; Stand still is default
   LD de, level_x_stand_frame  ; Stand still frame is default
   CALL lxmpl_subcopy          ;
   
   CALL level_y_move_platforms ; Move the platforms
   CALL level_y_handle_jumping ; Handle jumping and falling

; Go left or right?
   IN a, (PORT_INPUT1)         ;   
   CALL lymp_left_or_right     ;
   BIT 5, a                    ;
   CALL Z, lymp_left_or_right  ;  

; Game over condition?   
   CALL level_y_detect_game_over ;   

lyrun_paused:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine  

lymp_left_or_right:
   BIT 2, a                    ; Go left?
   CALL Z, level_y_move_player_left ;
; Go right   
   BIT 3, a                    ; Go right?
   CALL Z, level_y_move_player_right;
   RET                         ; End subroutine     
   
;-----------------------------------
; level_y_move_platforms
;
level_y_move_platforms:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;

   LD a, (RAM_FOE_STATE_1)     ; move fast blocks
   CP $00                      ;
   CALL Z, level_y_mp_left     ;
   CP $7F                      ;
   CALL Z, level_y_mp_right    ;

   LD a, (RAM_HPOS)            ; Time to switch? 
   CP $C0                      ;
   JP NZ, lymp_skip            ;

   LD a, $7F                   ;
   LD (RAM_FOE_STATE_1), a     ;
   JP lymp_skip2               ;
   
lymp_skip: 
   LD a, (RAM_HPOS)            ; Time to switch? 
   CP $10                      ;
   JP NZ, lymp_skip2           ; 

   LD a, $00                   ;
   LD (RAM_FOE_STATE_1), a     ;
   
lymp_skip2:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine  

;-----------------------------------
; level_y_mp_left
;
level_y_mp_left:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;

   LD hl, RAM_HPOS             ; Block 1
   LD b, 8                     ;
   
lympl_loop_0:
   INC (hl)                    ;
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympl_loop_0         ;
 
   LD b, 8                     ; 
lympl_loop_1:
   DEC (hl)                    ; Block 2
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympl_loop_1         ;   

   LD b, 8                     ; 
lympl_loop_2:
   INC (hl)                    ; Block 3
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympl_loop_2         ;  

   LD b, 8                     ; 
lympl_loop_3:
   DEC (hl)                    ; Block 4
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympl_loop_3         ;    

   LD b, 8                     ; 
lympl_loop_5:
   INC (hl)                    ; Block 5
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympl_loop_5         ; 
   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine    
   
;-----------------------------------
; level_y_mp_right
;
level_y_mp_right:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;

   LD hl, RAM_HPOS             ; Block 1
   LD b, 8                     ;
   
lympr_loop_0:
   DEC (hl)                    ;
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympr_loop_0         ;
 
   LD b, 8                     ; 
lympr_loop_1:
   INC (hl)                    ; Block 2
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympr_loop_1         ;   

   LD b, 8                     ; 
lympr_loop_2:
   DEC (hl)                    ; Block 3
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympr_loop_2         ;  

   LD b, 8                     ; 
lympr_loop_3:
   INC (hl)                    ; Block 4
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympr_loop_3         ;    

   LD b, 8                     ; 
lympr_loop_5:
   DEC (hl)                    ; Block 5
   INC hl                      ;
   INC hl                      ;
   DEC b                       ;
   JP NZ, lympr_loop_5         ; 
   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine       

;---------------------------------------
; level_y_detect_game_over
;
level_y_detect_game_over:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD a, (RAM_VPOS + 52)       ; Player hit bottom?
   CP $CF                      ;
   JP NZ, lydgo_skip_000       ;
   
   LD a, 0                     ; Clear shock counter
   LD a, (RAM_SHOCK_CTR)       ;
   LD a, 7                     ; Go to death animation state
   LD (RAM_GAME_STATE), a      ;     
   CALL sound_shock_sfx        ;
   
lydgo_skip_000:   
   LD a, (RAM_PEDOMETER)       ; Completed 3 screens?
   CP 3                        ;
   JP NZ, lydgo_skip_001       ;

   LD a, 1                     ; Reset minor level
   LD (RAM_LEVEL_B), a         ;
   LD a, (RAM_LEVEL_A)         ;
   INC a                       ;
   LD (RAM_LEVEL_A), a         ; Increment major level 
   LD a, 10                    ; Go to ending
   LD (RAM_GAME_STATE), a      ;  

; Clear sprite RAM, but not player data
   CALL title_load_menu00      ; Reset sprites
   
lydgo_skip_001:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine  

   
.include "level_y_player.txt"

   