
.include "./manager/entities.h.s"
.include "./manager/rounds.h.s"
.globl _sprite_char
b_cmps == e_cmps_position | e_cmps_alive | e_cmps_physics | e_cmps_render


;;                   cmps - x - y - vx - vy - w - h - color - e_type- -prevpos  
;bullet_entity::   .db cmps, 0, 0, #0, #0, #0x01, #0x04, #0x0F, e_type_bullet, #0, #0, #e_ai_st_noAI
;                    .dw _sprite_char, #0x0000
bullet_entity:: .db b_cmps
.ds sizeof_e-1

bullet_speed_mach == 3
bullet_speed_pist == 2

;;recibir en h->x en l->y
;;recibir en a velocidad
create_bullet::
    ld ix, #bullet_entity
    
    ld bc, #0x0207; para que aparezca sobre el jugador
    add hl,bc
    ld e_x(ix), h
    ld e_y(ix), l

    ld e_fs1(ix), #0x3F     ;; Color de la bala

    ld e_prv_ptr(ix), #0x64
    ld e_prv_ptr+1(ix), #0xE5

    ld e_w(ix), #01
    ld e_h(ix), #04
    ld e_type(ix), #e_type_bullet
    ld e_lifes(ix), #1
    ld e_bullet_ctr(ix), #1

    cp #0
    jp z, up
    cp #1
    jp z, right
    cp #2
    jp z, down
    cp #3   
    jp z, left

    up:
        call get_weapon
        cp #0
        jr z, es_pist_up

        ld e_vy(ix), #-3*bullet_speed_mach
        ld e_vy+1(ix), #0x80
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0
        ld e_bullet_v(ix), #0
        cp #2
        jp c, finish_config_bullet
        ld e_lifes(ix), #2
        jp finish_config_bullet
        es_pist_up:

        ld e_vy(ix), #-3*bullet_speed_pist
        ld e_vy+1(ix), #0x80
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0
        ld e_bullet_v(ix), #0

        jp finish_config_bullet

    down:

        call get_weapon
        cp #0
        jr z, es_pist_down

        ld e_vy(ix), #2*bullet_speed_mach
        ld e_vy+1(ix), #0x80
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0
        ld e_bullet_v(ix), #2
        cp #2
        jp nz, finish_config_bullet
        ld e_lifes(ix), #2

        jp finish_config_bullet

        es_pist_down:

        ld e_vy(ix), #2*bullet_speed_pist
        ld e_vy+1(ix), #0x80
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0
        ld e_bullet_v(ix), #2

        jr finish_config_bullet

    left:
        call get_weapon
        cp #0
        jr z, es_pist_left

        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld e_vx(ix), #-1*bullet_speed_mach
        ld e_vx+1(ix), #0x00
        ld e_bullet_v(ix), #3
        cp #2
        jp nz, finish_config_bullet
        ld e_lifes(ix), #2

        jp finish_config_bullet

        es_pist_left:

        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld e_vx(ix), #-1*bullet_speed_pist
        ld e_vx+1(ix), #0x00
        ld e_bullet_v(ix), #3

        jr finish_config_bullet


    right:

        call get_weapon
        cp #0
        jr z, es_pist_right

        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld e_vx(ix), #1*bullet_speed_mach
        ld e_vx+1(ix), #0x00
        ld e_bullet_v(ix), #1
        cp #2
        jp nz, finish_config_bullet
        ld e_lifes(ix), #2
        jp finish_config_bullet

        es_pist_right:

        ld e_vy(ix), #0
        ld e_vy+1(ix), #0
        ld e_vx(ix), #1*bullet_speed_pist
        ld e_vx+1(ix), #0x00
        ld e_bullet_v(ix), #1

    finish_config_bullet:

    ld hl, #bullet_entity
    call man_entity_create
ret

;; ----------------------------------
;; Actualiza el estado de una bala
;; Destruye: A
;;

bullet_update::
    ld a, e_type(ix)
    cp #e_type_bullet
    jr nz, not_colision
    ld a, e_bullet_ctr(ix)
    dec a
    jr z, not_colision
        ld e_bullet_ctr(ix), a
        ret
    not_colision:
ret

;; ----------------------------------
;; Actualiza todas las balas
;; Destruye:
;;

sys_bullet_update::
    
    ld hl, #bullet_update
    ld b, #e_cmps_physics ;; Condition: entity must have physics
    call man_entity_forall_matching
ret
