;;-----------------------------LICENSE NOTICE------------------------------------
;;  This file is part of Viking Hangover
;;  Copyright (C) 2021 Ahmed Begga / Érika Moreno / Adriana Morejón (@ComputosGame)
;;
;;  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"
.include "game.h.s" 
.include "entity.h.s"
.include "sys/render.h.s"
.include "sys/prerender.h.s"
.include "sys/colisions.h.s"
.include "sys/input.h.s"
.include "sys/physics.h.s"
.include "sys/ai.h.s"
.include "sys/generator.h.s"
.include "sys/animations.h.s"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                         Variables                     ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
playerScore:
   .db #5
playerLife:
   .db #3
Invulnerability4player:
   .db #Invulnerability
subInvulnerability4player:
   .db #0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Init the game                                    ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_game_init::
   call man_entity_init
   call sys_render_init 
   call sys_generator_init_game
   call sys_pre_render_update
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Core                                             ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_game_play::
   _loop:
      call man_entity_destroy_dead
      call man_game_update
      call sys_generator_update
      call sys_input_update
      call sys_ai_update
      call sys_animations_update
      call sys_colisions_update
      call sys_physics_update

      call cpct_akp_musicPlay_asm

   _control_pause_menu:
      call cpct_scanKeyboard_f_asm
      ld hl, #Key_Esc
      call cpct_isKeyPressed_asm
      jr z, _wait_for_key
      call man_menu_pause
      or a
      jr z, _continue_pause
      call _reset
   _continue_pause:
      call man_continue_playing
   _wait_for_key:
      .rept 2
         halt
         halt
         call cpct_waitVSYNC_asm
      .endm
      
      call sys_render_update 
      call sys_pre_render_update
      jp _loop
ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Our full game is here                            ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
man_game_update:
   ld a, (#Invulnerability4player)
   or a
   jp z, _out
   
   call inv_viking
   jp z, render_viking 
   jp _out
render_viking:
   ld a, #InvulnerabilityTime
   ld (#subInvulnerability4player), a 

   ld a, #1
   ld (#change_renderable), a
_out: 
   ret
inv_viking:
   dec a
   ld (#Invulnerability4player), a 
   ld a, (#subInvulnerability4player)
   dec a
   ld (#subInvulnerability4player), a 
   ret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;      Decreases the life of the player every time that ;;
;;      the player gets a hit from an enemy              ;;
;;Required:                                              ;;
;;      IX -> Pointer to the player                      ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;TODO AHMED
man_game_player_colision_shot::
   ld a, (#Invulnerability4player)
   or a
   ret nz
   call quita_vida

   push ix
   ld ix, #initVida

   ld b, #1                   
   cp b
   jp z, vida1               
   
   or a
   jp z, vida0

   ld b, #2                   
   cp b
   jp z, vida2
   
   ld b, #3                   
   cp b
   jp z, vida3

vida0:
   ld hl, #_spr_vida_0
   jp _next
vida1:
   ld hl, #_spr_vida_1
   jp _next
vida2:
   ld hl, #_spr_vida_2
   jp _next
vida3:
   ld hl, #_spr_vida_3
_next:
   ld e_sprite1(ix), h 
   ld e_sprite2(ix), l
   call sys_render_draw_once
   pop ix
   call actualiza_viking
   ret nz
   call man_entity_set4_destroy
   call man_menu_lose
   ;call _reset                            ;; Reseteamos el juego
ret

actualiza_viking:
   ld a, #Invulnerability
   ld (Invulnerability4player),  a
   ld a, #InvulnerabilityTime
   ld (subInvulnerability4player),  a
   ld a, (#playerLife)
   or a
   ret
quita_vida:
   ld a, (#playerLife)
   dec a
   ld (playerLife), a 
   ret
quita_punt:
   ld a, (#playerScore)
   dec a
   ld (playerScore), a 
   ret
man_game_player_colision_bottle:
   call quita_punt
   push ix
   ld ix, #initBottle

   ld b, #1                   
   cp b
   jp z, bottle1              

   or a
   jp z, bottle0

   ld b, #2                   
   cp b
   jp z, bottle2

   ld b, #3                   
   cp b
   jp z, bottle3

   ld b, #4                   
   cp b
   jp z, bottle4
   
   ld b, #5                  
   cp b
   jp z, bottle5

bottle0:
   ld hl, #_spr_botella_winner_5
   jp _nexts
bottle1:
   ld hl, #_spr_botella_winner_4
   jp _nexts
bottle2:
   ld hl, #_spr_botella_winner_3
   jp _nexts
bottle3:
   ld hl, #_spr_botella_winner_2
   jp _nexts
bottle4:
   ld hl, #_spr_botella_winner_1
   jp _nexts
bottle5:
   ld hl, #_spr_botella_winner_0

_nexts:
   
   ld e_sprite1(ix), h 
   ld e_sprite2(ix), l

   call sys_render_draw_once
   pop ix
   ld a, (#playerScore)
   or a
   ret nz
   call man_entity_set4_destroy

   call man_menu_win
   ;call _reset                   ;; Reseteamos el juego
   ret


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Description:                                           ;;                                                          
;;     Clear the screen after pause                      ;;
;;Required:                                              ;;
;;     -                                                 ;;  
;; Return:                                               ;;                                                  
;;     -                                                 ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

man_continue_playing::
   call cpct_cleanScreen_asm
   ret
