; level_x_move_foes - make the foes move
; level_x_sway_GP - make the foes sway back and forth
; level_x_foe_collision - did the foes hit the player?
; level_x_move_bonus_points - did the player hit the bonus item?

;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------
; Foe movement
;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------

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

   LD a, (RAM_LEVEL_B)         ;
   CP 1                        ;   
   CALL Z, intro_move_GP1_down ;
   CP 1                        ;      
   CALL Z, intro_move_GP2_down ;
   CP 2                        ;   
   CALL Z, intro_move_GP1_up   ;
   CP 2                        ;      
   CALL Z, intro_move_GP2_up   ;  
   CP 3                        ;   
   CALL Z, intro_move_GP1_down ;
   CP 3                        ;      
   CALL Z, intro_move_GP2_down ;   
   CALL level_x_sway_GP        ; 
   
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine   

;--------------------------------------   
; level_x_sway_GP   
;   
level_x_sway_GP:   
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD a, (RAM_FRAME_CTR)       ; Sway slowly
   AND $01                     ; 
   JP Z, level_x_sway_GP_end   ;

   LD a, (RAM_SECOND_CTR)      ; Sway slowly
   AND $01                     ;    
   JP Z, lxsGP_sway2           ;

; GP 1
   LD hl, RAM_HPOS             ; Start of foe 1 HPOS
   LD b, 21                    ; 21 sprites
   
lxsGP_loop_0:   
   DEC (hl)                    ; Go left
   INC hl                      ; Move address
   INC hl                      ;
   DEC b                       ;
   JP NZ, lxsGP_loop_0         ;

; GP 2
   LD hl, RAM_HPOS + 42        ; Start of foe 2 HPOS
   LD b, 21                    ; 21 sprites
   
lxsGP_loop_1:   
   INC (hl)                    ; Go right
   INC hl                      ; Move address
   INC hl                      ;
   DEC b                       ;
   JP NZ, lxsGP_loop_1         ;

   JP level_x_sway_GP_end      ; 
   
lxsGP_sway2:
; GP 1
   LD hl, RAM_HPOS             ; Start of foe 1 HPOS
   LD b, 21                    ; 21 sprites
   
lxsGP_loop_2:   
   INC (hl)                    ; Go right
   INC hl                      ; Move address
   INC hl                      ;
   DEC b                       ;
   JP NZ, lxsGP_loop_2         ;

; GP 2
   LD hl, RAM_HPOS + 42        ; Start of foe 2 HPOS
   LD b, 21                    ; 21 sprites
   
lxsGP_loop_3:   
   DEC (hl)                    ; Go left
   INC hl                      ; Move address
   INC hl                      ;
   DEC b                       ;
   JP NZ, lxsGP_loop_3         ;   

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

   
;-----------------------------------------   
; level_x_foe_collision 
;  
level_x_foe_collision:
   PUSH BC                     ; Save registers
   PUSH DE                     ;
   PUSH HL                     ;
   PUSH AF                     ;
   
   LD a, (RAM_VPOS + 52)       ; Player y
   LD (RAM_COLL_Y1), a         ;
   LD a, (RAM_HPOS + 104)      ; Player x
   LD (RAM_COLL_X1), a         ;
   
   LD a, (RAM_VPOS + 4)        ; Foe 1 y 
   LD (RAM_COLL_Y2), a         ;   
   LD a, (RAM_HPOS + 2)        ; Foe 1 x
   LD (RAM_COLL_X2), a         ;   
   CALL xy_collision_detect16  ;
   
   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP Z, lxfcoll_hit           ;


   LD a, (RAM_VPOS + 19)       ; Foe 1 y 
   LD (RAM_COLL_Y2), a         ;     
   CALL xy_collision_detect16  ;

   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP Z, lxfcoll_hit           ;
   
   LD a, (RAM_VPOS + 25)       ; Foe 2 y 
   LD (RAM_COLL_Y2), a         ;  
   LD a, (RAM_HPOS + 44)       ; Foe 2 x
   LD (RAM_COLL_X2), a         ;    
   CALL xy_collision_detect16  ;

   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP Z, lxfcoll_hit           ;
   
   LD a, (RAM_VPOS + 40)       ; Foe 2 y 
   LD (RAM_COLL_Y2), a         ;    
   CALL xy_collision_detect16  ;
   
   LD a, (RAM_COLL_RESULT)     ; Collision?
   CP $7F                      ;
   JP Z, lxfcoll_hit           ;
   
   JP lxfcoll_miss             ;
   
lxfcoll_hit:
; 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        ;   
   
lxfcoll_miss:
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine  

   
;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------
; Bonus Points
;-------------------------------------------------------------------
;-------------------------------------------------------------------
;-------------------------------------------------------------------

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

; Fall until hit floor
   LD a, (RAM_VPOS + 54)       ; Y-coordinate of bonus points
   ADD a, 16                   ;
   LD b, a                     ;
   LD a, (RAM_HPOS + 108)      ; Index for floor table
   RRA                         ; Divide by 8
   RRA                         ;
   RRA                         ;
   AND $1F                     ; Mask unwanted upper bits  
   LD de, $0000                ; Read from table with index
   LD e, a                     ;
   LD hl, RAM_FLOOR_TABLE      ;
   ADD hl, de                  ;
   LD a, (hl)                  ; Result, Y-coordinate of floor
   
   CP b                        ; b == a ?
   JP Z, lxmbp_skip_fall       ;
   
   INC hl                      ;
   LD a, (hl)                  ; Result, Y-coordinate of floor   
   CP b                        ; b == a ?
   JP Z, lxmbp_skip_fall       ;
   
   LD hl, RAM_VPOS + 54        ; Go down 
   INC (hl)                    ;
   INC (hl)                    ;
   INC hl                      ;
   INC (hl)                    ;
   INC (hl)                    ;
   INC hl                      ;
   INC (hl)                    ;
   INC (hl)                    ;
   INC hl                      ;
   INC (hl)                    ;
   INC (hl)                    ;
   INC hl                      ;
      
lxmbp_skip_fall:   
; Respawn if hit bottom or left
   LD a, (RAM_HPOS + 108)      ; Hit left?
   CP $08                      ;
   JP Z, lxmbd_respawn         ;

   LD a, (RAM_VPOS + 56)       ; Hit bottom?
   CP $B0                      ;
   JP Z, lxmbd_respawn         ;
   
   JP lxmbd_skip_respawn       ;
   
lxmbd_respawn:
   CALL sound_pow_sfx          ;
   LD de, level_x_sp_vpos + 54 ;
   LD hl, RAM_VPOS + 54        ;
   LD b, 4                     ;

lxmbd_respawn_loop0:   
   LD a, (de)                  ;
   LD (hl), a                  ;
   INC hl                      ;
   INC de                      ;
   DEC b                       ;   
   JP NZ, lxmbd_respawn_loop0  ;

   LD de, level_x_sp_hpos + 108 ;
   LD hl, RAM_HPOS + 108       ;
   LD b, 8                     ;
   
lxmbd_respawn_loop1:   
   LD a, (de)                  ;
   LD (hl), a                  ;
   INC hl                      ;
   INC de                      ;
   DEC b                       ;   
   JP NZ, lxmbd_respawn_loop1  ;
   
lxmbd_skip_respawn:
   POP AF                      ; Restore registers
   POP HL                      ;
   POP DE                      ;
   POP BC                      ;
   RET                         ; End subroutine 