;;
;; PHYSICS SYSTEM
;;
.include "cpctelera.h.s"
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area _CODE






;;INPUT 
physics_check_keyboard::

    ld e_vx(ix), #0
    ld e_vy(ix), #0
    ;ld e_at(ix), #0

    call cpct_scanKeyboard_f_asm
    ld hl, #Key_O
    call cpct_isKeyPressed_asm
    jr z, a_not_pressed 

a_pressed:   
    ld e_vx(ix), #-1
    ld e_dir(ix), #3
    jp s_not_pressed
a_not_pressed:



    ld hl, #Key_P
    call cpct_isKeyPressed_asm ;; 0 o 1 
    jr z, d_not_pressed 
    
d_pressed:
    ld e_vx(ix), #1
    ld e_dir(ix), #1
    jp s_not_pressed
d_not_pressed:
    ld hl, #Key_Q
    call cpct_isKeyPressed_asm ;; 0 o 1 
    jr z, w_not_pressed 

w_pressed:

    ld e_vy(ix), #-1
    ld e_dir(ix), #4
    jp s_not_pressed
w_not_pressed:
    ld hl, #Key_A
    call cpct_isKeyPressed_asm ;; 0 o 1 
    jr z, s_not_pressed 

s_pressed:
    ld e_vy(ix), #1
    ld e_dir(ix), #2
s_not_pressed:

ld hl, #Key_R
    call cpct_isKeyPressed_asm ;; 0 o 1 
    jr z, r_not_pressed 

r_pressed:
    call reset_completo_game
r_not_pressed:




ld hl, #Key_Space
    call cpct_isKeyPressed_asm ;; 0 o 1 
    jr z, p_not_pressed 

p_pressed:
    ld e_at(ix), #1
    call atack_controller
    
p_not_pressed:


;;MUSICA
ld hl, #Key_M
        call cpct_isKeyPressed_asm ;; 0 o 1 
        jr z, m_not_pressed 

    m_pressed:
        ld a, (enable_music)
        dec a
        jr z, desactivar_musica
            ;;ACTIVAR MUSICA
            ld a, #1
            ld (enable_music), a
        ret

        desactivar_musica:
            ;;DESACTIVAR MUSICA
            ld a, #0
            ld (enable_music), a
            ;call cpct_akp_stop_asm
    m_not_pressed:

ret


;;; UPDATE
physics_update_one_entity::

    ld b, #_e_type_movable
    ld a, e_tipo(ix) 
    and b      
    cp a,b      
    ret nz

    ld b, #_e_type_input
    ld a, e_tipo(ix) 
    and b      
    cp a,b      
    jr nz, _sigan
    
    call physics_check_keyboard

    ld a,e_atack_posx(ix)
    ld e_atack_pos_antx(ix),a

    ld a,e_atack_posy(ix)
    ld e_atack_pos_anty(ix),a

    _sigan:

    ld a, e_cont_no_hit(ix)
    or a
    jr z, ia_updates
    dec e_cont_no_hit(ix)


    ia_updates:
    ld b, #_e_type_ia
    ld a, e_tipo(ix) 
    and b      
    cp a,b      
    jr nz, _sigan2

;; UPDATES DE IA; No comportamientos
    

    disp:
    ;; caso archer
    ld b, #_e_type_ia_archer
    ld a, e_comportamiento_ia(ix) 
    and b      
    cp a,b      
    jr nz, vel_dec

    ld a, e_t_disparo(ix)
    or a
    jr z, vel_dec
    dec e_t_disparo(ix)
    ret



    vel_dec:
    dec e_cont_velocidad(ix)
    ret nz
    ld a, e_max_vel(ix)
    ld e_cont_velocidad(ix), a

    _sigan2:
    call megacontrolador
    
    call cambio_dir

    moverse:
    call preparar_borrado

    ld h, e_vx(ix)
    ld a, e_x(ix)
    add h
    ld e_x(ix), a


    ld b, e_vy(ix)
    ld c, #4
    call perubian_mult
    ld a, e_y(ix)
    add h
    ld e_y(ix), a

ret


perubian_mult:

ld a, #0
bucleMult:
    add a,b
    dec c
jr nz, bucleMult
ld h, a

ret



;;; LIMITES PANTALLA
megacontrolador:

   izq_der:
        ld h, e_vx(ix)
        inc h
        jr nz, der
        call controlar_izq
        jp arriba_abajo

        der:
        dec h
        dec h
        jr nz, arriba_abajo
        call controlar_der

    arriba_abajo:
        

        ld h, e_vy(ix)
        inc h
        jr nz, abajo
        call controlar_arriba
        jp fincont

        abajo:
        dec h
        dec h
        jr nz, fincont
        call controlar_abajo
    jp fincont

    




    controlar_izq:
        ld a, (limite_izq)
        ld b, a
        ld a,  e_x(ix)
        cp a,b

        jr nz, i_fin
            ld e_vx(ix), #0
        i_fin:
    ret

    controlar_der:
        ld a, (limite_der)
        ld b, a
        ld a,  e_x(ix)
        cp a,b

        jr nz, d_fin
            ld e_vx(ix), #0
        d_fin:
    ret



    controlar_arriba:
            
        ld a, (limite_up)
        ld b,a 
        ld a,  e_y(ix)
        cp a,b 
        

        jr c, quieto ;0
        

        jp u_fin 

        quieto:
            ld e_vy(ix), #0
            ;ld e_y(ix), #0 ;; POSIBLEMENTE QUITAR
            ret

        u_fin:
            ld e_vy(ix), #-1   
    ret

    controlar_abajo:
        ld a, (limite_down)
        ld b,  e_y(ix)
        cp a,b 
        jr z, quieto2 ;0
        inc b
        cp a,b
        jr z, quieto2; 1
        inc b
        cp a,b
        jr z, quieto2; 2
        inc b
        cp a,b
        jr z, quieto2; 3
        jp u_fin2 

        quieto2:
            ld e_vy(ix), #0
            ;ld a,(limite_down) 
            ;ld e_y(ix), a ;; POSIBLEMENTE QUITAR




            ld b, #_e_type_projectile
            ld a, e_tipo(ix) 
            and b      
            cp a,b      
            ret nz
            ld a, (_num_proyectiles)
            dec a 
            ld (_num_proyectiles), a

            ld e_tipo(ix), #_e_type_dead







            ret

        u_fin2:
            ld e_vy(ix), #1   
        
    ret


    fincont:


ret







physics_update::
    ld hl, #physics_update_one_entity
    ld d, #_e_type_movable
    call man_entity_forall
ret


;;; UPDATE POS BORRADO

preparar_borrado:
	;ld      de, #0xC000
    ld      c, e_x(ix) ;;x
    ld      b, e_y(ix) ;;y

    ld e_posicion_borrado(ix), c
    ld e_posicion_borrado+1(ix), b
ret






cambio_dir::
    ld a, e_dir(ix)
    ld b, e_dir_ant(ix)
    cp a,b
    ret z
    ld e_cont_anim(ix), #1
    call nuevo_sprite_dir
    ld a, e_dir(ix)
    ld e_dir_ant(ix),a
    
ret






;; UPDATES DEL ATAQUE
;
;
;






atack_controller:
    

    ld a, e_at_dir(ix)
    or a 
    ret nz

    

    ld a, e_dir(ix)
    ld e_at_dir(ix), a

    dec a
	jr z, der_izq_at
	dec a
	jr z, abajo_arriba_at
	dec a
	jr z, der_izq_at
	dec a
	jr z, abajo_arriba_at

    der_izq_at:
    ld e_atack_w(ix), #8
    ld e_atack_h(ix), #16 
	jr f_atack

	abajo_arriba_at:                  
	ld e_atack_w(ix), #8
    ld e_atack_h(ix), #16


	f_atack:

    ;call determinat_pos_atack
ret





determinat_pos_atack::

    ld e, #0

	ld a, e_at_dir(ix)
	dec a
	jr z, der_at
	dec a
	jr z, abajo_at
	dec a
	jr z, izq_at
	dec a
	jr z, arriba_at
    ret

	;; POSIBLEMMENTE A CAMBIAR

	der_at:
	ld      c, e_x(ix) ;;x
    ld 		a, #8
	add a, c
    push af
    ld a, (limite_der)
    ld b, a
    pop af
    cp a, b
    jr c, no_modifico_der
    ld a, b
    ld e, #1
    no_modifico_der:
    ld  e_atack_posx(ix),a
	ld      a, e_y(ix) ;;y
    ld  e_atack_posy(ix),a
	jp f_det_pos_at



	abajo_at:
    ld a, e_x(ix)
    ld e_atack_posx(ix), a
    ld 		b, #16
    ld      a, e_y(ix) ;;y
	add a, b
    push af 
    ld a, (limite_down)
    ld b, a
    pop af
    cp a,b
    jr c, no_modifico_abajo
    ld a,b
    ld e, #1
    no_modifico_abajo:
    ld  e_atack_posy(ix),a
	jp f_det_pos_at



	izq_at:
	ld      c, e_x(ix) ;;x
    ld 		a, #-8
	add a, c
    push af
    ld a, (limite_izq)
    ld b, a
    pop af
    cp a, b
    jr nc, no_modifico_izq
    ld a, b
    ld e, #1
    no_modifico_izq:
    ld  e_atack_posx(ix),a
	ld      a, e_y(ix) ;;y
    ld  e_atack_posy(ix),a
	jp f_det_pos_at

	arriba_at:
    ld a, e_x(ix)
    ld e_atack_posx(ix), a
    ld 		b, #-16
    ld      a, e_y(ix) ;;y
	add a, b
    push af 
    ld a, (limite_up)
    ld b, a
    pop af
    cp a,b
    jr nc, no_modifico_arriba
    ld a,b
    ld e, #1
    no_modifico_arriba:
    ld  e_atack_posy(ix),a


    f_det_pos_at:
    

ret




