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


;;
;; GAME MANAGER
;;

.include "../sys/render.h.s"
.include "input.h.s"
.include "player.h.s"
.include "../sys/music.h.s"

.globl _main

;; 0 -> Player on lobby 
;; 1 -> Player on world
onLobby: .db 0

;; 0 -> Player select world 1
;; 1 -> Player select world 2
;; 2 -> Player select world 3
selectedWorld: .db 0

;; World Level { 0 1 2 3 4 5 6 7 8 9}
worldLevel: .db 0

;; Game Level Manager
game_manager_level_update::

    ld a, (onLobby)
    sub a, #0
    jp z, startLevel

    ;; Player playing a map
    ;; Render next level of the map
    
    ld a, (worldLevel)
    ld c, a
    sub a, #9
    jp z, completed_level

    ld a, c
    inc a
    ld (worldLevel), a
    
    ;; Check if last level
    ld a, (worldLevel)
    sub a, #9

    call render_levelup
    
    jp z, last_level

        call play_door
        call game_stop
        call load_screen
        call game_stop
        call game_stop
        call game_stop
        call render_level

        ;; Reset hero position
        ld a, (selectedWorld)
        call entityman_player_spawn_world

        ret

    last_level:

        call play_door
        call game_stop
        call load_lvl10_screen
        call game_stop
        call game_stop
        call game_stop
        call game_stop
        call render_level

        ;; Reset hero position
        call entityman_player_spawn_world_completed
        ret

    ;; Last level of a world completed
    completed_level:
    
        ;; Update variables
        ld (worldLevel), a
        ld (onLobby), a
        ld a, (selectedWorld)
        inc a
        ld (selectedWorld), a

        ;; Set and render new lobby
        call render_set_lobby
        ld a, (selectedWorld)
        call render_lobby

        ;; Reset hero position
        call entityman_player_spawn_lobby
        ret

    ;; Player on lobby
    startLevel:

        ;; Check if last lobby (game completed)
        ld a, (selectedWorld)
        cp a, #3
        jp z, endgame

        ;; Updating onLobby flag
        ld a, #1
        ld (onLobby), a

        ;; Rendering selected world
        ld a, (selectedWorld)
        call render_set_world
        call play_door
        call game_stop
        ld a, (selectedWorld)
        call load_start_screen
        call game_stop
        call game_stop
        call game_stop
        call game_stop
        call render_level
        
        ;; Reset hero position
        ld a, (selectedWorld)
        call entityman_player_spawn_world

    ret


;; Set and render level selector
game_manager_init_lobby::

    ;; Set and render lobby 0
    xor a
    call render_set_lobby
    call render_lobby
    
    ;; Config  player
    call init_player
    
    ;; Spawn lobby
    call entityman_player_spawn_lobby

    ret

;; Create and config player to default values
init_player::

    ;; Set jumps
    ld a, #1
    ld  (_jumps), a

    ;; Create player
    ld hl, #hero
    call entityman_create_hero

    ret


resetgame:

    xor a
    ld (onLobby), a
    ld (selectedWorld), a
    ld (worldLevel), a

    jp _main

game_manager_death::

    push ix
    call play_dead
    pop ix
    call renderSys_remove_hero
    call render_death
    call game_stop
    call renderSys_remove_hero

    ld a, (selectedWorld)
    call entityman_player_spawn_world

    ret

game_stop:

    ld a, #130
    
    loop:
        dec a
        halt
        jp nz, loop

    ret

endgame:

    call game_stop
    call game_stop
    call game_stop
    call renderSys_remove_hero
    call game_stop
    call game_stop
    call game_stop
    call render_end_screen
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop
    call game_stop

    jp resetgame