;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  This program is free software: you can redistribute it and/or modify
;;  it under the terms of the GNU Lesser General Public License as published by
;;  the Free Software Foundation, either version 3 of the License, or
;;  (at your option) any later version.
;;
;;  This program is distributed in the hope that it will be useful,
;;  but WITHOUT ANY WARRANTY; without even the implied warranty of
;;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;  GNU Lesser General Public License for more details.
;;
;;  You should have received a copy of the GNU Lesser General Public License
;;  along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "animation.h.s"

;;Managers
.include "man/entity.h.s"
.include "man/game.h.s"

;;Macros
.include "macros/math.h.s"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should contain the entity's memory direction we want to update the input 
;; Objetive: Update the animation for one entity.
;;
;; Modifies: -
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_animation_update_one_entity:

    ;;Check if the entity has an animation right now, if it hasnt, ret
    ld a, e_anim_b2(ix)
	IS_EQUALS 00
    ret z

    ;;Entity has an animation to play
    ld a, e_anim_counter(ix)    ;;
    dec a                       ;; --e->anim_counter
    ld e_anim_counter(ix), a    ;;
    
    ;;Check if our anim counter == 0 to change the animation
    IS_EQUALS 0
    ret nz

    ;;Our animation counter is == 0

    ;;e->anim_counter == 0, we should change the sprite
    ld l, e_anim_b1(ix)
    ld h, e_anim_b2(ix)

    ;;HL pointing to e->anim

    ;;++ e->anim
    INCREMENT_REGISTER_2_BYTES hl, 3    ;;This is the necessary padding to jump to the next animation time

    ld e_anim_b1(ix), l
    ld e_anim_b2(ix), h

    ld a, (hl)  ;;A contains the e->anim->time 
    IS_EQUALS 0
    jr nz, e_anim_time_not_zero

    ;;e_anim_time_zero
    
    ;;In case our entity is set to e_type_dying_animation
    ;;We need to play the animation until the end
    ;;Once we reach the end, destroy de entity
    IS_ENTITY_GIVEN_TYPE_IX e_type_dying_animation
    jr z, e_not_dying_animation
    
    ;;In case the entity is a bullet 
    IS_ENTITY_GIVEN_TYPE_IX e_type_bullet
    jr nz, e_bullet_is_dead

    ;;e_player_is_dead:
    call man_game_finish_game
    ret

    e_bullet_is_dead:
    ;;Destroy it to allow the player to shoot more bullets
    call man_entity_set4destruction
    ret

    e_not_dying_animation:
    
    ;;Now, HL is pointing to the next sprite time, entity anim should point at the same direction
    inc hl                    ;;
    ld a, (hl)                ;;
    ld e_anim_b1(ix), a       ;;
                              ;; ======> e->anin = e->anim->val.next
    inc hl                    ;;
    ld a, (hl)                ;;
    ld e_anim_b2(ix), a       ;;

    ld l, e_anim_b1(ix)
    ld h, e_anim_b2(ix)

    e_anim_time_not_zero:

    ld a, (hl)                  ;;
    ld e_anim_counter(ix), a    ;; e->anim_counter = e->anim->time


    inc hl                      ;;
    ld a, (hl)                  ;;
    ld e_sprite_b1(ix), a       ;;
                                ;; ======> e->sprite = e->anim->val.sprite
    inc hl                      ;;
    ld a, (hl)                  ;;
    ld e_sprite_b2(ix), a       ;;

ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Update the animation for all the entities matching with the signature
;; Modifies: HL, A, C
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
signature_animation = (e_cmp_animation | e_cmp_render)
sys_animation_update::
    ld hl, #sys_animation_update_one_entity

    ;;C will contain the signature for the man_entity_forall_matching
    ld  a, #signature_animation
    ld c, a

    call man_entity_forall_matching
ret
