; ; ; ; MIT License
; ; ; ; 
; ; ; ; Copyright (c) 2021 Ramiro Hernán García Iten / Raúl Ripoll Pérez / Alexandru Marius Cozma
; ; ; ; 
; ; ; ; Permission is hereby granted, free of charge, to any person obtaining a copy
; ; ; ; of this software and associated documentation files (the "Software"), to deal
; ; ; ; in the Software without restriction, including without limitation the rights
; ; ; ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; ; ; ; copies of the Software, and to permit persons to whom the Software is
; ; ; ; furnished to do so, subject to the following conditions:
; ; ; ; 
; ; ; ; The above copyright notice and this permission notice shall be included in all
; ; ; ; copies or substantial portions of the Software.
; ; ; ;
; ; ; ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; ; ; ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; ; ; ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; ; ; ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; ; ; ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; ; ; ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; ; ; ; SOFTWARE.

; ; ; ; ----------------- AUTHORS ------------------
; ; ; ;     Ramiro Hernán García Iten: rhgi1@alu.ua.es
; ; ; ;     Raúl Ripoll Pérez: rrp42@alu.ua.es
; ; ; ;     Alexandru Marius Cozma: amc246@alu.ua.es
; ; ; ; 
; ; ; ; Art by Sara Alfosea - IG: @Slinfir
; ; ; ; ---------------------------------------------

; ; ; ; Third Party source code used
; ; ; ; ----------------------------
; ; ; ; CPCtelera - owned by ronaldo / (Cheesetea, Fremos, ByteRealms) - GNU Lesser General Public License.


;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of CPCtelera: An Amstrad CPC Game Engine 
;;  Copyright (C) 2018 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/>.
;;-------------------------------------------------------------------------------

.include "cpctelera.h.s"

;;; ==========================================
;;; ==========================================
;;; LEVEL MANAGER
;;; ==========================================
;;; ==========================================

man_level_start::
   call  man_interruption_stop
   ;; Se muestra el total de vidas y la puntuación que llevamos
   call  menu_levelStart
   call  man_game_init

   ld    a, (currentLevel)
   cp    #3
   jr    c, darkSong
      ld    de, #_song_dawn
      jp    endSong
 darkSong:
      ld    de, #_song_dark
 endSong:
   call  man_interruption_init

 loop:
      call  man_game_update
      call  man_game_render
      call  man_game_checkEnd  ; Comprueba si hemos muerto
   jr    loop

level_lose::
   ld    c, #(HW_BLACK + 64)
   call  transition_layers

   ld    a, (lives)     ; Restamos una vida
   dec   a
   ld    (lives), a  

   ret

level_win::
   ld    c, #(HW_BLACK + 64)
   call  transition_layers

   ret

generate_platform::
   ;Almacenamos los datos necesarios
   ld a, (platform_x)
   ld b, a
   ld a, (platform_y)
   ld c, a

   ;Esta pendiente la siguiente plataforma? Se tiene que generar ya?
   ld a, (distance)
   cp b              ; Comprobamos la distancia a la que tiene que estar la plataforma y a la que esta
   jr z, generate    ; Si es igual generamos la nueva plataforma
   jr c, _end       ; Si es menor que la distancia acabamos

   ;Preparación de una nueva plataforma
   call xrnd          ;Obtenemos un numero aleatorio
   ;ld b, a              ;Cargamos una copia

   ;Posición Y
   cp #0b01         ;Segun sea par o impar la arriba o abajo
   jp PO, up         ;Par arriba   
   up:
      neg               ;Restamos la distancia del techo
      add c ;(platform_y)
      cp  #75
      jr nc, continue_gen  ;Si entra dentro de la "pantalla"
      ;Si no lo hace "rebota" 
         add #60
   jp continue_gen
   down:
      add c ;(platform_y)  
      jr c, overflow  ;Si ha habido overflow 
      cp #160         ;MAXIMO DE LA PANTALLA - ALTURA PLATAFORMA
      jr c, continue_gen
      ;Se sale del margen
         sub #65         ;255 - 190 = 65 -> Para que entre en los margenes
         jp continue_gen
      overflow:         ;Le restamos el desbordamiento al maximo
         neg
         add #160
   ;Generada la Y en una posición valida la guardo
   ;Here be dragons. Thou art forewarned
   continue_gen:
      ld (new_platform_y), a
      ;Genero la distancia a la que estará
      sub c ;platform_y
      jr c, above
      jr z, same 
      jp below
      above:
         ld c, #base_distance
         sub c
         neg
         ld d, #1
         srl d
         cp    #50
         jr    nc, leibel
            ld    a, #50
            jp    val_ok
         leibel:
         ;Comprobación de valor
         cp #70
         jr c, val_ok
         ld a, #60
      jp val_ok
      same:
         ld a, #50
      jp val_ok
      below:
         ld a, #60
         ;ld c, #base_distance
         ;ld d, #2
         ;srl d
         ;add c
         ;neg
         ;add #70
         ;ld (distance), a
      val_ok:
         ld (distance), a
      ret
   _end:
      ld hl, #platform_x
      dec (hl)
ret

generate:
   ;Ya tenemos la posición de la plataforma generada aleatoriamente y a la distancia designada
   ld ix, #_platform_dummy
   ld a, (new_platform_y)
   ld e_y(ix), a
   ;Guardamos los nuevos datos
   ld hl, #platform_y
   ld (hl), a
   ld a, #75
   ld hl, #platform_x
   ld (hl), a
   ;Asignamos el maximo para que se vuelva a generar un numero aleatorio
   ld a, #250
   ld (distance), a
   ;La generamos
   ld hl, #_platform_dummy
   call  man_entity_create   
ret

generate_enemy::
   ld a, (currentLevel)
   cp #1
   jr nz, t_case_2
      case_1:  ;Solo murcielagos
         call xrnd
         g_murcielago:
         ld ix, #_murcielago
      jp end_ge
   t_case_2:
   cp #2
   jr nz, t_case_3
      case_2:  ;Murcielagos y pajaros
         call xrnd
         jp PO, case_1
         g_buho:
         ld ix, #_buho
      jp end_ge
   t_case_3:
   cp #3
   jr nz, t_case_4
      case_3:  ;Murcielagos y buhos
         call xrnd
         jp PO, g_buho
         g_pajaro:
         ld ix, #_pajaro
      jp end_ge
   t_case_4:
   cp #4
   jr nz, case_5
      case_4:  ;Murcielagos y fantasmas
         call xrnd
         jp PO, case_1
         ld ix, #_fantasma
      jp end_ge
      case_5:  ;TODOS
         call xrnd
         ld b, a
         and #0b11
         cp #0
         jp z, g_murcielago
         cp #1
         jp z, g_buho
         cp #2
         jp z, g_pajaro
         ld ix, #_fantasma
   end_ge:
   call xrnd
   and #0b01111110
   ld e_y(ix), a  ;Cambiar a A del numero generado parseado entre 30 y 134
   ld__d_ixh
   ld__e_ixl
   ex de, hl
   call  man_entity_create  
ret

;-----> Genera un numero aleatorio
xrnd:
   ld hl,#84
   ld a,h
   rra
   ld a,l
   rra
   xor h
   ld h,a
   ld a,l
   rra
   ld a,h
   rra
   xor l
   ld l,a
   xor h
   ld h,a
   ld (xrnd+1),hl
   ld a, h
   and #0b00111111
ret
