;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
;;  Copyright (C) 2021 Daniel José García Evans, Iván Alejandro Ontano Herrera,
;;  Sergio Izquierdo Pintado (PyroClastic Software)
;;
;;  Special Thanks to Ennio Fernando Velásquez Berlingeri (PyroBombastic)
;;
;;  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/>.

;;
;;  GAME MANAGER
;;

.include "globals.h.s"

man_game_init::
    ;; Disable firmware to prevent it from interfering with string drawing
    call cpct_disableFirmware_asm

    call set_int_handler
    call play_song

    call man_entity_reset_variables ;;para reiniciar las variables de juego
    call man_entity_init            ;;para reiniciar todas las entidades
    call sys_render_palette
    call man_screen_init            ;;para mostrar las pantallas de inicio
    call sys_render_init            ;;para cargar el mapa y dibujar TODAS las entidades
    call man_ui_init                ;;para dibujar la interfaz
    call man_ui_update_score

    ret 

man_game_reset::
    call man_entity_reset_variables ;;para reiniciar las variables de juego
    call man_entity_init            ;;para reiniciar todas las entidades
    call sys_render_init            ;;para cargar el mapa y dibujar TODAS las entidades
    call man_ui_init                ;;para dibujar la interfaz
    call man_ui_update_score

    ret 

man_game_next_map::
    call man_entity_init            ;;para reiniciar todas las entidades
    call sys_render_init            ;;para cargar el mapa y dibujar TODAS las entidades
    call man_ui_init                ;;para dibujar la interfaz
    call man_ui_update_score
    
    ret

man_game_update::
    ;;cpctm_setBorder_asm HW_BRIGHT_RED           ;;color de borde de render
    call sys_render_update

    ;;cpctm_setBorder_asm HW_ORANGE               ;;color de borde de input de protagonista
    call sys_input_update

    ;;cpctm_setBorder_asm HW_BRIGHT_YELLOW        ;;color de borde de IA
    call sys_ia_update

    ;;cpctm_setBorder_asm HW_BRIGHT_GREEN         ;;color de borde de fisicas
    call sys_physics_update

    ;;cpctm_setBorder_asm HW_BRIGHT_BLUE          ;;color de borde de colisiones
    call sys_collision_update

    ;;cpctm_setBorder_asm HW_MAGENTA              ;;color de borde de entidades
    call man_entity_update

    ;;cpctm_setBorder_asm HW_WHITE                ;;color de borde libre
    call cpct_waitVSYNC_asm

    ret 

int_counter: .db 06
int_handler::
    push af
    push bc
    push de
    push hl

    ld a, (int_counter)
    dec a
    jr nz, _cont
_zero:
    call cpct_akp_musicPlay_asm
    call cpct_scanKeyboard_i_asm
    ld a, #6
_cont:
    ld (int_counter), a
    ld h, a
    ld l, #16
    
    pop hl
    pop de
    pop bc
    pop af
    ei
    reti 

set_int_handler::
    ld hl, #0x38
    ld (hl), #0xc3
    inc hl
    ld (hl), #<int_handler
    inc hl
    ld (hl), #>int_handler
    inc hl
    ld (hl), #0xc9

    ret 

play_song::
    ld de, #_song_ptr
    call cpct_akp_musicInit_asm
    ret 