;;;;;;;;;;;;;-------LICENSE NOTICE-----------;;;;;;;;;;;;;;;
;; MIT License

;; Copyright (c) 2021 DreamyZombies Studio

;; 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.
;;
;; Authors:
;;  Abderrahamane Guermat Benaouali (agb139@alu.ua.es)
;;  Jorge Angulo Espí (jae17@alu.ua.es)
;;  Sami Hadj-Djilani (sh101@alu.ua.es)


;;
;; PHYSICS SYSTEM
;;

.include "../man/input.h.s"
.include "../cpct_functions.h.s"
.include "../sys/render.h.s"

.globl map_ptr
.globl _aceleration
.globl _pixelMovs
.globl block_check_up

;;  INPUT:
;;     IX: Pointer to hero.
sys_update_entities::

    call is_jumping
    call graviti_force

    ret

is_jumping::
    ld  a, (_jumps)
    dec a
    ret z

    ld  (_jumps), a

    ld a, #6
    ld e, a
jumps_loop:
    
    push de
    call can_move_hero_up
    pop de

    jp nz, block_check_up
    ret nz
    ld a, 1(ix)
    sub a, #1
    ld 1(ix), a
    
    dec e
    ret z
    jp jumps_loop


graviti_force::

    call updateDown

    ret

moveRight::

    call can_move_hero_rigth
    ret nz
    ;;call renderSys_remove_hero
    ld a, 0(ix)
    add a, #1
    ld 0(ix), a

    ret

moveLeft::

    call can_move_hero_left
    ret nz
    ;;call renderSys_remove_hero
    ld a, 0(ix)
    sub a, #1
    ld 0(ix), a

    ret

;;  INPUT:
;;     IX: Pointer to block entities.
can_move_hero_up::
;; ////////////////////////////// 
    ;; tx= px/4
    ;; ty= py/8
    ;; tw= ancho del tilemap
    ;; p= posTilemapinicial+ty*tw+tx
    ld a, 0(ix)     ;; a=px
    srl a
    ;;srl a 
    srl a           ;; a=(px)/4=tx
    ld b, a         ;; b=(px)/4=tx

    ld a, 1(ix)     ;; a=py
    sub a, #1       ;; a=py+pvy(en este caso menos uno)
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    
    ret nz

    ld a, 0(ix)     ;; a=px
    ld b, 2(ix)     ;; b=pw
    srl b
    srl b
    srl b
    add a, b    ;; a=px+pw
    sub a, #1       ;; a=px+pw
    srl a
    ;;srl a 
    srl a           ;; a=(px)/4=tx
    ld b, a         ;; b=(px)/4=tx

    ld a, 1(ix)     ;; a=py
    sub a, #1       ;; a=py+pvy(en este caso menos uno)
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    ret

;;  INPUT:
;;     IX: Pointer to block entities.
can_move_hero_down::

    ;;ld a, 1(ix)
    ;;add a, #2
    ;;sub #165
;; ////////////////////////////// 
    ;; tx= px/4
    ;; ty= py/8
    ;; tw= ancho del tilemap
    ;; p= posTilemapinicial+ty*tw+tx
    ld a, 0(ix)     ;; a=px
    srl a
    ;;srl a 
    srl a           ;; a=(px)/4=tx
    ld b, a         ;; b=(px)/4=tx

    ld a, 1(ix)     ;; a=py
    add a, #1       ;; a=py+pvy(en este caso mas uno)
    ld c, 3(ix)     ;; c=ph
    add a, c        ;; a=py+ph
    sub a, #1       ;; a=py+ph
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    
    ret nz

    ld a, 0(ix)     ;; a=px
    ld b, 2(ix)     ;; b=pw
    srl b
    srl b
    srl b
    add a, b    ;; a=px+pw
    sub a, #1       ;; a=px+pw
    srl a
    ;;srl a 
    srl a           ;; a=(px)/4=tx
    ld b, a         ;; b=(px)/4=tx

    ld a, 1(ix)     ;; a=py
    add a, #1       ;; a=py+pvy(en este caso mas uno)
    ld c, 3(ix)     ;; c=ph
    add a, c        ;; a=py+ph
    sub a, #1       ;; a=py+ph
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    ret 

;;  INPUT:
;;     IX: Pointer to block entities.
can_move_hero_left::

    ;;ld a, 0(ix)
    ;;add a, #1
    ;;sub #4
    ;; ////////////////////////////// 
    ;; tx= px/4
    ;; ty= py/8
    ;; tw= ancho del tilemap
    ;; p= posTilemapinicial+ty*tw+tx
    ld a, 0(ix)     ;; a=px
    sub a, #1       ;; a=px+pvx(en este caso menos 1)
    srl a
    ;;srl a 
    srl a           ;; a=(px+pvx)/4=tx
    ld b, a         ;; b=(px+pvx)/4=tx

    ld a, 1(ix)     ;; a=py
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    
    ret nz

    ld a, 1(ix)     ;; a=py
    ld c, 3(ix)     ;; c=ph
    add a, c        ;; a=py+ph
    sub a, #1       ;; a=py+ph
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0

    ret


;;  INPUT:
;;     IX: Pointer to block entities.
can_move_hero_rigth::

    ;;ld a, 0(ix)
    ;;add a, #1
    ;;sub #79
    ;; ////////////////////////////// 
    ;; tx= px/4
    ;; ty= py/8
    ;; tw= ancho del tilemap
    ;; p= posTilemapinicial+ty*tw+tx
    ld a, 0(ix)     ;; a=px
    add a, #1       ;; a=px+pvx(en este caso mas uno)
    ld b, 2(ix)     ;; b=pw
    srl b
    srl b
    srl b
    add a, b    ;; a=px+pvx+pw
    sub a, #1       ;; a=px+pvx+pw
    srl a
    ;;srl a 
    srl a           ;; a=(px+pvx+pw)/4=tx
    ld b, a         ;; b=(px+pvx+pw)/4=tx

    ld a, 1(ix)     ;; a=py
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0
    
    ret nz
    
    ld a, 1(ix)     ;; a=py
    ld c, 3(ix)     ;; c=ph
    add a, c        ;; a=py+ph
    sub a, #1       ;; a=py+ph
    srl a
    srl a 
    srl a           ;; a=py/8=ty
    ld h, #0
    ld l, a         ;; hl=ty
    ;;ty*20(corresponde a tw)
    add hl, hl      ;; hl=ty*2
    add hl, hl      ;; hl=ty*4
    ld d, h   
    ld e, l         ;; de=ty*4
    add hl, hl      ;; hl=ty*8
    add hl, hl      ;; hl=ty*16
    add hl, de      ;; hl=ty*20

    ld d, #0
    ld e, b         ;; de=tx
    add hl, de      ;; hl=ty*tw+tx
    ld de, (map_ptr) ;; de=posTilemapinicial
    add hl, de      ;; hl=posTilemapinicial+ty*tw+tx

    ld a, (hl)
    sub a, #0

    ret