;;------------------------------------------------LICENSE NOTICE------------------------------------------------------------------------------
;;  This file is part of Amstrad CPC videogame "Redsea"
;;  Copyright (C) 2021 Crab Party / Elena López (AneleSplats in itch.io) / Iván Tortosa (Pluvs in itch.io, @IvanTortosaBer1 on Twitter) / Antonio Gosálvez (BalgroHub in itch.io)
;;  Copyright (C) 2015 ronaldo / Fremos / Cheesetea / ByteRealms (@FranGallegoBR)
;;
;;  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/>.
;;------------------------------------------------LICENSE NOTICE-------------------------------------------------------------------------
.include "cpctelera.h.s"

;Funciones y variables globales
.globl man_entity_init
.globl man_entity_update
.globl man_entity_create
.globl m_entities

;.globl Spawner
;.globl Inmovil
;.globl Movible
.globl Personaje
.globl Suelo
.globl Suelo2

.globl sys_render_gameinit
.globl sys_physics_update
.globl sys_render_update
.globl sys_ai_update
.globl sys_input_update
.globl sys_animations_update
.globl sys_collision_tilemap
.globl sys_collision_update
;.globl create
.globl cpct_akp_musicPlay_asm
.globl cpct_waitVSYNC_asm
.globl cpct_memcpy_asm
.globl man_entity_freeSpace

.globl cargarNivel
.globl borrarNivel
.globl cargarNivel1
.globl levelUpdate

.globl cpct_akp_SFXPlay_asm
.globl cpct_akp_SFXStop_asm



; Guardamos el tilemap del nivel actual
tilemapActual:: .dw #0x0000


;m_lane_status: .db #0, #0, #0


;;FUNCION PRIVADA
; esperar:
;     .rept 2
;         halt
;         halt
;         call cpct_waitVSYNC_asm
;     .endm
; ret

;Hay que hacer que devuelva la ix de la entidad creada
man_game_create_template::
  
    ;No se crea nada si no hay espacios libres
    call man_entity_freeSpace
    or a
    ret z

   

   push bc
   call man_entity_create
   pop bc
   push ix
   push ix
   pop de
   ld h, b
   ld l, c
   ld bc, #0x12
   call cpct_memcpy_asm

   pop ix
ret


;Se le pasa por ix la posicion del spawn, y crea un enemigo en esa posicion
man_game_create_enemy::

    ;Se crea una entidad con le template enemigo
    ;ld iy, #m_lane_status
    ;ld a, 2(iy)
    ;cp #1
    ;jr z, nocrearlo

    ;ld bc, #Movible
    ;call man_game_create_template



    ;ld a, #1
    ;ld 2(iy), a
    ;Se le asigna la x y la y igual que el spawner
;nocrearlo:
ret

;man_game_enemy_lane_down::

;ret

;;FUNCIONES PUBLICAS
man_game_init::

   call man_entity_init
   call cargarNivel1

   call sys_render_gameinit

  ;  ld bc, #Personaje
  ;  call man_game_create_template

  ;  ld bc, #Suelo2
  ;  call man_game_create_template



   ;ld bc, #Movible
   ;call man_game_create_template

  ;  ld bc, #Inmovil
  ;  call man_game_create_template

  ;  ld bc, #Spawner
  ;  call man_game_create_template

   ;call sys_render_level_start

ret

;Bucle del estado juego
man_game_play::
    ; cpctm_setBorder_asm HW_RED
     ;call cpct_waitVSYNC_asm
         ;call cpct_akp_musicPlay_asm

    ; halt
    ; halt
    ;jr man_game_play

    
    loop:
    

    call levelUpdate
      

    ; cpctm_setBorder_asm HW_YELLOW
      call sys_input_update

    call sys_collision_tilemap
    call sys_ai_update

    ; cpctm_setBorder_asm HW_BLACK
      call sys_collision_update

    ; cpctm_setBorder_asm HW_RED
      call sys_physics_update



    ; cpctm_setBorder_asm HW_MAUVE
      call sys_animations_update

    ; cpctm_setBorder_asm HW_WHITE
      call sys_render_update

    ; cpctm_setBorder_asm HW_ORANGE
      call man_entity_update

      

      ;call esperar
      call cpct_waitVSYNC_asm

       

; ld a,  #2  ;canal para reproducirlo

;  call cpct_akp_SFXStop_asm
    jr loop
ret
