;; INIT OF PHYSICS.S

.include "cpctelera.h.s"
.include "./manager/entities.h.s"
.include "./manager/rounds.h.s"
.include "cpct_globals.h.s"
.include "./entities/bullet.h.s"

.globl _sprite_char_D1
.globl _sprite_char_L1
.globl _sprite_char_U1
.globl _sprite_char_R1
.globl _sprite_char_D2
.globl _sprite_char_L2
.globl _sprite_char_U2
.globl _sprite_char_R2


cooldown: .db #0
shoot_dir: .db #2
is_shooting: .db #0

current_cool_1 == 29
current_cool_2 == 15
;; ----------------------------------------
;; Puts actions in certain keys
;; ----------------------------------------
keyactions:: ;;el eje horizontal tendra prioridad ya que hay mas enemigos de los lados
    .dw Key_Space, key_space_action
    .dw Joy0_Fire1, key_space_action
    .dw Key_Q, key_up_action
    .dw Key_A, key_down_action
    .dw Key_O, key_left_action
    .dw Key_P, key_right_action
    .dw Key_CursorUp, key_up_action
    .dw Key_CursorDown, key_down_action
    .dw Key_CursorLeft, key_left_action
    .dw Key_CursorRight, key_right_action
    .dw Joy0_Up, key_up_action
    .dw Joy0_Down, key_down_action
    .dw Joy0_Left, key_left_action
    .dw Joy0_Right, key_right_action
    .dw 0x0000

;;receives A
change_shoot_dir:
    ld hl, #is_shooting
    inc (hl)
    dec (hl)
    jr nz, exit_csd
    ld hl, #shoot_dir
    ld (hl), a
    exit_csd:
ret

restart_shoot_dir::
    ld hl, #shoot_dir
    ld (hl), #2
ret

;; ------------------------------------
;;  Horizontal movement
;; ------------------------------------

key_left_action::
    ld e_vx(ix), #-1      ;-1
    ld e_vx+1(ix), #0xA0  ;+0.A0 (hex) (-0.6hex) -0,35 mas o menos
    ld e_player_moving(ix), #1

    ld hl, #is_shooting
    inc (hl)
    dec (hl)
    jr z, fast_kla
        ld e_vx(ix), #-1
        ld e_vx+1(ix), #0xD0

    fast_kla:

    ld a, #3
    call change_shoot_dir
ret

key_right_action::
    ld e_vx(ix), #0       ;0
    ld e_vx+1(ix), #0x60  ;+0.60 (hex) (+0.6hex) 0,35 mas o menos
    ld e_player_moving(ix), #1


    ld hl, #is_shooting
    inc (hl)
    dec (hl)
    jr z, fast_kra
        ld e_vx(ix), #0
        ld e_vx+1(ix), #0x30

    fast_kra:

    ld a, #1
    call change_shoot_dir
ret

;; ------------------------------------
;;  Vertical movement
;; ------------------------------------
key_up_action::
    ld e_vy(ix), #-1
    ld e_player_moving(ix), #1

    ld hl, #is_shooting
    inc (hl)
    dec (hl)
    jr z, fast_kua
        ld e_vy(ix), #-1
        ld e_vy+1(ix), #0x80

    fast_kua:

    ld a, #0
    call change_shoot_dir
ret

key_down_action::
    ld e_vy(ix), #1
    ld e_player_moving(ix), #1

    ld hl, #is_shooting
    inc (hl)
    dec (hl)
    jr z, fast_kda
        ld e_vy(ix), #0
        ld e_vy+1(ix), #0x80

    fast_kda:
    ld a, #2
    call change_shoot_dir
ret

;; ------------------------------------
;;  Extra action: shoot
;; ------------------------------------

key_space_action::
    ld hl, #is_shooting
    ld (hl), #1

    ld hl, #cooldown
    inc (hl)
    dec (hl)
    jr nz, no_bullet

    call get_weapon
    cp #0
    jr z, es_pistola
    ld a, #current_cool_2
    jr end_space_weap
    es_pistola:
    ld a, #current_cool_1

    end_space_weap:
    ld (hl), a

    ld h, e_x(ix)
    ld l, e_y(ix)
    ld a, (#shoot_dir)
    push ix
    call create_bullet
    pop ix

    no_bullet:
ret

;;ix entidad
sys_input_check_keyboard_and_update_player::
    ld e_vx(ix), #0
    ld e_vy(ix), #0
    ld e_vx+1(ix), #0
    ld e_vy+1(ix), #0
    ld e_player_moving(ix), #0

    ld hl, #is_shooting
    ld (hl), #0

    ld hl, #cooldown
    inc (hl)
    dec (hl)
    jr z, cont_cool
    dec (hl)
    cont_cool:
    ld iy, #keyactions-4
    ;; Checks if any key is pressed
    nextkey:
        ld bc, #4
        add iy, bc
        
        ;; Check next key code
        ld l, (iy)
        ld h, 1(iy)
        
        ;; Check for null to end
        ld a, l
        or h
        ret z

        ;; Test Key and perform
        call cpct_isKeyPressed_asm
    jr z, nextkey

    ;; Key is pressed, perform key_left_action
    ld hl, #nextkey
    push hl
    ld l, 2(iy)
    ld h, 3(iy)
    jp (hl)

;; --------------------------------------
;;  Update all entities' physics
;;  B -> mask for filter
;; --------------------------------------

sys_input_update::
    ld hl, #sys_input_check_keyboard_and_update_player
    ld b, #e_cmps_input
    call man_entity_forall_matching
    call player_anim_update
ret


player_anim_update::
    ld a, #0
    call man_entity_get_from_idx

    ld a, e_player_moving(ix)
    cp #0
    jp z, stopped_moving

    ld a, (#shoot_dir)

    cp #0
    jr z, anim_up
    cp #1

    jr z, anim_right
    cp #2

    jr z, anim_down
    cp #3
    jr z, anim_left

    anim_up:
        ld a, e_player_anim_ctr(ix)
        dec a
        call z, restart_anim_player
        jr z, dont_update
        ld e_player_anim_ctr(ix), a

        dont_update:
        ld a, e_player_anim_ctr(ix)
        cp #11
        jr c, still_sprite_up;;sprite moviendose
        ld hl, #_sprite_char_U2
        jp assign_sprite
        still_sprite_up:;;sprite quieto
        ld hl, #_sprite_char_U1
        jp assign_sprite
    ret

    anim_right:
        ld a, e_player_anim_ctr(ix)
        dec a
        call z, restart_anim_player
        jr z, dont_update_r
        ld e_player_anim_ctr(ix), a

        dont_update_r:
        ld a, e_player_anim_ctr(ix)
        cp #11
        jr c, still_sprite_r;;sprite moviendose
        ld hl, #_sprite_char_R2
        jp assign_sprite
        still_sprite_r:;;sprite quieto
        ld hl, #_sprite_char_R1
        jp assign_sprite

    ret

    anim_down:
        ld a, e_player_anim_ctr(ix)
        dec a
        call z, restart_anim_player
        jr z, dont_update_d
        ld e_player_anim_ctr(ix), a

        dont_update_d:
        ld a, e_player_anim_ctr(ix)
        cp #11
        jr c, still_sprite_d;;sprite moviendose
        ld hl, #_sprite_char_D2
        jp assign_sprite
        still_sprite_d:;;sprite quieto
        ld hl, #_sprite_char_D1
        jp assign_sprite

    ret

    anim_left:
        ld a, e_player_anim_ctr(ix)
        dec a
        call z, restart_anim_player
        jr z, dont_update_l
        ld e_player_anim_ctr(ix), a

        dont_update_l:
        ld a, e_player_anim_ctr(ix)
        cp #11
        jr c, still_sprite_l;;sprite moviendose
        ld hl, #_sprite_char_L2
        jp assign_sprite
        still_sprite_l:;;sprite quieto
        ld hl, #_sprite_char_L1
        jp assign_sprite
    ret

    assign_sprite:
        ld e_sprite(ix), l
        ld e_sprite+1(ix), h
    ret

    stopped_moving:

    ld e_player_anim_ctr(ix), #1

    ld a, (#shoot_dir)

    cp #0
    jr z, sprite_up
    cp #1

    jr z, sprite_right
    cp #2

    jr z, sprite_down
    cp #3
    jr z, sprite_left

    sprite_up:
    ld hl, #_sprite_char_U1
    jr end_csd

    sprite_right:
    ld hl, #_sprite_char_R1

    jr end_csd

    sprite_down:
    ld hl, #_sprite_char_D1

    jr end_csd

    sprite_left:
    ld hl, #_sprite_char_L1

    end_csd:
    ld e_sprite(ix), l
    ld e_sprite+1(ix), h
ret

restart_anim_player::
    ld e_player_anim_ctr(ix), #22
ret

