; level_x_detect_loss_of_life - is the player hit by a foe or fallen to the bottom of the screen?
; level_x_ouch - animate player getting shocked 
; level_x_end_level_same - repeat level after death

;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------
; Game Over
;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------

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

; Fall to floor?
   LD a, (RAM_VPOS + 52)       ;
   CP $B7                      ; Fell too far
   JP NZ, lxdlol_next_check    ;   

; Go to in-shock state   
   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        ;
   JP lxdlol_end               ;
   
lxdlol_next_check:
   CALL level_x_foe_collision  ; Hit a foe?
   
lxdlol_end:   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine   
 
;---------------------------------
; level_x_ouch
; 
level_x_ouch:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD a, (RAM_FRAME_CTR)       ;
   AND $08                     ;   
   JP Z, lxo_level_x_shock_frame ;
   
   LD de, level_x_stand_frame  ;
   CALL lxmpl_subcopy          ;

   JP lxo_ouch_end             ;
   
lxo_level_x_shock_frame:   
   LD de, level_x_shock_frame  ; Stand still frame is default
   CALL lxmpl_subcopy          ;

lxo_ouch_end:
   LD a, (RAM_SHOCK_CTR)       ; Increment shot counter
   INC a                       ;
   LD (RAM_SHOCK_CTR), a       ;
   
   CP 120                      ; 2 seconds of death animation
   CALL Z, level_x_end_level_same ; End, but repeat level
  
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine     
   
;----------------------------
; level_x_end_level_same
;
level_x_end_level_same:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   CALL title_load_menu00      ; Reset sprites

   LD a, 0                     ; Reset scroll
   LD (RAM_SCROLL_CTR), a      ; Scroll
   LD (RAM_COLUMN_INDEX), a    ;

   LD hl, $3801                ; Address of column to write to
   LD (RAM_COLUMN_ADDR), hl    ; 2 bytes
   LD a, 20*8                  ; Initial floor table value
   LD b, 32                    ;
   LD hl, RAM_FLOOR_TABLE      ; 
   
initft_002:
   LD (hl), a                  ; Initialize floor table
   INC hl                      ;
   DEC b                       ;
   JP NZ, initft_002           ;
   LD a, 5                     ; Initial player column (0-31)
   LD (RAM_PLAYER_X_COLUMN), a ; Player X-coordinate (sprite 53)    
   
; Keep same level
   
   LD a, 0                     ; Reset pedometer
   LD (RAM_PEDOMETER), a       ;
   
   LD a, 3                     ; Display score
   LD (RAM_GAME_STATE), a      ;

; Decrement number of lives
   LD a, (RAM_LIVES)           ;
   DEC a                       ;
   LD (RAM_LIVES), a           ;
   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine     
   
   