;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of Viking Hangover
;;  Copyright (C) 2021 Ahmed Begga / Érika Moreno / Adriana Morejón (@ComputosGame)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;-------------------------------------------------------------------------------

.include "cpctelera.h.s"
.include "input.h.s"
.include "render.h.s"
 
.include "man/game.h.s"
.include "man/entity.h.s"


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;  VARIABLES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
delay:
    .db #del    
change_renderable::
    .db #0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;    Modifies the vx or vy, pressing the bottons        ;;
;;Required:                                              ;;
;;     IX -> Position of the array entities              ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_input_update_one:
    call sys_input_change_rend
    ld e_vx(ix), #0
    ld e_vy(ix), #0
    call cpct_scanKeyboard_asm
    ld a, (#delay)
    or a

    jr z, _check_keyboard

    dec a
    ld (delay), a
    
    jr nz, _keep_checking

_check_keyboard:
    ld hl, #Key_A
    call cpct_isKeyPressed_asm
    jr nz, _q_pressed
    
    ld hl, #Key_Q
    call cpct_isKeyPressed_asm
    jr nz, _a_pressed

    ld hl, #Key_P
    call cpct_isKeyPressed_asm
    jr nz, _p_pressed

    ld hl, #Key_O
    call cpct_isKeyPressed_asm
    jr nz, _o_pressed

    jp _keep_checking
    _o_pressed:
        ld e_vx(ix), #-3
        ld a, #del
        ld (delay), a
        jp _keep_checking
    _p_pressed:
        ld e_vx(ix), #3
        ld a, #del
        ld (delay), a
        jp _keep_checking
    _a_pressed:

        ld e_vy(ix), #-32
        ld a, #del
        ld (delay), a
        jp _keep_checking
    _q_pressed:
        ld e_vy(ix), #32
        ld a, #del
        ld (delay), a
        jp _keep_checking
_keep_checking:
ret

sys_input_change_rend:
    ld a, (#change_renderable)
    or a
    ret z
    call desactiva_rend
    call sys_render_delete
ret
desactiva_rend:
    ld a, #0
    ld (change_renderable), a
    ld a, e_type(ix)
    ld b, #e_cmp_render
    xor b
    ld e_type(ix), a
    ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;    Updates all physics                                ;;
;;Required:                                              ;;
;;     IX -> Position of the array entities              ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_input_update::
    ld a, #e_cmp_input
    ld hl, #sys_input_update_one
    call man_entity_forall
ret