.include "man/entity_manager.h.s"
.include "cpctelera_functions.h.s"
.include "cpctelera.h.s"
.include "sys/input_system.h.s"
.include "sys/render_system.h.s"
.include "man/game_manager.h.s"


;; Inicializamos el sistema de input
input_system_init::
    ret

;; INPUT
;; IX: Puntero a la entidad[0]
input_system_update::
    ;; Ponemos las velocidades de las entidades
    ;; a cero
    ld      e_vx(ix), #0
    ld      e_vy(ix), #0

    call    cpct_scanKeyboard_f_asm
    
    ;; Vemos que teclas hay pulsadas
    ld      hl, #Key_O
    call    cpct_isKeyPressed_asm
    jr      z, O_NotPressed

O_pressed:
    ld      e_vx(ix), #-1

O_NotPressed:
    ld      hl, #Key_P
    call    cpct_isKeyPressed_asm
    jr      z, P_NotPressed

P_pressed:
    ld      e_vx(ix), #1

P_NotPressed:
    ld      hl, #Key_Q
    call    cpct_isKeyPressed_asm
    jr      z, Q_NotPressed

Q_pressed:
    ld      e_vy(ix), #-2


Q_NotPressed:
    ld      hl, #Key_A
    call    cpct_isKeyPressed_asm
    jr      z, A_NotPressed

A_pressed:
    ld      e_vy(ix), #2

A_NotPressed:
    ;;ld      e_ai_aim_x(ix), #0 ;; Posicion a 0
    ld      hl, #Key_Space
    call    cpct_isKeyPressed_asm
    jr      z, Space_NotPressed
    ;;ret 

Space_Pressed:
    ld      e_vy(ix), #-4
    ;;ld      e_ai_aim_x(ix), #1 ;; Las entidades con IA (y move_to) van hacia mi

Space_NotPressed:
    ld      hl, #Key_Esc
    call    cpct_isKeyPressed_asm
    jr      z, Escape_NotPressed


Escape_Pressed:
;; PONEMOS TODO COMO AL PRINCIPIO Y SALIMOS AL MENU

 ;; Limpiamos la memoria de video
    ld      hl, #0xC000
    ld      de, #0xC001
    ld      bc, #0x4000-1
    ld      (hl), #0
    ldir
 
  
    ;; player posicion
    ld      hl, #0xA023
    ld  e_x(ix), l
    ld  e_y(ix), h
    
    ;; Borramos sprites y reniciamos los sistemas
    call  entity_manager_getEntityArray
    call  render_system_eraseAll
    call  game_manager_init_systems
    call  entity_manager_getEntityArray

    ;;aumentamos ix para que apunte al primer asteroide
    ld      de, #sizeof_e
    add     ix, de
    add     ix, de

    ;;#enemy2
    ld      hl, #0x231B
    ld  e_x(ix), l
    ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;#enemy3
    ld      hl, #0x501B
    ld  e_x(ix), l
    ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;#enemy4
    ld      hl, #0x2837
    ld  e_x(ix), l
    ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;#enemy5
    ld      hl, #0x280C
    ld  e_x(ix), l
    ld  e_y(ix), h
    

    

Escape_NotPressed:
    ret