.include "./system/cpct_globals.h.s"
.include "cpctelera.h.s"
.include "entities.h.s"
.include "game.h.s"
.include "./screens/screen_menu_z.h.s"
.include "rounds.h.s"
.include "./system/ui.h.s"
.include "./screens/credits_menu_z.h.s"
.include "music.h.s"
;;;;;;;;;;
.globl _myDrawStringM0
.globl _myDrawChar_inner
.globl _sprite_arma_0
.globl _sprite_arma_1
.globl _sprite_arma_2
;;;;;;;;
;; HISTORIA
story1Str: .asciz "WHAT A TOUGH DAY"
story2Str: .asciz "IM CLOSING THE STORE"  ;;ancho max -> 'IM CLOSING THE STORE'
story3Str: .asciz "WAIT, WHAT ARE..."
story4Str: .asciz "...THOSE THINGS?"
story5Str: .asciz "HEY, YOU!"
story6Str: .asciz "GET OUT OF MY STORE!"
enterStr: .asciz "[SPACE] TO CONTINUE"
.globl title_screen

;; PRIMERA PANTALLA
startStr: .asciz "START"
lvlStr1_start: .asciz "NIGHT 1"
continueStr: .asciz "[SPACE] TO CONTINUE"

;; CUANDO TE PASAS UNA PANTALLA
lvlStr1: .asciz "NIGHT"
lvlStr2: .asciz "COMPLETED"
nexNStri: .asciz "NEXT NIGHT:"

;; PANTALLA DE MUERTE
stringD1:  .asciz "YOU DIED!"
stringD2:  .asciz  "[SPACE] TO RESTART"
yourScString: .asciz "YOUR SCORE:"

;; NOMBRES DE LOS NIVELES
lvl1Str:  .asciz "FOOD EATERS"
lvl2Str:  .asciz "   DOUBLE TROUBLE"
lvl3Str:  .asciz " GHOSTS N' BURGERS"
lvl4Str:  .asciz "   LOOK AROUND"
lvl5Str:  .asciz "   DOCTOR DOCTOR"
lvl6Str:  .asciz "   GUN UPGRADE!"
lvl7Str:  .asciz "   FLYING DANGER"
lvl8Str:  .asciz "   SPOOKY SEASON"
lvl9Str:  .asciz "    PUMPKINVANIA"
lvl10Str: .asciz "    CPCRETROBOO"
lvl11Str: .asciz "       RUN!"
lvl12Str: .asciz "GET OUT OF MY MINE!";;;;;;;;;;;;;
lvl13Str: .asciz "  UNDER PRESSURE"
lvl14Str: .asciz "  BULLET UPGRADE!"
lvl15Str: .asciz "  BATS BATS BATS!"
lvl16Str: .asciz "   HELL O WEEN"
lvl17Str: .asciz " BOUNCING BEAUTIES"
lvl18Str: .asciz "  SPOOKY SKELETONS"
lvl19Str: .asciz " THE LAST STRETCH"
lvl20Str: .asciz "     THE END!"

;; PANTALLA FINAL DEL JUEGO
endStr: .asciz "CONGRATULATIONS!"
youSStri: .asciz "YOU SURVIVED"
allNStri: .asciz "ALL NIGHTS!"
theEndStr: .asciz "THE END"
enterToTStri: .asciz "[ENTER].TITLE SCREEN"

screen_man_first_lvl_screen::
   cpctm_setBorder_asm HW_BLACK
   cpctm_clearScreen_asm #0x00

   ld hl, #0x0006
   call cpct_setDrawCharM0_asm   ;; Set draw char colours

   ;cpctm_screenPtr_asm hl, #0xC000, #27, #20
   ld hl, #0xE0BB
   ld   iy, #startStr   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ld hl, #0x0006
   call cpct_setDrawCharM0_asm   ;; Set draw char colours

   cpctm_screenPtr_asm hl, #0xC000, #25, #50
   ld   iy, #lvlStr1_start   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ld hl, #0x0004
   call cpct_setDrawCharM0_asm   ;; Set draw char colours

   cpctm_screenPtr_asm hl, #0xC000, #15, #70
   ld   iy, #lvl1Str   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   cpctm_screenPtr_asm de, #0xC000, #20, #100
   ld hl, #_sprite_arma_0
   ld c, #36
   ld b, #56
   call cpct_drawSprite_asm

   ld hl, #0x000F
   call cpct_setDrawCharM0_asm   ;; Set draw char colours
   cpctm_screenPtr_asm hl, #0xC000, #03, #190

   ld   iy, #continueStr   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ld hl, #Key_Space
   call waitKeyPressed

   ld l, #2 ; que sonido va a sonar
   ld h, #15 ; volumen 0-15
   ld e, #80 ; nota 0-143
   ld d, #0 ; speed (0 es original, 1 es la mas rapida)
   ld bc, #0xFFFF ;pitch
   ld a, #10 ;canal del medio
   call cpct_akp_SFXPlay_asm
ret

screen_man_story_screen::
   cpctm_clearScreen_asm #0x00
   ld hl, #0x0004
   call cpct_setDrawCharM0_asm
   ;; ENTER TO CONTINUE
   ld hl,#0x0004
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #02, #170
   ld iy, #enterStr
   call _myDrawStringM0

   ld hl,#0x000F
   call cpct_setDrawCharM0_asm

   cpctm_screenPtr_asm hl, #0xC000, #05, #40
   ld iy, #story1Str
   call _myDrawStringM0
   ;; Mostrar la siguiente linea
   ld hl, #Key_Space
   call waitKeyPressed_story

   cpctm_screenPtr_asm hl,#0xC000,#00,#60
   ld iy, #story2Str
   call _myDrawStringM0
   ld hl, #Key_Space
   call waitKeyPressed_story

   cpctm_screenPtr_asm hl,#0xC000,#10,#80
   ld iy, #story3Str
   call _myDrawStringM0
   ld hl, #Key_Space
   call waitKeyPressed_story

   cpctm_screenPtr_asm hl,#0xC000,#10,#100
   ld iy, #story4Str
   call _myDrawStringM0
   ld hl, #Key_Space
   call waitKeyPressed_story

   cpctm_screenPtr_asm hl,#0xC000,#20,#120
   ld iy, #story5Str
   call _myDrawStringM0
   ld hl, #Key_Space
   call waitKeyPressed_story

   cpctm_clearScreen_asm #0xC000
   
   cpctm_screenPtr_asm hl,#0xC000,#00,#50
   ld iy, #story6Str
   call _myDrawStringM0

   ld hl,#0x0004
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #02, #170
   ld iy, #enterStr
   call _myDrawStringM0
   ld hl, #Key_Space
   call waitKeyPressed_story
ret

screen_man_lvl_screen::
   ld de, #_main_song;;;;;;;;;;
   call play_song;

   cpctm_setBorder_asm HW_BLACK
   cpctm_clearScreen_asm #0x00

   ld hl, #0x0006
   call cpct_setDrawCharM0_asm   ;; Set draw char colours

   ;; NIGHT

   ;cpctm_screenPtr_asm hl, #0xC000, #05, #40
   ld hl, #0xC195
   ld   iy, #lvlStr1   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ;; Numero de ronda
   call screen_man_get_current_round
   
   ;;cpctm_screenPtr_asm hl, #0xC000, #40, #40
   ;; COMPLETED
   ld hl, #0xC1B8
   ld   iy, #lvlStr2   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ;; Aviso de siguiente noche
   ld hl, #0x0006
   call cpct_setDrawCharM0_asm
   ;cpctm_screenPtr_asm hl, #0xC000, #100, #60
   ld hl, #0xE294
   ld   iy, #nexNStri   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ;; Nombre del nivel
   ld hl, #0x0004
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #00, #90
   ld hl, #0xD370
   call get_round
   add #1

   cp #2
   jr z, show_str_2
   cp #3
   jr z, show_str_3
   cp #4
   jr z, show_str_4
   cp #5
   jr z, show_str_5
   cp #6
   jr z, show_str_6
   cp #7
   jr z, show_str_7
   cp #8
   jr z, show_str_8
   cp #9
   jr z, show_str_9
   cp #10
   jr z, show_str_10
   cp #11
   jr z, show_str_11
   cp #12
   jr z, show_str_12
   cp #13
   jr z, show_str_13
   cp #14
   jr z, show_str_14
   cp #15
   jp z, show_str_15
   cp #16
   jp z, show_str_16
   cp #17
   jp z, show_str_17
   cp #18
   jp z, show_str_18
   cp #19
   jp z, show_str_19
   cp #20
   jp z, show_str_20

   show_str_2:
      ld   iy, #lvl2Str   ;; IY = Pointer to the string 
   jp skip_lvl_specific

   show_str_3:
      ld   iy, #lvl3Str   ;; IY = Pointer to the string 
   jp skip_lvl_specific

   show_str_4:
      ld   iy, #lvl4Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_5:
      ld   iy, #lvl5Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_6:
      push hl
      ld   iy, #lvl6Str   ;; IY = Pointer to the string 
      cpctm_screenPtr_asm de, #0xC000, #20, #110
      ld hl, #_sprite_arma_1
      ld c, #36
      ld b, #56
      call cpct_drawSprite_asm
      pop hl
   jr skip_lvl_specific

   show_str_7:
      ld   iy, #lvl7Str   ;; IY = Pointer to the string
   jr skip_lvl_specific

   show_str_8:
      ld   iy, #lvl8Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_9:
      ld   iy, #lvl9Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_10:
      ld   iy, #lvl10Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_11:
      ld   iy, #lvl11Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_12:
      ld   iy, #lvl12Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_13:
      ld   iy, #lvl13Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific
   
   show_str_14:
      push hl
      ld   iy, #lvl14Str   ;; IY = Pointer to the string
      cpctm_screenPtr_asm de, #0xC000, #20, #110
      ld hl, #_sprite_arma_2
      ld c, #36
      ld b, #56
      call cpct_drawSprite_asm
      pop hl
   jr skip_lvl_specific

   show_str_15:
      ld   iy, #lvl15Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_16:
      ld   iy, #lvl16Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_17:
      ld   iy, #lvl17Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_18:
      ld   iy, #lvl18Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific
   
   show_str_19:
      ld   iy, #lvl19Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   show_str_20:
      ld   iy, #lvl20Str   ;; IY = Pointer to the string 
   jr skip_lvl_specific

   skip_lvl_specific:
   call _myDrawStringM0  ;; Draw the string

   ld hl, #0x000F
   call cpct_setDrawCharM0_asm   ;; Set draw char colours

   cpctm_screenPtr_asm hl, #0xC000, #03, #190
   ld   iy, #continueStr   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string

   ld hl, #Key_Space
   call waitKeyPressed
ret

screen_man_title_screen::
   cpctm_setBorder_asm HW_BLACK
   ld hl, #_screen_menu_z_end
   ld de, #0xFFFF
   call cpct_zx7b_decrunch_s_asm

   loop_title:
      ;; Comprobacion game
      ld hl, #Key_1
      call cpct_isKeyPressed_asm
      jr nz, screen_man_credits_screen

      ;; Comprobacion credits
      ld hl, #Key_Return
      call cpct_isKeyPressed_asm
      jr z, loop_title
ret

screen_man_credits_screen::
   cpctm_setBorder_asm HW_BLACK

   ld hl, #_credits_menu_z_end
   ld de, #0xFFFF
   call cpct_zx7b_decrunch_s_asm

   credits_loop:
   ld hl, #Key_Return
   call cpct_isKeyPressed_asm
   jr z, credits_loop
   jr screen_man_title_screen
ret

screen_man_end_screen::
   cpctm_clearScreen_asm #0x00

   ld hl, #man_entity_set4destruction_simple
   call man_entity_forall
   call man_entity_update
   call man_entity_update
   call player_restart

   ld de, #_main_song
   call play_song

   ld hl, #0x000E
   call cpct_setDrawCharM0_asm

   cpctm_screenPtr_asm hl, #0xC000, #6, #30

   ld iy, #endStr
   call _myDrawStringM0

   ld hl, #0x0006
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #15, #50
   ld iy, #youSStri
   call _myDrawStringM0

   cpctm_screenPtr_asm hl, #0xC000, #15, #70
   ld iy, #allNStri
   call _myDrawStringM0

   ld hl, #0x0004
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #22, #100
   ld iy, #theEndStr
   call _myDrawStringM0

   ld hl, #0x000F
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #07, #140
   ld iy, #yourScString
   push hl
   call _myDrawStringM0
   pop hl
   push hl
   call sys_ui_update_score_millar_ds
   pop hl
   push hl
   call sys_ui_update_score_centenas_ds
   pop hl
   push hl
   call sys_ui_update_score_decenas_ds
   pop hl
   push hl
   call sys_ui_update_score_unidades_ds
   pop hl

   ld hl, #0x000E
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #00, #180
   ld iy, #enterToTStri
   call _myDrawStringM0

   end_loop:
   ld hl, #Key_Return
   call cpct_isKeyPressed_asm
   jr z, end_loop
   pop hl ;;Este pop es porque nunca se llega al ret
   jp title_screen
ret

screen_man_get_current_round:

   ld hl, #0x0006
   call cpct_setDrawCharM0_asm   ;; Set draw char colours
   ld hl, #0xC1AE
   call get_round  ;; A = rondas
   cp #10
   jp m, less_than_ten_screen
   ;cpctm_screenPtr_asm hl, #0xC000, #32, #40
   ld hl, #0xC1B0
   call sys_ui_rounds_unidades
   and #0x0F
   add #48
   ld e, a
   call _myDrawChar_inner
   ld hl, #0xC1AC
   call sys_ui_rounds_decenas
   and #0x0F
   add #48 
   ld e, a
   call _myDrawChar_inner
   ret
   less_than_ten_screen:
   and #0x0F
   add #48  ;; primer digito en Ascii
   ld e, a
   call _myDrawChar_inner
ret

screen_man_death_screen::
   ;;; se deberian eliminar todos los no player
   ld hl, #man_entity_set4destruction_simple
   call man_entity_forall
   call man_entity_update
   call man_entity_update
   call player_restart
   ;;;
   cpctm_clearScreen_asm #0x00
   cpctm_setBorder_asm HW_RED

   ld de, #_death_song
   call play_song

   ld hl, #0x0002
   call cpct_setDrawCharM0_asm   ;; Set draw char colours
   cpctm_screenPtr_asm hl, #0xC000, #20, #72
   ld   iy, #stringD1   ;; IY = Pointer to the string 
   call _myDrawStringM0  ;; Draw the string
   
   ld hl, #0x000F
   call cpct_setDrawCharM0_asm
   cpctm_screenPtr_asm hl, #0xC000, #03, #180
   ld iy, #stringD2
   call _myDrawStringM0

   cpctm_screenPtr_asm hl, #0xC000, #07, #140
   ld iy, #yourScString
   push hl
   call _myDrawStringM0
   pop hl
   push hl
   call sys_ui_update_score_millar_ds
   pop hl
   push hl
   call sys_ui_update_score_centenas_ds
   pop hl
   push hl
   call sys_ui_update_score_decenas_ds
   pop hl
   push hl
   call sys_ui_update_score_unidades_ds
   pop hl

   ld hl, #Key_Space
   call waitKeyPressed

   ld de, #_main_song
   call play_song

   call restart_init
ret


waitKeyPressed:
    push hl 
    call cpct_isKeyPressed_asm
    pop hl
    jr nz, waitKeyPressed

    loop2:
    push hl
    call cpct_isKeyPressed_asm
    pop hl
    jr z, loop2
   
    ld l, #2 ; que sonido va a sonar
    ld h, #15 ; volumen 0-15
    ld e, #80 ; nota 0-143
    ld d, #0 ; speed (0 es original, 1 es la mas rapida)
    ld bc, #0xFFFF ;pitch
    ld a, #10 ;canal del medio
    call cpct_akp_SFXPlay_asm
ret

waitKeyPressed_story:
    push hl 
    call cpct_isKeyPressed_asm
    pop hl
    jr nz, waitKeyPressed_story

    _loop2:
    push hl
    call cpct_isKeyPressed_asm
    pop hl
    jr z, loop2
ret