;;-----------------------------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 "physics.h.s"
.include "man/entity.h.s"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Modifies every entity with mov flag, updating vx ;;
;;     and vy                                            ;;
;;Required:                                              ;;
;;     IX -> Position of the array entities              ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_update_one:
    call mueve_x
    push af
    
    ld a, #76
    pop de   
    cp d
    jp c, _set2destroy
    ld e_x(ix), d

    call mueve_y
    dec a
    cp c
    ret nc
    inc a
    ld e_y(ix), a
    ret
_set2destroy:
    ld h, e_phy1(ix)
    ld l, e_phy2(ix)
    ld bc, #_out_physics
    push bc
    jp (hl)
    _out_physics:
    ret
mueve_x:
    ld b,e_vx(ix)
    ld a,e_x(ix)
    add a,b
    ret
mueve_y:
    ld b, e_vy(ix)
    ld a, e_y(ix)
    add a, b
    ld c, #138
    ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;    Updates all physics                                ;;
;;Required:                                              ;;
;;     IX -> Position of the array entities              ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_update::
    ld hl, #sys_physics_update_one
    ld a, #e_cmp_mov
    call man_entity_forall
    ret