;; This file is part of Moonsters
;;  Copyright (C) 2021 Dam Studios
;;
;;     Moonsters is free software: you can redistribute it and/or modify
;;     it under the terms of the GNU General Public License as published by
;;     the Free Software Foundation, either version 3 of the License, or
;;     (at your option) any later version.
;; 
;;     SHOOT TO MOVE 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 General Public License for more details.
;; 
;;     You should have received a copy of the GNU General Public License
;;     along with Moonsters  If not, see <https://www.gnu.org/licenses/>.


.include "man/entity_manager.h.s"
.include "sys/input.h.s"
.include "cpctelera.h.s"
.include "assets/assets.h.s"


DefineEntity bala , e_type_bullet, e_render_visible, 30, 12, 2, 1, 0, 0, _sp_alien, e_ai_noAI, 0
contador_balas:
    .db #40
shoot_pulsado::

    ld a, (contador_balas)
    cp #40
    jr nz, no_disparar

    ld iy, #bala
    ld a, e_x(ix)
    ld e_x(iy), a
    ld a, e_y(ix)
    add #10
    ld e_y(iy), a

    ld a, e_vx(ix)
    cp #00
    jr nz, con_velocidad
    ld a, #01
con_velocidad:

    ld e_vx(iy), a

    ld hl, #bala
    call man_entity_create

    ld a, #0x00
    ld (contador_balas), a

no_disparar:

    ret

sys_input_update::


    ld a, (contador_balas)
    cp #40
    jr z, no_incrementar
    ld a, (contador_balas)
    inc a
    ld (contador_balas), a
no_incrementar:

    call man_entity_getArray
    call cpct_scanKeyboard_asm
   

    ;;Jumping 
        ld hl, #Key_Q
        call cpct_isKeyPressed_asm
        call nz, tecla_up_pulsada

        ld a, #0
        ld e_vx(ix), a
    ;;Derecha
        ld hl, #Key_P
        call cpct_isKeyPressed_asm
        call nz, tecla_right_pulsada
        ;call tecla_r_l_soltada
    
    ;;Izquierda
        ld hl, #Key_O
        call cpct_isKeyPressed_asm
        call nz, tecla_left_pulsada
        ;call tecla_r_l_soltada

         call jump_control
    ;;Hero shoot
        ld hl, #Key_Space
        call cpct_isKeyPressed_asm
        jr nz, shoot_pulsado

ret





tecla_right_pulsada::
    ld a, #1
    ld e_vx(ix), a
ret

tecla_left_pulsada::
    ld a, #-1
    ld e_vx(ix), a  
ret


tecla_up_pulsada::
    
    call start_jump

ret

estado_salto: .db #-1
;; Jump table
jump_table:
    .db #-2, #-2, #-2, #-2
    .db #-2, #-2, #-2, #-2
    .db #-2, #-2, #-2, #-2
    .db #-2, #-2, #-2, #-2
    .db #-2, #-2, #-2, #-2
    .db #-1, #-1, #-1, #-1
    .db #-1, #01, #01, #01
    .db #01, #01, #02, #02
    .db #02, #02, #02, #02
    .db #02, #02, #02, #02
    .db #02, #02, #02, #02
    .db #02, #02, #02, #02
    .db #02, #02, #02, #02
    .db #0x80


jump_control::

    ld a, (estado_salto)
    cp #-1
    ret z ;;si no salta no hacemos nada

    ;;Move
    ld hl, #jump_table      ;;accede a la dirección de memoria
    ld c, a
    ld b, #0               ;; bc = a
    add hl, bc

    ;;Check if end jump
    ld a, (hl)
    cp #0x80            ;; a == 0x80 -> end jump
    jr nz, no_end_of_jump

    ld a, (estado_salto)
    dec a 
    ld (estado_salto), a    
    ld a, #2

no_end_of_jump:
    ;;Movimiento del salto
    ld b, a             ;; Valor de hl
    add a, b
    ld e_vy(ix), a

    ;;Estado de salto
    ld a, (estado_salto)
    inc a 
    ld (estado_salto), a

    call check_surface

ret


start_jump::
    ld a, e_vy(ix)
    cp #0
    ret nz

    ld a, #0
    ld (estado_salto), a 

ret



check_surface::
    ld a, e_y(ix)
    ld b, e_vy(ix)
    add a, b
    sub #174
    ret c
    ;;aqui esta si es menor de 174

   
    ;;comprobamos si es menor de 200
    ld a, e_y(ix)
    ld b, e_vy(ix)
    add a, b
    sub #200
    jr nc, ponerYaCero



    ld a, e_vy(ix)
    cp #-4
    ret z

    ld e_vy(ix), #0
    ld e_y(ix), #174
ret


ponerYaCero:

    ld a, e_oldY(ix)
    ld e_y(ix), a

    ret