;; MIT License
;; 
;; Copyright (c) 2021 capAI / Aitana Devis  Ávila (@AitanaDevis) / Carlos Peñarrubia Morcillo  (@carlospm12) / Pablo Ruiz Ponce (@pabloruizp)
;; 
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; 
;; The above copyright notice and this permission notice shall be included in all
;; copies or substantial portions of the Software.
;; 
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.

.include "man_animations.h.s"
.include "entity.h.s"
.include "cpctelera.h.s"

.globl man_animations_getAnimHL

.area _CODE


;;////////////////////////////////////////////////////////
;;  MAN_ANIMATION_UPDATE: Updates the current animation
;;////////////////////////////////////////////////////////
man_animation_update::
    ld ix, #_player
    call calculateAnimState
    ld a, P_ANIM_STATE(ix) ;;miramos si estamos en el estado correcto para continuar con la animación o cambiar el estado
    cp b
    jr z, _continueAnim ;;si es el correcto continuamos con la animación

_newAnimationState: ;;si no son iguales tengo que establecer un estado de animación distinto
    ld P_ANIM_STATE(ix), b

    call man_animations_getAnimHL ;;obtenemos el array de animaciones en HL
    ld P_ANIM(ix), l
    ld P_ANIM+1(ix), h

    jr _nextSprite

_continueAnim:
    ld l, P_ANIM(ix) ;;store next animation step into the player
    ld h, P_ANIM+1(ix)


_return2anim:
    ld a, P_ANIM_COUNTER(ix) ;;accedemos als contador para calcular num frames
    dec a
    jr z, _nextSprite ;;si counter = 0 cambiamos el sprite

    ld P_ANIM_COUNTER(ix), a ;;actualizamos contador
    ret

_nextSprite:
    ld P_ANIM_COUNTER(ix), #defaultFrames ;;volvemos a poner contador en valor inicial

    ld c, (hl)  ;;BC = current sprite
    inc hl
    ld b, (hl)
    inc hl   ;;HL+=2 points to next anim element


    ld a, c  ;;if current sprite (BC) == nullptr then resets the anim
    or b
    jr z, _resetAnim

    
_nextAnim: ;;siguiente animacion
    ld P_ANIM(ix), l ;;store next animation step
    ld P_ANIM+1(ix), h

    ld P_SPR(ix), c ;;store next sprite pointer
    ld P_SPR+1(ix), b

    ret

_resetAnim:
    ld a, (hl) ;;HL = (*HL) meto en HL aquello a lo q apunta HL para apuntar al ppio de la animación
    inc hl
    ld h, (hl) ;;HL points to the first step in the animation
    ld l, a

    jr _nextSprite




;;//////////////////////////////////////////////////////////////////////////////////////////////
;; CALCULATE ANIM STATE: In order to change the animation depending on the state of the player 
;; INPUT
;;      IX = Entity_t
;; OUTPUT
;;      B = Anim STATE
;; DESTROYS: AF
;//////////////////////////////////////////////////////////////////////////////////////////////
calculateAnimState:
    ld b, #0 ;;0=original state (standing)

    ld a, P_JUMP(ix) ;;si está a 0 es que no está saltando
    or a
    jr z, _not_jumping

_jumping:
    set 3, b ;;si está saltando ponemos a 1 el 3er bit de b(el estado)
    
_not_jumping: ;;comprobamos si está agachado o corriendo
    ld a, P_STATE(ix)
    or a
    jr z, _running ;;si da 0 está corriendo

_crouched:
    set 2, b
    
_running: ;;si está corriendo tiene que tener todos los bits a 0
    
    ret