;; 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 "man/level.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"


DefineEntity player , e_type_default,e_render_visible, 5, 174, 4, 18, 0, 0, _sp_hero, e_ai_noAI, 0

.globl current_level

;Pantllas niveles

.globl _song_ingame
.globl cpct_akp_musicInit_asm

lvl_1: .asciz "LEVEL 1"
lvl_2: .asciz "LEVEL 2"
lvl_3: .asciz "LEVEL 3"
lvl_4: .asciz "LEVEL 4"
lvl_5: .asciz "LEVEL 5"
lvl_6: .asciz "LEVEL 6"
lvl_7: .asciz "LEVEL 7"
lvl_8: .asciz "LEVEL 8"
ending_victory:: .asciz "VICTORY!"
press_str2: .asciz "PRESS ENTER"

;Hast aqui

man_level_load:

	;;@TINY
	;;AQUI DEBERIA IR EL BUCLE DEL MENU
	;;EL NUMERO DEL NIVEL ESTA EN current_level
	call pantalla_nivel

	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;,,,;


	call man_entity_init
  
   	call sys_collisions_init
   	call sys_ai_init

   	ld hl, #player
   	call man_entity_create

	ld a, (current_level)

	cp #01
	jr z, load_level1
	cp #02
	jr z, load_level2
	cp #03
	jp z, load_level3
	cp #04
	jp z, load_level4
	cp #05
	jp z, load_level5
	cp #06
	jp z, load_level6
	cp #07
	jp z, load_level7

load_level1:

   	ld hl, #lvl1dmg1
   	call man_entity_create
   	ld hl, #lvl1dmg2
   	call man_entity_create
   	ld hl, #lvl1dmg3
   	call man_entity_create
   	ld hl, #lvl1dmg4
   	call man_entity_create
   	ld hl, #lvl1dmg5
   	call man_entity_create


   	ld hl , #_lvl1_end
   	ld (tilemap_comprimido), hl
   	

   	jp end_loading
load_level2:

   	ld hl, #lvl2plat1
   	call man_entity_create
   	ld hl, #lvl2plat2
   	call man_entity_create
   	ld hl, #lvl2plat3
   	call man_entity_create
   	ld hl, #lvl2plat4
   	call man_entity_create
   	ld hl, #lvl2plat5
   	call man_entity_create
   	ld hl, #lvl2dmg1
   	call man_entity_create
   	ld hl, #lvl2dmg2
   	call man_entity_create
   	ld hl, #lvl2dmg3
   	call man_entity_create
   	ld hl, #lvl2dmg4
   	call man_entity_create
   	ld hl, #lvl2dmg5
   	call man_entity_create
   	ld hl, #lvl2dmg6
   	call man_entity_create

   	ld hl , #_lvl2_end
   	ld (tilemap_comprimido), hl
   	jp end_loading


load_level3:

   	ld hl, #lvl3plat1
   	call man_entity_create
   	ld hl, #lvl3plat2
   	call man_entity_create
   	ld hl, #lvl3plat3
   	call man_entity_create
   	ld hl, #lvl3plat4
   	call man_entity_create
   	ld hl, #lvl3plat5
   	call man_entity_create
   	ld hl, #lvl3plat6
   	call man_entity_create
   	ld hl, #lvl3plat7
   	call man_entity_create
   	ld hl, #lvl3plat8
   	call man_entity_create
   	ld hl, #lvl3plat9
   	call man_entity_create
   	ld hl, #lvl3plat10
   	call man_entity_create

   	ld hl, #lvl3dmg1
   	call man_entity_create
   	ld hl, #lvl3dmg2
   	call man_entity_create
   	ld hl, #lvl3dmg3
   	call man_entity_create
   	ld hl, #lvl3dmg4
   	call man_entity_create
   	ld hl, #lvl3dmg5
   	call man_entity_create
   	ld hl, #lvl3dmg6
   	call man_entity_create
   	ld hl, #lvl3dmg7
   	call man_entity_create
   	ld hl, #lvl3dmg8
   	call man_entity_create

   	ld hl , #_lvl3_end
   	ld (tilemap_comprimido), hl
   	jp end_loading



load_level4:

   	ld hl, #lvl4plat1
   	call man_entity_create
   	ld hl, #lvl4plat2
   	call man_entity_create
   	ld hl, #lvl4plat3
   	call man_entity_create
   	ld hl, #lvl4enem1
   	call man_entity_create
   	ld hl, #lvl4enem2
   	call man_entity_create
   

   	ld hl , #_lvl4_end
   	ld (tilemap_comprimido), hl
   	jp end_loading


load_level5:



   	ld hl, #lvl5plat2
   	call man_entity_create
   	ld hl, #lvl5plat3
   	call man_entity_create
   	ld hl, #lvl5plat4
   	call man_entity_create
   	ld hl, #lvl5plat5
   	call man_entity_create
   	ld hl, #lvl5plat6
   	call man_entity_create
   	ld hl, #lvl5plat7
   	call man_entity_create
   	ld hl, #lvl5enem1
   	call man_entity_create
   	ld hl, #lvl5enem2
   	call man_entity_create
   	ld hl, #lvl5enem3
   	call man_entity_create
   	ld hl, #lvl5enem4
   	call man_entity_create
   	ld hl, #lvl5enem5
   	call man_entity_create


   

   	ld hl , #_lvl5_end
   	ld (tilemap_comprimido), hl
   	jp end_loading


load_level6:

   	ld hl, #lvl6plat1
   	call man_entity_create
   	ld hl, #lvl6plat2
	call man_entity_create
   	ld hl, #lvl6plat3
	call man_entity_create
	ld hl, #lvl6plat4
	call man_entity_create
	ld hl, #lvl6plat5
	call man_entity_create
	ld hl, #lvl6plat6
	call man_entity_create
	ld hl, #lvl6plat7
	call man_entity_create
	ld hl, #lvl6plat8
	call man_entity_create
	ld hl, #lvl6plat9
	call man_entity_create
	ld hl, #lvl6plat10
	call man_entity_create
	ld hl, #lvl6enem1
	call man_entity_create
	ld hl, #lvl6enem2
	call man_entity_create
	ld hl, #lvl6enem3
	call man_entity_create

	ld hl, #lvl6dmg1
	call man_entity_create
   

   	ld hl , #_lvl6_end
   	ld (tilemap_comprimido), hl
   	jp end_loading


load_level7:

   
   	ld hl, #lvl7plat1
   	call man_entity_create
   	ld hl, #lvl7plat2
   	call man_entity_create
   	ld hl, #lvl7plat3
   	call man_entity_create
   	ld hl, #lvl7plat4
   	call man_entity_create
   	ld hl, #lvl7plat5
   	call man_entity_create
   	ld hl, #lvl7plat6
   	call man_entity_create
   	ld hl, #lvl7plat7
   	call man_entity_create
   	ld hl, #lvl7plat8
   	call man_entity_create

   	ld hl, #lvl7enem1
   	call man_entity_create
   	ld hl, #lvl7enem2
   	call man_entity_create
   	ld hl, #lvl7enem3
   	call man_entity_create

   	ld hl, #lvl7dmg1
   	call man_entity_create
   	ld hl, #lvl7dmg2
   	call man_entity_create
   	ld hl, #lvl7dmg3
   	call man_entity_create

   	ld hl , #_lvl7_end
   	ld (tilemap_comprimido), hl
   	jp end_loading






end_loading:
	call sys_render_init


	ret


DefineEntity lvl1dmg1 , e_type_static_dmg, e_render_notVisible, 44, 188, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl1dmg2 , e_type_static_dmg, e_render_notVisible, 88, 188, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl1dmg3 , e_type_static_dmg, e_render_notVisible, 122, 188, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl1dmg4 , e_type_static_dmg, e_render_notVisible, 156, 188, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl1dmg5 , e_type_static_dmg, e_render_notVisible, 182, 188, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0


DefineEntity lvl2plat1 , e_type_static, e_render_notVisible, 32, 168, 16, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2plat2 , e_type_static, e_render_notVisible, 70, 152, 6, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2plat3 , e_type_static, e_render_notVisible, 88, 124, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2plat4 , e_type_static, e_render_notVisible, 122, 156, 16, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2plat5 , e_type_static, e_render_notVisible, 154, 168, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2enem1 , e_type_enemy, e_render_visible, 64, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10
DefineEntity lvl2enem2 , e_type_enemy, e_render_visible 140, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10

DefineEntity lvl2dmg1 , e_type_static_dmg, e_render_notVisible, 20, 188, 40, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2dmg2 , e_type_static_dmg, e_render_notVisible, 82, 188, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2dmg3 , e_type_static_dmg, e_render_notVisible, 102, 188, 28, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2dmg4 , e_type_static_dmg, e_render_notVisible, 128, 152, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2dmg5 , e_type_static_dmg, e_render_notVisible, 162, 164, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl2dmg6 , e_type_static_dmg, e_render_notVisible, 172, 188, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0


DefineEntity lvl3plat1 , e_type_static, e_render_notVisible, 16, 164, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat2 , e_type_static, e_render_notVisible, 28, 132, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat3 , e_type_static, e_render_notVisible, 42, 108, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat4 , e_type_static, e_render_notVisible, 60, 140, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat5 , e_type_static, e_render_notVisible, 90, 164, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat6 , e_type_static, e_render_notVisible, 100, 100, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat7 , e_type_static, e_render_notVisible, 120, 164, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat8 , e_type_static, e_render_notVisible, 148, 164, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat9 , e_type_static, e_render_notVisible, 158, 140, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3plat10 , e_type_static, e_render_notVisible, 170, 116, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0


DefineEntity lvl3dmg1 , e_type_static_dmg, e_render_notVisible, 46, 104, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg2 , e_type_static_dmg, e_render_notVisible, 40, 188, 40, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg3 , e_type_static_dmg, e_render_notVisible, 90, 160, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg4 , e_type_static_dmg, e_render_notVisible, 106, 188, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg5 , e_type_static_dmg, e_render_notVisible, 120, 160, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg6 , e_type_static_dmg, e_render_notVisible, 140, 188, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg7 , e_type_static_dmg, e_render_notVisible, 170, 112, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl3dmg8 , e_type_static_dmg, e_render_notVisible, 166, 188, 24, 4, 0, 0, _sp_hero, e_ai_noAI, 0


DefineEntity lvl4plat1 , e_type_static, e_render_notVisible, 116, 164, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl4plat2 , e_type_static, e_render_notVisible, 128, 136, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl4plat3 , e_type_static, e_render_notVisible, 142, 108, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0

DefineEntity lvl4enem1 , e_type_enemy, e_render_notVisible, 82, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10
DefineEntity lvl4enem2 , e_type_enemy, e_render_notVisible, 170, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10

DefineEntity lvl5plat2 , e_type_static, e_render_notVisible, 62, 180, 8, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl5plat3 , e_type_static, e_render_notVisible, 78, 144, 24, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl5plat4 , e_type_static, e_render_notVisible, 116, 144, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl5plat5 , e_type_static, e_render_notVisible, 136, 124, 10, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl5plat6 , e_type_static, e_render_notVisible, 152, 92, 20, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl5plat7 , e_type_static, e_render_notVisible, 172, 124, 16, 16, 0, 0, _sp_hero, e_ai_noAI, 0

DefineEntity lvl5enem1 , e_type_enemy, e_render_notVisible, 30, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 8
DefineEntity lvl5enem2 , e_type_enemy, e_render_notVisible, 80, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10
DefineEntity lvl5enem3 , e_type_enemy, e_render_notVisible, 80, 126, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 10
DefineEntity lvl5enem4 , e_type_enemy, e_render_notVisible, 118, 126, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 6
DefineEntity lvl5enem5 , e_type_enemy, e_render_notVisible, 172, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 15


DefineEntity lvl6plat1 , e_type_static, e_render_notVisible, 24, 144, 22, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat2 , e_type_static, e_render_notVisible, 24, 144, 2, 48, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat3 , e_type_static, e_render_notVisible, 26, 168, 20, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat4 , e_type_static, e_render_notVisible, 66, 144, 20, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat5 , e_type_static, e_render_notVisible, 78, 180, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat6 , e_type_static, e_render_notVisible, 82, 88, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat7 , e_type_static, e_render_notVisible, 96, 120, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat8 , e_type_static, e_render_notVisible, 108, 180, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat9 , e_type_static, e_render_notVisible, 134, 180, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl6plat10 , e_type_static, e_render_notVisible, 126, 64, 36, 88, 0, 0, _sp_hero, e_ai_noAI, 0

DefineEntity lvl6enem1 , e_type_enemy, e_render_notVisible, 26, 126, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4
DefineEntity lvl6enem2 , e_type_enemy, e_render_notVisible, 136, 46, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4
DefineEntity lvl6enem3 , e_type_enemy, e_render_notVisible, 134, 162, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4

DefineEntity lvl6dmg1 , e_type_static_dmg, e_render_notVisible, 80, 188, 4, 4, 0, 0, _sp_hero, e_ai_noAI, 0

DefineEntity lvl7plat1 , e_type_static, e_render_notVisible, 14, 164, 18, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat2 , e_type_static, e_render_notVisible, 40, 124, 18, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat3 , e_type_static, e_render_notVisible, 64, 164, 18, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat4 , e_type_static, e_render_notVisible, 102, 132, 16, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat5 , e_type_static, e_render_notVisible, 140, 96, 18, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat6 , e_type_static, e_render_notVisible, 156, 96, 2, 64, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat7 , e_type_static, e_render_notVisible, 156, 164, 14, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7plat8 , e_type_static, e_render_notVisible, 188, 132, 2, 60, 0, 0, _sp_hero, e_ai_noAI, 0

DefineEntity lvl7enem1 , e_type_enemy, e_render_notVisible, 46, 106, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4
DefineEntity lvl7enem2 , e_type_enemy, e_render_notVisible, 106, 114, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4
DefineEntity lvl7enem3 , e_type_enemy, e_render_notVisible, 160, 174, 4, 18, 0, 0, _sp_alien, e_ai_stand_by, 4

DefineEntity lvl7dmg1 , e_type_static_dmg, e_render_notVisible, 42, 188, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7dmg2 , e_type_static_dmg, e_render_notVisible, 92, 188, 12, 4, 0, 0, _sp_hero, e_ai_noAI, 0
DefineEntity lvl7dmg3 , e_type_static_dmg, e_render_notVisible, 140, 92, 18, 4, 0, 0, _sp_hero, e_ai_noAI, 0


man_level_update:

	call man_entity_getArray
	ld a, e_x(ix)
	cp #200
	jr nz, continue

	ld a, (current_level)
	inc a
	ld (current_level), a

	call man_level_load

continue:


	ret


pantalla_nivel::

	ld l, #0x20
	call cpct_setVideoMemoryPage_asm

	ld de, #0x8000;(2B DE) array  Pointer to the first byte of the array to be filled up (starting point in memory)
   	ld a, #0x00
   	ld bc, #0x4000
   	call cpct_memset_asm


	ld a, #00
	;ld (scroll), a
	ld l, a
    call cpct_setVideoMemoryOffset_asm

	;; lvl number
	

	call cpct_akp_stop_asm
	ld a, (current_level)

	cp #01
	jr z, nivel_1
	cp #02
	jr z, nivel_2
	cp #03
	jr z, nivel_3
	cp #04
	jr z, nivel_4
	cp #05
	jr z, nivel_5
	cp #06
	jr z, nivel_6
	cp #07
	jr z, nivel_7
	cp #08
	jr z, victory







	

	continue_level::
	
	;call cpct_akp_stop_asm
	
	;; lvl number
    ld hl, #0x000F
    call cpct_setDrawCharM0_asm
    ld de, #0x8000
	ld c, #25
	ld b, #100
    call cpct_getScreenPtr_asm
    call cpct_drawStringM0_asm

	;; enter
	ld hl, #0x0004
    call cpct_setDrawCharM0_asm
    ld iy, #press_str2
    ld de, #0x8000
	ld c, #18
	ld b, #180
    call cpct_getScreenPtr_asm
    call cpct_drawStringM0_asm

    ld hl, #Key_Return
    call esperaTecla

	ld de, #_song_ingame
   	call cpct_akp_musicInit_asm

    cpctm_clearScreen_asm 0


ret

nivel_1::

	ld iy, #lvl_1

	jr continue_level


nivel_2::

	ld iy, #lvl_2

	jr continue_level



nivel_3::

	ld iy, #lvl_3

	jr continue_level

nivel_4::

	ld iy, #lvl_4

	jr continue_level

nivel_5::

	ld iy, #lvl_5

	jr continue_level

nivel_6::

	ld iy, #lvl_6

	jr continue_level

nivel_7::

	ld iy, #lvl_7

	jr continue_level

victory::

	ld iy, #ending_victory

	jr end


end::

	ld hl, #0x000F
    call cpct_setDrawCharM0_asm
    ld de, #0x8000
	ld c, #25
	ld b, #100
    call cpct_getScreenPtr_asm
    call cpct_drawStringM0_asm

	;; enter
	ld hl, #0x0004
    call cpct_setDrawCharM0_asm
    ld iy, #press_str2
    ld de, #0x8000
	ld c, #18
	ld b, #180
    call cpct_getScreenPtr_asm
    call cpct_drawStringM0_asm

    ld hl, #Key_Return
    call esperaTecla

	ld a, #01
	ld (current_level), a

	call man_level_load

    cpctm_clearScreen_asm 0

ret


