;MIT License

;Copyright (c) 2021 Fedora Adventures Pixeltronics

;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.

.include "physics.h.s"
.include "man/entity.h.s"
.include "man/levels.h.s"
.include "cpct_functions.h.s"
.include "screens/menu_z.h.s"
.include "screens/controls_z.h.s"
.include "screens/dead_z.h.s"
.include "screens/win_z.h.s"
.include "screens/level_complete_z.h.s"
.include "screens/credits_z.h.s"



sys_game_update:
    call man_entity_put_treasure_in_iy
    call sys_game_check_treasure
    call man_entity_put_door_in_iy
    call sys_game_check_door
ret

sys_game_check_treasure:
    ld a, #(e_cmps_alive)               ;cargo el vector de componentes en a
    and e_cmps(iy)                      ;compruebo si esta vivo
    ;si no esta viva dejo de comprobar
    jr z, fin_sgct
    ;si esta viva tengo que verificar si el pj esta chocando contra el diamante
    call sys_physics_check_collision
    jr c, fin_sgct                     ;si no choca, salgo de la funcion
    ;si esta chocando hago tres cosas:
    ;cojo el diamante
    call sys_game_catch_treasure
    ld de, #entity_size
    add iy, de               ;pongo el puntero iy en la direccion de la puerta
    ;abro la puerta
    call sys_game_open_door
fin_sgct:
ret

sys_game_catch_treasure:
    ld a, e_cmps(iy)                    ;cargo el vector de componentes
    res 7, a
    res 6, a
    set 3, a
    ld e_cmps(iy), a
ret

sys_game_open_door:
    ld a, e_cmps(iy)                    ;cargo el vector de componentes
    set 7, a
    set 4, a
    ld e_cmps(iy), a
ret

sys_game_check_door:
    ld a, #(e_cmps_alive)
    and e_cmps(iy)                    ;cargo el vector de componentes en a                              
    ;si no esta viva dejo de comprobar
    jr z, fin_sgcd
    ;si esta viva tengo que verificar si el pj esta chocando contra la puerta
    call sys_physics_check_door_collision
    jr c, fin_sgcd                     ;si no choca, salgo de la funcion
    ;si esta chocando salgo del nivel
    call sys_game_exit_level
fin_sgcd:
ret

sys_game_exit_level:
    call man_levels_get_level
    cp a, #7
    jr nz, sgel_next
    call you_win
    call man_entity_reset
    call man_levels_load_level_1
    jp mainMenu
sgel_next:    
    call level_complete
    call man_levels_load_next_level
sgel_fin:    
ret

sys_game_die:
    call you_died
    call man_levels_reload_level
ret


mainMenu:
    call cpct_akp_stop_asm
    ld hl, #_menu_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm

    ld de, #_song_menu
    call cpct_akp_musicInit_asm

    wait_space:

        call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm


        call cpct_scanKeyboard_f_asm
        ld hl, #Key_1
        call cpct_isKeyPressed_asm
        jr nz, controls

        call cpct_scanKeyboard_f_asm
        ld hl, #Key_2
        call cpct_isKeyPressed_asm
        jr nz, credits

        call cpct_scanKeyboard_f_asm
        ld hl, #Key_Space
        call cpct_isKeyPressed_asm

        jr z, wait_space

    cpctm_clearScreen_asm 0xC000
    call cpct_akp_stop_asm

    call man_entity_reset    
    call man_levels_load_level_1

ret

controls:

    cpctm_clearScreen_asm 0

    ld hl, #_controls_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
    
    wait_b1:

        call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm
        call cpct_scanKeyboard_f_asm
        ld hl, #Key_B
        call cpct_isKeyPressed_asm

        jr z, wait_b1

    cpctm_clearScreen_asm 0xC000
    jp mainMenu

credits:
    cpctm_clearScreen_asm 0

    ld hl, #_credits_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
    
    wait_b2:

        call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm
        call cpct_scanKeyboard_f_asm
        ld hl, #Key_B
        call cpct_isKeyPressed_asm

        jr z, wait_b2

    cpctm_clearScreen_asm 0xC000
    jp mainMenu

you_died:
    cpctm_clearScreen_asm 0

    ld hl, #_dead_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
    
    wait_key:

         call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm

        call cpct_scanKeyboard_f_asm
        ld hl, #Key_R
        call cpct_isKeyPressed_asm
        ret nz

        call cpct_scanKeyboard_f_asm
        ld hl, #Key_B
        call cpct_isKeyPressed_asm
        
        jr z, wait_key

    cpctm_clearScreen_asm 0xC000
    jp mainMenu

you_win:
    cpctm_clearScreen_asm 0

    ld hl, #_win_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
    
    wait_win:

        call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm

        call cpct_scanKeyboard_f_asm
        ld hl, #Key_Space
        call cpct_isKeyPressed_asm
        
        jr z, wait_win

    cpctm_clearScreen_asm 0xC000
    
    ret



level_complete:
    cpctm_clearScreen_asm 0

    ld hl, #_level_complete_z_end
    ld de, #0xFFFF
    call cpct_zx7b_decrunch_s_asm
    
    wait_space_c:
    
         call cpct_waitVSYNC_asm
        call cpct_akp_musicPlay_asm
        
        call cpct_scanKeyboard_f_asm
        ld hl, #Key_Space
        call cpct_isKeyPressed_asm
        
        jr z, wait_space_c

    ret
