;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;; INPUT CONTROLLER ;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.include "cpctelera.h.s"
.include "../cpct_globals.h.s"
.include "../man/man_entity.h.s"
.include "render.h.s"
.include "../assets/assets.h.s"

input_cmps = e_cmps_input

;;BUCLE HASTA QUE SE PULSE ALGUNA TECLA
input_detectAnyKey::

    key_pressed:

        call    cpct_scanKeyboard_f_asm         ;;  Function to read the keyboard
        call    cpct_isAnyKeyPressed_asm      ;;  Detects if any key is pressed
        jr nz, key_pressed

    key_not_pressed:

        call    cpct_scanKeyboard_f_asm         ;;  Function to read the keyboard
        call    cpct_isAnyKeyPressed_asm      ;;  Detects if any key is pressed
        jr      z , key_not_pressed         

ret

move_right:

    ld a, e_posx(ix)
    ld e_last_x(ix), a

    ld a, e_posy(ix)
    ld e_last_y(ix), a

    ld e_vx(ix), #1
    ChangeSpriteDirection _sprite_player_08
ret

move_left:

    ld a, e_posx(ix)
    ld e_last_x(ix), a

    ld a, e_posy(ix)
    ld e_last_y(ix), a

    ld e_vx(ix), #-1
    ChangeSpriteDirection _sprite_player_12
ret

move_up:

    ld a, e_posx(ix)
    ld e_last_x(ix), a

    ld a, e_posy(ix)
    ld e_last_y(ix), a

    ld e_vy(ix), #-3
    ChangeSpriteDirection _sprite_player_04
ret

move_down:

    ld a, e_posx(ix)
    ld e_last_x(ix), a

    ld a, e_posy(ix)
    ld e_last_y(ix), a

    ld e_vy(ix), #3
    ChangeSpriteDirection _sprite_player_00
ret


key_actions:
    .dw Key_P,              move_right
    .dw Key_O,              move_left
    .dw Key_Q,              move_up
    .dw Key_A,              move_down
    .dw Key_CursorRight,    move_right
    .dw Key_CursorLeft,     move_left
    .dw Key_CursorUp,       move_up
    .dw Key_CursorDown,     move_down
    .dw Joy0_Up,            move_up
    .dw Joy0_Down,          move_down
    .dw Joy0_Left,          move_left
    .dw Joy0_Right,         move_right
    .dw 0

    key_l       = 0   ;;  Byte 0  \ KEY PRESSED
    key_h       = 1   ;;  Byte 1  /
    action_l    = 2   ;;  Byte 2  \ ACTION TO TAKE
    action_h    = 3   ;;  Byte 3  /


;;IX : tiene  direccion del player
input_check::

    ;;Reset velocity
    ld e_vx(ix), #0
    ld e_vy(ix), #0
    
    call cpct_scanKeyboard_f_asm

    ;;Key-Action Check loop
    ld iy, #key_actions-4

    loop_keys:
        ld  bc, #4              ;;  HL = NEXT KEY
        add iy, bc
        
        ld  l,  key_l(iy)       ;;  HL NEXT KEY
        ld  h,  key_h(iy)
        ;;Check if key == null
        ld  a,  l               ;;  a = H|L
        or  h;
        ret z                   ;;salir si es cero key =null
    
        call cpct_isKeyPressed_asm
        jr  z,loop_keys         ;;  Si no esta pulsada bucle
        ;;Key is pressed execute action
        ld hl, #loop_keys       ;;ret to loop_keys
        push hl
        ld  l, action_l(iy)     ;;HL = action function pointer
        ld  h, action_h(iy)  
        jp  (hl)                ;;salta a la direccion que hay en hl
        ;;jr loop_keys implicit in function ret
input_update::

    ld de, #input_cmps
    ld hl, #input_check
    call man_entity_for_all_matching

ret