.include "./system/ui.h.s"
.include "screen.h.s"
.include "./system/render.h.s"
.include "entities.h.s"
.include "music.h.s"
.globl spawn_enemy1
.globl spawn_enemy2
.globl end_game_func

spawner_time == 125 ;;en updates max 256
spawner_time2 == 175 ;;en updates max 256
round_end_time == 1

round:: .db #1
max_enems: .db #3
curr_enems: .db #0
enems_to_spawn: .db #5
enems_left_to_spawn: .db #5
enems_left: .db #5
spawn_timer: .db spawner_time
time_to_next_round: .db #round_end_time

;;;;;;;;;;;;;;;;;;;
; Niveles

;;  puertas
;;-------------------------
;;           1            ;
;;6                      2;
;;                        ;
;;                        ;
;;5                      3;
;;           4            ;
;;---------------------------

;;!!!!!!! 0 AL FINAL DE CADA ARRAY O SE ROMPE !!!!!!!!!!!!  

doors1:  .db #2,#0
doors2:  .db #1,#2,#0
doors3:  .db #1,#5,#0
doors4:  .db #4,#3,#6,#0
doors5:  .db #2,#5,#0
doors6:  .db #4,#2,#5,#0
doors7:  .db #2,#5,#6,#0
doors8:  .db #1,#2,#4,#5,#0
doors9:  .db #2,#3,#5,#6,#0
doors10: .db #1,#3,#4,#6,#0
doors11: .db #1,#5,#6,#0
doors12: .db #1,#2,#6,#0
doors13: .db #1,#3,#4,#5,#0
doors14: .db #1,#2,#4,#5,#6,#0
doors15: .db #1,#2,#3,#4,#0
doors16: .db #2,#3,#5,#0
doors17: .db #1,#4,#5,#6,#0
doors18: .db #2,#3,#4,#5,#0
doors19: .db #2,#3,#4,#6,#0
doors20: .db #1,#2,#3,#4,#5,#6,#0

door_pointers:
    .dw #doors1
    .dw #doors2
    .dw #doors3
    .dw #doors4
    .dw #doors5
    .dw #doors6
    .dw #doors7
    .dw #doors8
    .dw #doors9
    .dw #doors10
    .dw #doors11
    .dw #doors12
    .dw #doors13
    .dw #doors14
    .dw #doors15
    .dw #doors16
    .dw #doors17
    .dw #doors18
    .dw #doors19
    .dw #doors20
    .dw 0x0000

;;!!!!!!! 0 AL FINAL DE CADA ARRAY O SE ROMPE !!!!!!!!!!!!
;1 zombie
;2 ghost
;3 ovni
;4 bat
;5 calabaza
;6 araña
;7 Skull
;8 Comida (sprite araña)

enemies1:  .db #1,#0                    ;; Zombie, 1 puerta------------------ APARECE ZOMBIE
enemies2:  .db #1,#0                    ;; Zombie, 2 puertas
enemies3:  .db #1,#2,#0                 ;; Zombie, Ghost, 2 puertas----------------- APARECE GHOST
enemies4:  .db #1,#2,#0                 ;; Zombie, Ghost, 3 puertas
enemies5:  .db #1,#3,#0                 ;; Zombie, UFO, 2 puertas -------------------- APARECE UFO        
enemies6:  .db #3,#0                    ;; UFO, 3 puertas
enemies7:  .db #2,#3,#0                 ;; Ghost, UFO, 3 puertas
enemies8:  .db #1,#2,#3,#0              ;; Zombie, Ghost, UFO 4 puertas
enemies9:  .db #1,#5,#0                 ;; Zombie, Calabaza ----------------------------- APARECE CALABAZA
enemies10: .db #1,#2,#5,#0              ;; Zombie, ghost, calabaza
enemies11: .db #2,#3,#5,#0              ;; ghost, UFO, calabaza
enemies12: .db #1,#2,#6,#0              ;; Zombie, ghost, Araña ------------------------- APARECE ARAÑA
enemies13: .db #1,#2,#5,#6,#0           ;; Zombie, ghost, Araña, calabaza
enemies14: .db #2,#3,#5,#6,#0           ;; Ghost, Calabaza, Araña, ufo
enemies15: .db #1,#2,#4,#0              ;; Zombie, Ghost, Bat --------------------------- APARECE BAT
enemies16: .db #1,#2,#4,#5,#0           ;; Zombie, Ghost, Bat, Calabaza
enemies17: .db #2,#3,#4,#6,#0           ;; Ghost, UFO, Bat, araña
enemies18: .db #1,#1,#2,#7,#0           ;; Zombie, ghost, skull -------------------- APARECE SKULL
enemies19: .db #1,#2,#3,#4,#7,#0        ;; Zombie, Ghost, Bat, UFO, Skull
enemies20: .db #1,#2,#3,#4,#5,#6,#7,#0  ;; Todos los enemigos

enemy_pointers:
    .dw #enemies1
    .dw #enemies2
    .dw #enemies3
    .dw #enemies4
    .dw #enemies5
    .dw #enemies6
    .dw #enemies7
    .dw #enemies8
    .dw #enemies9
    .dw #enemies10
    .dw #enemies11
    .dw #enemies12
    .dw #enemies13
    .dw #enemies14
    .dw #enemies15
    .dw #enemies16
    .dw #enemies17
    .dw #enemies18
    .dw #enemies19
    .dw #enemies20
    .dw 0x0000

current_doors: .dw #doors1
current_enemies: .dw #enemies1

weapon: .db #0
;;;;;;;;;;;;;;;;;;;
get_weapon::
    ld a, (weapon)
ret

get_enems_left_to_spawn::
    ld a, (enems_left_to_spawn)
ret

;recibe a
set_weapon:
    ld (weapon), a
ret

get_current_doors:: ;mete en hl el puntero a la primera posicion de las puertas actuales
    push ix
    ld ix, #current_doors
    ld h, 1(ix)
    ld l, 0(ix)
    pop ix
ret

get_current_enemies:: ;mete en hl el puntero a la primera posicion de los enemigos actuales
    push ix
    ld ix, #current_enemies
    ld h, 1(ix)
    ld l, 0(ix)
    pop ix
ret

adjust_round:
    ld a, (round)
    ld hl, #door_pointers ;;hl apunta a el puntero del nivel 1
    
    bucle_ar:
        dec a
        jr z, end_bucle1_ar ;;Se aumenta hl de 2 en 2 hasta que apunte al puntero de esta ronda
        inc hl
        inc hl
        jr bucle_ar
    end_bucle1_ar: ;;aqui hl ya apunta al nivel que es

    ld a, (hl)
    ld (current_doors), a
    inc hl
    ld a, (hl)
    ld (current_doors+1), a ;;Ahora current doors deberia ser un puntero a las puaertas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

    ld a, (round)
    ld hl, #enemy_pointers ;;hl apunta a el puntero del nivel 1
    bucle2_ar:
        dec a
        jr z, end_bucle2_ar ;;Se aumenta hl de 2 en 2 hasta que apunte al puntero de esta ronda
        inc hl
        inc hl
        jr bucle2_ar
    end_bucle2_ar: ;;aqui hl ya apunta al nivel que es

    ld a, (hl)
    ld (current_enemies), a
    inc hl
    ld a, (hl)
    ld (current_enemies+1), a ;;Ahora current enemies deberia ser un puntero a las puertas


ret
get_round::
    ld a, (round)
ret

init_round_1::
    ld hl, #round
    ld (hl), #1
    ld a, #0
    ld (weapon), a
    ld hl, #max_enems
    ld (hl), #3

    ld hl, #curr_enems
    ld (hl), #0

    ld hl, #enems_to_spawn
    ld (hl), #5

    ld a, (#enems_to_spawn)
    ld hl, #enems_left
    ld (hl), a

    ld a, (#enems_to_spawn)
    ld hl, #enems_left_to_spawn
    ld (hl), a

    ld hl, #time_to_next_round
    ld (hl), #round_end_time
    
    ld a, #spawner_time
    ld (spawn_timer), a

    call adjust_round

    ld de, #_fight_song;;;;;;;;;;
    call cpct_akp_musicInit_asm;
ret

enemy_died::
    ld hl, #curr_enems
    dec (hl)

    ld hl, #enems_left
    dec (hl)
ret

inc_maxenems:
    ld hl, #max_enems
    inc (hl)
ret

check_game_ended:
    ld a, (round)
    inc a
    ld hl, #door_pointers ;;hl apunta a el puntero del nivel 1
    
    bucle_ar_cgm:
        dec a
        jr z, end_bucle1_ar_cgm ;;Se aumenta hl de 2 en 2 hasta que apunte al puntero de esta ronda
        inc hl
        inc hl
        jr bucle_ar_cgm
    end_bucle1_ar_cgm: ;;aqui hl ya apunta al nivel que es

    inc (hl)
    dec (hl)
    jr nz, not_ended_yet  ;;chequeo de si hl es 0000, se acaba el juego
        inc hl
        inc (hl)
        dec (hl)
        jr z, end_game_rounds
        dec hl
    not_ended_yet:
    ld a, #0
    inc a
    ret

    end_game_rounds:
    call sys_ui_get_life ;;ROMPO A, B, IY
    call inc_decenas_score

    call end_game_func
    ld a, #1
    dec a
ret

next_round:
    call check_game_ended
    jr z, no_n_r

    ld hl, #man_entity_set4destruction_simple ;;intento de arreglar bug
    call man_entity_forall
    call man_entity_update
    call man_entity_update

    call screen_man_lvl_screen

    ld de, #_fight_song;;;;;;;;;;
    call cpct_akp_musicInit_asm;

    ld hl, #round
    inc (hl)
    call inc_unidades_rounds
    ld a, (round)
    cp #6
    jr nz, gun_pistol
    ld a, #1 
    ld (weapon), a

    gun_pistol:

    ld a, (round)
    cp #14
    jr nz, gun_metralleta
    ld a, #2 
    ld (weapon), a

    gun_metralleta:

    ld a, (hl)
    cp #5
    call z, inc_maxenems;;maxenems aumentará de 3 a 4 en la ronda 5

    ld hl, #curr_enems
    ld (hl), #0

    ld hl, #enems_to_spawn
    inc (hl)

    ld a, (round)
    cp #12
    jr nc, uno_por_uno
    inc (hl)
    uno_por_uno:

    ld a, (#enems_to_spawn)
    ld hl, #enems_left
    ld (hl), a

    ld a, (#enems_to_spawn)
    ld hl, #enems_left_to_spawn
    ld (hl), a

    ld hl, #time_to_next_round
    ld (hl), #round_end_time
    
    call adjust_round
    call sys_render_init
    call sys_ui_add_round
    call sys_ui_init
    no_n_r:
ret

dec_enems_left_to_spawn::
    ld hl, #enems_left_to_spawn
    dec (hl)
ret

spawner_check:
    ld a, (round)
    cp #7
    jr nc, reset_time_2
    
    ld a, #spawner_time
    jr after_calc_time_s

    reset_time_2:
    ld a, #spawner_time2

    after_calc_time_s:
    ld (spawn_timer), a ;;actualizo el timer a 100
    ld a, (enems_left) ;;chequeo si ya spawnee todos los enemigos
    cp #0 
    jr z, next_round_check
    jp m, next_round_check

    ld a, (max_enems)
    ld b, a
    ld a, (curr_enems)
    cp b
    jr z, end_spawn_check ;;si el numero de enemigos curr y max es distinto, se spawnea enemigo

    ld a, (enems_left_to_spawn) 
    cp #0
    jr z, end_spawn_check

    ld a, (round)
    cp #7
    jr c, solo_un_enemigo

    ld a, (curr_enems)
    inc a
    ld hl, #max_enems
    cp (hl)
    jr z, solo_un_enemigo

    ld a, (enems_left_to_spawn) 
    cp #1
    jr z, solo_un_enemigo
    call spawn_enemy2 ;; 2 enemigos
    ld hl, #curr_enems
    inc (hl)
    inc (hl)
    jr end_spawn_check

    solo_un_enemigo:
    call spawn_enemy1
    ld hl, #curr_enems
    inc (hl)
    jr end_spawn_check

    next_round_check:
    ld a, (time_to_next_round)
    or a
    jr z, go_to_next_round
    dec a
    ld (time_to_next_round), a
    jr end_spawn_check
    go_to_next_round:
    call next_round
    end_spawn_check:
ret

update_rounds::
    ld a, (spawn_timer)
    cp #0
    jr nz, skip_update_rounds
    call spawner_check

    skip_update_rounds:
    ld hl, #spawn_timer
    dec (hl)
ret