;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of Viking Hangover
;;  Copyright (C) 2021 Ahmed Begga / Érika Moreno / Adriana Morejón (@ComputosGame)
;;
;;  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/>.
;;-------------------------------------------------------------------------------

.include "colisions.h.s"
.include "generator.h.s"
.include "man/entity.h.s"
.include "man/game.h.s"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;   Checks if any entity colided                        ;;
;;                                                       ;;
;;Required:                                              ;;
;;     IX                                                ;;  
;;     IY                                                ;;
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_colisions_update_one:
    ;;Aqui comprobamos que nos se ha chocado en el eje x
    call viking_colides_shot
    ret nc
    
    inc a
    ld c, e_w(iy)
    ld b, e_x(ix) 
    add a, c
    cp b
    ret c
    ;;Aqui comprobamos que nos se ha chocado en el eje y
    call viking_colides_shot_y
    ret nc

    inc a
    ld c, e_h(iy)
    add a, c
    cp b
    ret c
         
    ld a, e_subType(iy)
    call sys_colisions_typecoli
    ld a, e_subType(ix)
    call _switch_array
ret
viking_colides_shot:
    ld a, e_x(iy)      
    dec a
    push af
    
    ld b, e_w(ix)
    ld a, e_x(ix)      
    add a, b
    ld b, a
    pop af
    cp b
    ret
viking_colides_shot_y:
    ld a, e_y(iy)      
    ld b, e_y(ix)      
    dec a
    cp b
    ret
_switch_array:
    push ix         
    push iy
    pop ix
    call sys_colisions_typecoli
    push ix         
    pop iy
    pop ix
    ret
sys_colisions_typecoli:
    ld h, e_coli1(ix)
    ld l, e_coli2(ix)
    ld bc, #_out_coli
    push bc
    jp (hl)
    _out_coli:
ret

sys_colisions_destroy:
    call man_entity_set4_destroy
ret
sys_colisions_shot:
    call sys_generator_expShot
    call sys_colisions_enemy
ret
sys_colisions_enemy:
    call man_entity_set4_destroy
ret
sys_colisions_sub_enemy:
    call man_entity_set4_destroy
ret

sys_colisions_bottle:
    call man_entity_set4_destroy
    call man_game_player_colision_bottle
ret

sys_colisions_bottle_reward:
    call man_entity_set4_destroy
ret

sys_colisions_player:
    ld b, #type_shot
    and b
    ret z
    call man_game_player_colision_shot
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;  updates all the entities with the flag e_cmp_colision;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_colisions_update::
    ld hl, #sys_colisions_update_one
    ld a, #e_cmp_colide
    ld b, #e_cmp_colision
    call man_entity_forall_match
ret