;; This file is part of Moonsters
;;  Copyright (C) 2021 Dam Studios
;;
;;     Moonsters is free software: you can redistribute it and/or modify
;;     it under the terms of the GNU General Public License as published by
;;     the Free Software Foundation, either version 3 of the License, or
;;     (at your option) any later version.
;; 
;;     SHOOT TO MOVE 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 General Public License for more details.
;; 
;;     You should have received a copy of the GNU General Public License
;;     along with Moonsters  If not, see <https://www.gnu.org/licenses/>.

.include "cpctelera.h.s"
.include "man/entity_manager.h.s"
.include "sys/render.h.s"
.include "sys/physics.h.s"
.include "sys/input.h.s"
.include "sys/collisions.h.s"
.include "sys/menu.h.s"
.include "sys/sys_ai.h.s"
.include "assets/assets.h.s"
.include "man/level.h.s"

.area _DATA
.area _CODE

.globl cpct_disableFirmware_asm
.globl cpct_waitVSYNC_asm
.globl cpct_setStackLocation_asm
.globl _song_ingame
.globl cpct_akp_musicInit_asm
.globl cpct_akp_musicPlay_asm

current_level::
   .db 0x01

load_level::
   .db 0x00

;;Initialize music
;ld de, #_song_ingame
;call cpct_akp_musicInit_asm
varmusic:: .db #01

_main::
   ;;cambiamos posicion pila
   ld hl, #0x8000;;HL POSICION NUEVA
   call cpct_setStackLocation_asm

   call cpct_disableFirmware_asm

   push hl
    cpctm_setBorder_asm HW_BLACK
    pop hl

   call sys_menu_start
   call man_level_load

   ld de, #_song_ingame
   call cpct_akp_musicInit_asm
   ;varmusic:: .db #1


loop:

               saveSetBorder HW_RED
   ;saveSetBorder HW_RED
   call sys_render_update


   saveSetBorder HW_WHITE
   call sys_ai_update
   call sys_input_update
   
   saveSetBorder HW_BLUE
   call sys_physics_update
   saveSetBorder HW_BLACK
   call sys_collisions_update
      


   ;saveSetBorder HW_WHITE
   saveSetBorder HW_GREEN
   call man_entity_update
   ;call sys_collisions_update

   call man_level_update

   call cpct_waitVSYNC_asm
   ;ld a, (varmusic)
   ;xor #1
   ;ld (varmusic), a
   ;call z, cpct_akp_musicPlay_asm
   call cpct_akp_musicPlay_asm
    ;  ld a, #2
   ;_cont:
    ;  ld (varmusic), a
   
   

   ld a, (load_level) 
   cp #01
   jr z, load_new_level

   jr   loop


load_new_level:

   ld a, #0
   ld (load_level), a
   call man_level_load

   jr loop
