;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of The Last Fungus: An Amstrad CPC Game 
;;  Copyright (C) 2021 Miguel Perez Tarruella / Antonio Jose Fernandez Belliure / 
;;                     David Martinez Garcia / Ocacho Games (@OcachoGames)
;;
;;  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/>.
;;------------------------------------------------------------------------------

;;====================================================================
;; GLOBL INCLUDES
;;====================================================================
.include "level_loader.h.s"
.include "cpctelera.h.s"

;;Globals
.include "global/global.h.s"

;;Manager includes
.include "man/entity.h.s"
.include "man/patrol.h.s"
.include "man/game.h.s"

;;Macros
.include "macros/math.h.s"

;;Resources
.include "resources/templates.h.s"
.include "resources/sprites.h.s"
.include "resources/bullet_animations.h.s"

;;Screens
.include "assets/img/maingamescreen_c.h.s"
.include "assets/img/maingamescreen_1_c.h.s"
.include "assets/img/maingamescreen_2_c.h.s"
.include "assets/img/maingamescreen_3_c.h.s"

;;====================================================================
;; PRIVATE VARIABLES
;;====================================================================
m_current_level::	 .db 0 ;;Current level of our single player (0, 1, 2, 3) if we win at the third level we won the game

;;====================================================================
;; FUNCTIONS
;;====================================================================

;;====================================================================
;; LOADIING FUNCTIONS
;;====================================================================

levels_backgrounds:
	.db 0	
	.dw #_maingamescreen_c_end
	.db 1	
	.dw #_maingamescreen_1_c_end
	.db 2	
	.dw #_maingamescreen_2_c_end
	.db 3	
	.dw #_maingamescreen_3_c_end

man_level_get_current_background::
	ld ix, #levels_backgrounds - 3

	loop_backgrounds:
	INCREMENT_REGISTER_2_BYTES ix, 3

	ld a, (#m_current_level)
	cp (ix)
	jr nz, loop_backgrounds

   ;;The game level matchs
	ld l, 1(ix)
	ld h, 2(ix)	;; HL contains the template for the game_level
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - 
;; Objetive: Set the platforms for the current level
;; Modifies: A, HL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_level_set_level_platforms::
;jp is_not_level_three
    ld a, (#m_current_level)
    IS_EQUALS 0
    jr nz, is_not_level_one

    ld hl, #platform_grass_tmpl
    call man_game_create_template_entity

    ld e_pos_x(iy), #20
    ld e_pos_y(iy), #148

    ld hl, #platform_grass_tmpl
    call man_game_create_template_entity

    ld e_pos_x(iy), #48
    ld e_pos_y(iy), #148

    ;;Change background color
    ld h, #HW_SKY_BLUE
    ld l, #0
    call cpct_setPALColour_asm

    ret

    is_not_level_one:
        ld a, (#m_current_level)
        IS_EQUALS 1
        jr nz, is_not_level_two

        ;;Creating the normal platforms
        ld hl, #platform_grass_rocks_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #32
        ld e_pos_y(iy), #158

        ;;Creating the small platforms
        ld hl, #platform_grass_rocks_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #16
        ld e_pos_y(iy), #141

        ld hl, #platform_grass_rocks_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #56
        ld e_pos_y(iy), #141

        ;;Change background color
        ld h, #HW_SKY_BLUE
        ld l, #0
        call cpct_setPALColour_asm

    ret

    is_not_level_two:
        ld a, (#m_current_level)
        IS_EQUALS 2
        jr nz, is_not_level_three

        ;;Creating the small platforms
        ld hl, #platform_rocks_grass_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #11
        ld e_pos_y(iy), #154

        ld hl, #platform_rocks_grass_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #61
        ld e_pos_y(iy), #154

        ;;Creating the normal platform
        ld hl, #platform_rocks_grass_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #32
        ld e_pos_y(iy), #140

        ;;Change background color
        ld h, #HW_BRIGHT_BLUE
        ld l, #0
        call cpct_setPALColour_asm

    ret

    is_not_level_three:
        ;;Creating the small platforms
        ld hl, #platform_rocks_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #31
        ld e_pos_y(iy), #154

        ld hl, #platform_rocks_small_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #45
        ld e_pos_y(iy), #154

        ;;Creating the normal platform
        ld hl, #platform_rocks_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #11
        ld e_pos_y(iy), #138

        ld hl, #platform_rocks_tmpl
        call man_game_create_template_entity

        ld e_pos_x(iy), #57
        ld e_pos_y(iy), #138

        ;;Change background color
        ld h, #HW_BLACK
        ld l, #0
        call cpct_setPALColour_asm

ret


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;		-
;; Objetive: Select the enemy template dependinig on the level of the game
;; Modifies: HL, A
;; Returns: HL: the template of enemy we should create
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
enemy_levels:
	.db 0	
	.dw #enemy_ghost_easy_tmpl
	.db 1	
	.dw #enemy_walker_easy_tmpl
	.db 2	
	.dw #enemy_ghost_tmpl
	.db 3	
	.dw #enemy_walker_tmpl

man_level_select_enemy_template:
	ld ix, #enemy_levels - 3

	loop_enemies:
	INCREMENT_REGISTER_2_BYTES ix, 3

	ld a, (#m_current_level)
	cp (ix)
	jr nz, loop_enemies

	;;The game level matchs
	ld l, 1(ix)
	ld h, 2(ix)	;; HL contains the template for the game_level 
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;		- IX should contain the enemy memory direction
;; Objetive: Select the patrol of the enemy dependinig on the level of the game
;; Modifies: HL, AF, BC, DE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_level_set_level_enemy_patrol:
	ld a, (#m_current_level)
	cp #0
	jr z, set_patrol_ghost

		;;Enemy type walker or ghost_difficult
		call man_patrol_init_walker
		ret

	;;Enemy type ghost_easy
	set_patrol_ghost:
		call man_patrol_init_ghost
ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Pre requirements
;;  - IX: should contain the memory direction of the entity that
;;        wants to create the bullet (Player/Enemy)
;; Objetive: Select the sprite for the owner's bullet
;; Modifies: ?
;; Returns:
;;		-HL: the sprite for the bullet
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
bullet_sprite_levels:
 	.db 0	
 	.dw #_spr_ge_bullet_0
   .dw #bullet_ge_idle
 	.db 1	
 	.dw #_spr_we_bullet_0
   .dw #bullet_we_idle
 	.db 2	
 	.dw #_spr_gd_bullet_0
   .dw #bullet_gd_idle
 	.db 3	
 	.dw #_spr_wd_bullet_0
   .dw #bullet_wd_idle

man_level_select_bullet_sprite:

 	IS_ENTITY_GIVEN_TYPE_IX e_type_player1
 	jr z, not_player_1_creating_the_bullet 

 	ld hl, #_spr_p1_bullet_0
    ld bc, #bullet_p1_idle
 	ret

 	not_player_1_creating_the_bullet:

 	ld a, (#m_gamemode)
 	IS_EQUALS 1
 	jr nz, mode_is_single_player

 	ld hl, #_spr_p2_bullet_0
   ld bc, #bullet_p2_idle
 	ret

  	mode_is_single_player:

 	push ix

 	ld ix, #bullet_sprite_levels - 5

 	loop_bullet_sprs:

 	INCREMENT_REGISTER_2_BYTES ix, 5

 	ld a, (#m_current_level)
 	cp (ix)
 	jr nz, loop_bullet_sprs

 	;;The game level matchs
 	ld l, 1(ix)
 	ld h, 2(ix)	;; HL contains the template for the game_level

    ld c, 3(ix) ;; DE contains the animation for the bullet
    ld b, 4(ix)

 	pop ix 
ret


