;;-----------------------------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 "physics.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 physics 
;; Objetive: Update the physics for one entity.
;;
;; Modifies: -
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_update_one_entity:
	ld de, #position_after_physics_behaviour
	push de

	;;Jump into the input_behaviour of the entity
	ld l, e_physics_bhv_b1(ix)
	ld h, e_physics_bhv_b2(ix)

    ;;Storing the next position in x axis on register A
    ld  a, e_pos_x(ix)
    add a, e_vel_x(ix)

	jp (hl)
	position_after_physics_behaviour:
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  -
;; Objetive: Update the physics for all the entities matching with the signature
;; Modifies: HL, A, C
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
signature_physics = (e_cmp_movable)
sys_physics_update::
    ld hl, #sys_physics_update_one_entity

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

    call man_entity_forall_matching
ret

;;===================================================================
;; PHYSICS BEHAVIOURS
;;===================================================================

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - A: should contain the pos_x + vel_x
;; Objetive: Physics behaviour for entitys that use gravity to fall (Players)
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_behaviour_gravity::

    IS_EQUALS -1
    jr z, update_y_axis_gravity

    IS_EQUALS max_player_right_border
    jr z, update_y_axis_gravity

    ;;Update X axis
    ld e_pos_x(ix), a

    update_y_axis_gravity:

    ld  a, e_pos_y(ix)
    add a, e_vel_y(ix)

    ld e_pos_y(ix), a

    ld a, e_is_grounded(ix)
    IS_EQUALS 1
    jr nz, not_grounded

    ;;grounded:
    ld e_vel_y(ix), #0x00
    ld a, e_last_position_y(ix)
    ld e_pos_y(ix), a
    ret

    not_grounded:
        ld a, e_vel_y(ix)
        inc a
        ld e_vel_y(ix), a
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - A: should contain the pos_x + vel_x
;; Objetive: Physics behaviour for entitys that dont use gravity to fall (Enemies)
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_behaviour_no_gravity::

    ;;Update X axis
    ld e_pos_x(ix), a

    ;;Update y axis
    ld  a, e_pos_y(ix)
    add a, e_vel_y(ix)

    ld e_pos_y(ix), a
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - A: should contain the pos_x + vel_x
;; Objetive: Physics behaviour for bullets
;; Modifies: A
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
sys_physics_behaviour_bullet::
    ;;Is the bullet < than the left border?
    IS_HIGHER 0xFC
    jr nc, set_to_destroy

    ;;Is the bullet > than right border?
    IS_HIGHER max_bullet_right_border
    jr nc, set_to_destroy

    ;;Update the bullet's x pos
    ld e_pos_x(ix), a

    ret
    ;;If the bullet needs to be destroyed, call the entity destroyer
    set_to_destroy:
        call man_entity_set4destruction
ret