;; INIT OF RENDER.S

.include "cpctelera.h.s"
.include "./manager/entities.h.s"
.include "./manager/rounds.h.s"
.include "cpct_globals.h.s"
.include "ui.h.s"
.globl _g_palette
.globl _sprite_char
.globl _sprite_puerta_arr
.globl _sprite_puerta_abaj
.globl _sprite_puerta_lat
.globl _sprite_puerta_lat_der
.globl _sprite_vent
.globl _sprite_vent_abaj
.globl _sprite_poster
.globl _sprite_comida
.globl _sprite_poster_rounds

;; ---------------------------------------------
;; Updates all the entities
;; B -> Mask to filter
;; ---------------------------------------------
should_render_lower: .db #0

sys_render_update::
    call draw_food    
    ld hl, #sys_render_upper
    call man_entity_forall
    call sys_ui_draw_food_counter

    wait_for_render_lower_signal:
    ld a, (should_render_lower)
    or a
    jr z, wait_for_render_lower_signal

    xor a
    ld (should_render_lower), a
    ld hl, #sys_render_lower
    call man_entity_forall
ret

set_render_lower::
    ld hl, #should_render_lower
    ld (hl), #1
ret

;
; IDEA RENDER SIN FLICKERING:
;
; RENDER UPPER SCREEN -> for all se renderiza si esta <=130
;
; RENDER LOWER SCREEN -> for all se renderiza si >130
;
;se llama a render upper screen en interrupcion (flag de 0 a 1)
;lower screen en ultima interrupcion (cerca del vsync, pero con la interrupcion 6(creo)) (flag de 0 a 1)
;

draw_door1:
    ld de, #0xF872

    ld c, #12 ;x
    ld b, #15;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #32, #16
    ld de, #0xC0C0
    ld c, #2 ;x
    ld b, #13;y
    ld hl, #_sprite_puerta_arr
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #46, #16
    ld de, #0xC0CE
    ld c, #2 ;x
    ld b, #13;y
    ld hl, #_sprite_puerta_arr
    call cpct_drawSprite_asm
ret

draw_door2:
    ;cpctm_screenPtr_asm de, #0xC000, #78, #49
    ld de, #0xCA2E
    ld c, #2 ;x
    ld b, #48;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #78, #97
    ld de, #0xCC0E
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat_der
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #78, #43
    ld de, #0xD9DE
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat_der
    call cpct_drawSprite_asm
ret

draw_door3:
    ;cpctm_screenPtr_asm de, #0xC000, #78, #128
    ld de, #0xC54E
    ld c, #2 ;x
    ld b, #48;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #78, #176
    ld de, #0xC72E
    ld c, #2 ;x
    ld b, #6 ;y
    ld hl, #_sprite_puerta_lat_der
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #78, #122
    ld de, #0xD4FE
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat_der
    call cpct_drawSprite_asm
ret

draw_door4:
    ;cpctm_screenPtr_asm de, #0xC000, #34, #195
    ld de, #0xDFA2
    ld c, #12 ;x
    ld b, #5;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #46, #196
    ld de, #0xE7AE
    ld c, #2 ;x
    ld b, #4;y
    ld hl, #_sprite_puerta_abaj
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #32, #196
    ld de, #0xE7A0
    ld c, #2 ;x
    ld b, #4;y
    ld hl, #_sprite_puerta_abaj
    call cpct_drawSprite_asm
ret

draw_door5:
    ;cpctm_screenPtr_asm de, #0xC000, #0, #128
    ld de, #0xC500
    ld c, #2 ;x
    ld b, #48;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #0, #176
    ld de, #0xC6E0
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #0, #122
    ld de, #0xD4B0
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat
    call cpct_drawSprite_asm
ret

draw_door6:
    ;cpctm_screenPtr_asm de, #0xC000, #0, #49
    ld de, #0xC9E0
    ld c, #2 ;x
    ld b, #48;y
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #0, #97  
    ld de, #0xCBC0
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat
    call cpct_drawSprite_asm

    ;cpctm_screenPtr_asm de, #0xC000, #0, #43
    ld de, #0xD990
    ld c, #2 ;x
    ld b, #6;y
    ld hl, #_sprite_puerta_lat
    call cpct_drawSprite_asm
ret


sys_render_draw_room::

    ;cpctm_screenPtr_asm de, #0xC000, #0, #10
    ld de, #0xD050
    ld c, #64 ;x
    ld b, #20;y
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #64, #10
    ld de, #0xD090
    ld c, #16 ;x
    ld b, #20;y
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;;;Paredes laterales

    ;cpctm_screenPtr_asm de, #0xC000, #0, #30
    ld de, #0xF0F0
    ld c, #1 ;x
    ld b, #170;y
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #79, #30
    ld de, #0xF13F
    ld c, #1 ;x
    ld b, #170;y
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;; Pared abajo

    cpctm_screenPtr_asm de, #0xC000, #1, #195
    ld de, #0xDF81 
    ld c, #64 ;x
    ld b, #5;y
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #64, #195
    ld de, #0xDFC0
    ld c, #16 ;w
    ld b, #5;h
    ld a, #0xC3
    call cpct_drawSolidBox_asm

    ;;;;Contornos arriba

    ;cpctm_screenPtr_asm de, #0xC000, #2, #29
    ld de, #0xE8F2
    ld c, #64 ;w
    ld b, #1;h
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #66, #29
    ld de, #0xE932
    ld c, #12 ;w
    ld b, #1;h
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;;contornos abajo
    ;cpctm_screenPtr_asm de, #0xC000, #2, #195
    ld de, #0xDF82
    ld c, #64 ;w
    ld b, #1;h

    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #66, #195
    ld de, #0xDFC2
    ld c, #12 ;w
    ld b, #1;h
    ld a, #0x00
    call cpct_drawSolidBox_asm

    ;;;Contornos

    ;cpctm_screenPtr_asm de, #0xC000, #1, #29
    ld de, #0xE8F1
    ld c, #1 ;x
    ld b, #167;y
    ld a, #0x82
    call cpct_drawSolidBox_asm

    ;cpctm_screenPtr_asm de, #0xC000, #78, #29
    ld de, #0xE93E
    ld c, #1 ;x
    ld b, #167;y
    ld a, #0x41
    call cpct_drawSolidBox_asm
    call draw_current_doors
    call draw_decorations
ret

draw_current_doors::
    call get_current_doors
    bucle_dcd:
    ld a, (hl)
    cp #0
    jr z, end_dcd
    cp #1
    jr z, es_1_dcd
    cp #2
    jr z, es_2_dcd
    cp #3
    jr z, es_3_dcd
    cp #4
    jr z, es_4_dcd
    cp #5
    jr z, es_5_dcd
    cp #6
    jr z, es_6_dcd

    es_1_dcd:
    push hl
    call draw_door1
    pop hl
    inc hl
    jr bucle_dcd

    es_2_dcd:
    push hl
    call draw_door2
    pop hl
    inc hl
    jr bucle_dcd

    es_3_dcd:
    push hl
    call draw_door3
    pop hl
    inc hl
    jr bucle_dcd

    es_4_dcd:
    push hl
    call draw_door4
    pop hl
    inc hl
    jr bucle_dcd

    es_5_dcd:
    push hl
    call draw_door5
    pop hl
    inc hl
    jr bucle_dcd

    es_6_dcd:
    push hl
    call draw_door6
    pop hl
    inc hl
    jr bucle_dcd

    end_dcd:
ret


draw_decorations:
    ld de, #0xD054
    ld c, #15 ;x
    ld b, #19;y
    ld hl, #_sprite_vent
    call cpct_drawSprite_asm

    ld de, #0xD083
    ld c, #15 ;x
    ld b, #19;y
    ld hl, #_sprite_vent
    call cpct_drawSprite_asm

    ld de, #0xE7B5
    ld c, #18 ;x
    ld b, #4;y
    ld hl, #_sprite_vent_abaj
    call cpct_drawSprite_asm
    
    ld de, #0xE787
    ld c, #18 ;x
    ld b, #4;y
    ld hl, #_sprite_vent_abaj
    call cpct_drawSprite_asm

    ld de, #0xE095
    ld c, #7 ;x
    ld b, #16;y
    ld hl, #_sprite_poster
    call cpct_drawSprite_asm

    ld de, #0xE864
    ld c, #10 ;x
    ld b, #15;y
    ld hl, #_sprite_poster_rounds
    call cpct_drawSprite_asm
ret


draw_food:
    ld de, #0xFB93
    ld c, #9 ;x
    ld b, #33;y
    ld hl, #_sprite_comida
    call cpct_drawSprite_asm
ret

sys_render_delete::
    ld c, e_w(ix)
    ld b, e_h(ix)
    ld e, e_prv_ptr+0(ix)
    ld d, e_prv_ptr+1(ix)
    ld a, #0x3C
    call cpct_drawSolidBox_asm
ret


sys_render_upper:
    ld a, e_y(ix)
    cp #125
    call c, sys_render_forone
ret


sys_render_lower:
    ld a, e_y(ix)
    cp #125
    call nc, sys_render_forone
ret


.globl _sprite_e1_1
.globl _sprite_e1_2
.globl _sprite_e2_1
.globl _sprite_e2_2
.globl _sprite_zombie_arcoiris
.globl _sprite_zombie_arcoiris2


.globl _sprite_esqueleto
.globl _sprite_esqueleto2
.globl _sprite_murcielago
.globl _sprite_murcielago2
.globl _sprite_arana
.globl _sprite_arana2

sys_render_anim_up:

    ld a, e_cmps(ix)
    and #e_cmps_deathanim
    jp nz, end_anim_up
    ;;no es deathanim
    ld a, e_type(ix)
    cp #e_type_enemy
    jp nz, end_anim_up
    ;;es enemigo
    ;ld a, e_deathanimCtr(ix)
    ;cp #26
    ;jr z, animup
;
    ;cp #13
    ;jr z, animup
;
    ;cp #0
    ;jr z, animup
    ;ret

    ;animup:
    ld a, e_type_enemy_ai(ix)

    cp #e_type_enemy_zombie
    jp z, animup_zombie

    cp #e_type_enemy_bat
    jp z, animup_bat

    cp #e_type_enemy_ghost
    jp z, animup_ghost

    cp #e_type_enemy_spider
    jp z, animup_spider

    cp #e_type_enemy_witch
    jp z, animup_witch
    ret

    animup_zombie:

        ld a, e_deathanimCtr(ix)
        dec a
        ld e_deathanimCtr(ix), a
        jr nz, no_zero_zomb

            ld e_deathanimCtr(ix), #30
            ld hl, #_sprite_e1_1
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h

            ld a, e_zombie_raimbow(ix)
            cp #0
            jr z, end_zomb1_anim

                ld hl, #_sprite_zombie_arcoiris
                ld e_sprite(ix), l
                ld e_sprite+1(ix), h

            end_zomb1_anim:

            ret

        no_zero_zomb:
        cp #15
        jr nz, no_fifteen_zomb
            ld hl, #_sprite_e1_2
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h

            ld a, e_zombie_raimbow(ix)
            cp #0
            jr z, end_zomb2_anim

                ld hl, #_sprite_zombie_arcoiris2
                ld e_sprite(ix), l
                ld e_sprite+1(ix), h

            end_zomb2_anim:
            ret

        no_fifteen_zomb:
    ret

    animup_ghost:

        ld a, e_deathanimCtr(ix)
        dec a
        ld e_deathanimCtr(ix), a
        jr nz, no_zero_ghost
            ld e_deathanimCtr(ix), #30
            ld hl, #_sprite_e2_1
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_zero_ghost:
        cp #15
        jr nz, no_fifteen_ghost
            ld hl, #_sprite_e2_2
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_fifteen_ghost:
    ret

    animup_bat:

    ld a, e_deathanimCtr(ix)
        dec a
        ld e_deathanimCtr(ix), a
        jr nz, no_zero_bat
            ld e_deathanimCtr(ix), #30
            ld hl, #_sprite_murcielago
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_zero_bat:
        cp #15
        jr nz, no_fifteen_bat
            ld hl, #_sprite_murcielago2
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_fifteen_bat:
    ret

    animup_spider:
        ld a, e_deathanimCtr(ix)
        dec a
        ld e_deathanimCtr(ix), a
        jr nz, no_zero_spider
            ld e_deathanimCtr(ix), #30
            ld hl, #_sprite_arana
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_zero_spider:
        cp #15
        jr nz, no_fifteen_spider
            ld hl, #_sprite_arana2
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_fifteen_spider:
    ret

    animup_witch:
    ld a, e_deathanimCtr(ix)
        dec a
        ld e_deathanimCtr(ix), a
        jr nz, no_zero_witch
            ld e_deathanimCtr(ix), #30
            ld hl, #_sprite_esqueleto
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_zero_witch:
        cp #15
        jr nz, no_fifteen_witch
            ld hl, #_sprite_esqueleto2
            ld e_sprite(ix), l
            ld e_sprite+1(ix), h
            ret

        no_fifteen_witch:
    ret


    end_anim_up:
ret




;; -----------------------------------------
;; Renders one entity
;; IX -> entity
;; -----------------------------------------

sys_render_forone::
    call sys_render_delete

    call sys_render_anim_up

    ld de, #0xC000
    ld c, e_x(ix)
    ld b, e_y(ix)
    call cpct_getScreenPtr_asm
    ex de, hl

    ld e_prv_ptr+0(ix), e
    ld e_prv_ptr+1(ix), d

    ld h, e_sprite+1(ix)
    ld l, e_sprite+0(ix)

    ld c, e_w(ix)
    ld b, e_h(ix)
    ld a, e_type(ix)
    cp #e_type_bullet
    jr nz, rend_sprite

    ld a, e_color(ix) ;; Color de la bala
    call cpct_drawSolidBox_asm
    jr final_rend

    rend_sprite:
    ld a, e_type(ix)
    cp #e_type_mainchar
    jr nz, not_mainchar

    ld a, e_playerctr(ix)
    cp #1
    jr z, not_mainchar

    cp #100
    jr nc, final_rend

    cp #90
    jr nc, not_mainchar

    cp #80
    jr nc, final_rend

    cp #70
    jr nc, not_mainchar

    cp #60
    jr nc, final_rend

    cp #50
    jr nc, not_mainchar

    cp #40
    jr nc, final_rend

    cp #30
    jr nc, not_mainchar

    cp #20
    jr nc, final_rend

    cp #10
    jr nc, not_mainchar

    not_mainchar:
    ld a, e_type(ix)
    cp #e_type_food
    jr nz, not_food_rend
        ld a, e_death(ix)
        cp #0
        jr nz, not_food_rend
        ld a, e_food_final_reder(ix)

        cp #110
        jr nc, not_food_rend

        cp #100
        jr nc, final_rend

        cp #90
        jr nc, not_food_rend

        cp #80
        jr nc, final_rend

        cp #70
        jr nc, not_food_rend

        cp #60
        jr nc, final_rend

        cp #50
        jr nc, not_food_rend

        cp #40
        jr nc, final_rend

        cp #30
        jr nc, not_food_rend

        cp #20
        jr nc, final_rend

        cp #10
        jr nc, not_food_rend

    not_food_rend:
    call cpct_drawSprite_asm
    final_rend:
ret

sys_render_init::
    cpctm_clearScreen_asm #0x3C
    call sys_render_draw_room
ret

sys_render_init_palette::
    halt
    ld c, #0 ;modo 0
    call cpct_setVideoMode_asm
    ld hl, #_g_palette
    ld de, #16
    call cpct_setPalette_asm
ret