;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; ANIMATION MANAGER ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;; INCLUDES ;;;;;;;;;;;;   
.include "cpctelera.h.s"
.include "man_animation.h.s"
.include "man_entity.h.s"
.include "../assets/assets.h.s"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;-----------------------------
;;  DEFINIMOS EL ARRAY DE
;;      ANIMACIONES
;;-----------------------------  

;;TEST
;;CreateAnimationArray3STEPS      player, _sprite_player_01, _sprite_player_02, _sprite_player_03
;;ANIMACIONES DEL PLAYER ANDANDO
CreateAnimationArray3STEPS      player_walking_down,    _sprite_player_01, _sprite_player_02, _sprite_player_03     ;;  ANIMACION DEL PLAYER ANDANDO HACIA ABAJO
CreateAnimationArray3STEPS      player_walking_up,      _sprite_player_05, _sprite_player_06, _sprite_player_07     ;;  ANIMACION DEL PLAYER ANDANDO HACIA ARRIBA
CreateAnimationArray3STEPS      player_walking_right,   _sprite_player_09, _sprite_player_10, _sprite_player_11     ;;  ANIMACION DEL PLAYER ANDANDO HACIA DERECHA
CreateAnimationArray3STEPS      player_walking_left,    _sprite_player_13, _sprite_player_14, _sprite_player_15     ;;  ANIMACION DEL PLAYER ANDANDO HACIA ARRIBA
;;ANIMACIONES DEL PLAYER ESTATICAS
CreateAnimationArray2STEPS      player_static_down,     _sprite_player_00, _sprite_player_01    ;;  ANIMACION PLAYER MIRA HACIA ABAJO ESTATICA
CreateAnimationArray2STEPS      player_static_up,       _sprite_player_04, _sprite_player_05    ;;  ANIMACION PLAYER MIRA HACIA ARRIBA  ESTATICA
CreateAnimationArray2STEPS      player_static_right,    _sprite_player_08, _sprite_player_09    ;;  ANIMACION PLAYER MIRA HACIA DERECHA  ESTATICA
CreateAnimationArray2STEPS      player_static_left,     _sprite_player_12, _sprite_player_13    ;;  ANIMACION PLAYER MIRA HACIA IZQUIERDA  ESTATICA
;;ANIMACiONES DE LOS ENEMGOS
CreateAnimationArray2STEPS      enemy1, _sprite_enemy_2, _sprite_enemy_3                    ;;MEDUSA
CreateAnimationArray2STEPS      enemy2, _sprite_enemy_4, _sprite_enemy_5                    ;;WITCH

animations_array:
    .dw player_static_down_anim
    .dw player_walking_left_anim
    .dw player_walking_right_anim
    .dw NULL
    .dw player_walking_down_anim
    .dw player_walking_down_anim
    .dw player_walking_down_anim
    .dw NULL
    .dw player_walking_up_anim
    .dw player_walking_up_anim
    .dw player_walking_up_anim
    .dw NULL
    .dw NULL
    .dw NULL
    .dw NULL
    .dw NULL
    .dw enemy1_anim
    .dw enemy1_anim
    .dw enemy1_anim
    .dw NULL
    .dw enemy1_anim
    .dw enemy1_anim
    .dw enemy1_anim
    .dw NULL
    .dw enemy1_anim
    .dw enemy1_anim
    .dw enemy1_anim
    .dw NULL
    .dw NULL
    .dw NULL
    .dw NULL
    .dw NULL
    .dw enemy2_anim
    .dw enemy2_anim
    .dw enemy2_anim
    .dw NULL
    .dw enemy2_anim
    .dw enemy2_anim
    .dw enemy2_anim
    .dw NULL
    .dw enemy2_anim
    .dw enemy2_anim
    .dw enemy2_anim

;; 0000     STANDING
;; 0001     LEFT
;; 0010     RIGHT
;; 0011     -----
;; 0100     DOWN
;; 0101     DOWN-LEFT
;; 0110     DOWN-RIGHT
;; 0111     -----
;; 1000     UP
;; 1001     UP-LEFT
;; 1010     UP-RIGHT
;; 1011     -----
;; 1100     -----
;; 1101     -----
;; 1110     -----
;; 1111     -----
;; 10000    ANIMENEMY1
;; 0001     
;; 0010     
;; 0011     -----
;; 0100     
;; 0101    
;; 0110     
;; 0111     -----
;; 1000    
;; 1001     
;; 1010     
;; 1011     -----
;; 1100     -----
;; 1101     -----
;; 1110     -----
;; 1111     -----
;;FUNCTION TO CHECK WHICH STATUS IS THE PLAYER ON AND THEN CHECK ITS VELOCITY
;; RETURN B: ANIMATION STATUS
man_anim_check_status:
    ld b , #0           ;; DEFAULT STATUS 0
    ld a, e_type(ix)
    and #type_e
    jr z, isPlayer
    isEnemy:
       
        ld a, e_cmp(ix)
        and #e_cmps_ai
        jr z, isEnemy2
        isEnemy1:
            ld b , #0x20
            jr isPlayer
        isEnemy2:
            ld b ,#0x10     ;;SIN IA ENEMY1
            
    isPlayer:
    ld a, e_vx(ix)      ;;  A = e_vx
    or a
    jr z, vx_zero
    vx_nozero:

        jp m, vx_negative
        
        vx_positive:     ;; SE MUEVE HACIA LA DERECHA
            set 1, b
            jr  vx_zero
        vx_negative:    ;; SE MUEVE A LA IZQ
            set 0 ,b
    vx_zero:

    ld a, e_vy(ix)      ;;  A = e_vy
    or a
    jr z, vy_zero

    vy_nozero:

        jp m, vy_negative
        
        vy_positive:     ;; SE MUEVE HACIA LA ABAJO
            set 2, b
            jr  vy_zero
        vy_negative:    ;; SE MUEVE A LA UP
            set 3 ,b
    vy_zero:

ret
;;INPUT
;;  B = ANIMATION STATUS
;;RETURN
;;  HL = POINTER TO ANIMATION ARRAY
man_animations_getAnimHL:
    
    ld  hl , #animations_array
    ld  a, b
    add a
    add_hl_a

    ld  a, (hl)
    inc hl
    ld  h, (hl)
    ld  l, a
    
ret

man_anim_animate:

        call man_anim_check_status
        ;;CHECK IF ANIMATION STATUS IS THE SAME AS BEFORE
        ld a, e_animation_status(ix)    ;; A = CURRENT STATUS
        cp b                            ;; CP NO MODIFICA A IF(A == B)
        jr z, continue_animation        ;;THEN

        newAnimationStatus:
            ld e_animation_status(ix),b ;; SAVE NEW ANIM STATUS

            ex de, hl
            call man_animations_getAnimHL;;
            ld e_anim_ptr_l(ix), l      ;;  Guarda el siguiente paso de la animacion 
            ld e_anim_ptr_h(ix), h      ;;  en la entidad
            ex de, hl

        continue_animation:

            ld e, e_anim_ptr_l(ix)      ;;  / DE: ACTUAL PASO ANIMACION
            ld d, e_anim_ptr_h(ix)      ;;  \
            ex de, hl                   ;;  POR COMODIDAD USAMOS HL
        
        returnTo_anim:
            ld c, (hl)                  ;;  /           <--- EN C CARGAMOS LA PARTE BAJA DE LA DIRECCION DE MEMORIA DEL SPRITE1 EN EL ARRAY ANIM
            inc hl                      ;;  | BC = CURRENT SPRITE
            ld b, (hl)                  ;;  \           <--- EN B CARGAMOS EL SIGUIENTE BYTE DE LA DIRMEMORY DEL SPRITE1
            inc hl                      ;;  HL =+ 2 --> APUNTA AL SIGUIENTE SPRITE DEL ARRAY ANIMACION

            ld a, c                     ;;  /
            or b                        ;;  |   IF(CurrentSprite (BC) == NULL) 
            jr z, reset_anim            ;;  \       resetAnimation

            ld e_anim_ptr_l(ix), l      ;;  Guarda el siguiente paso de la animacion 
            ld e_anim_ptr_h(ix), h      ;;  en la entidad

            ld  e_sprite_ptr_l(ix), C   ;;  Guarda el puntero del siguiente sprite
            ld  e_sprite_ptr_h(ix), b   ;;  en la entidad

            ex de, hl
            ret

        reset_anim:
            ld  a, (hl)             ;;  /
            inc hl                  ;;  |   HL = (*HL)
            ld  h, (hl)             ;;  |   HL Apunta al primer paso de la animacion
            ld  l, a                ;;  \
            jr  returnTo_anim       ;;

        ;;ld e_sprite_ptr_l(ix), #<_sprite_player_04        ;;  / DE: ACTUAL PASO ANIMACION
        ;;ld e_sprite_ptr_h(ix), #>_sprite_player_04        ;;  \   



man_anim_update::

    ld de, #e_cmp_animation
    ld hl, #man_anim_animate
    call man_entity_for_all_matching

ret 
