.include "render.h.s"
.include "man/entity.h.s"
.include "tilemaps/level1.h.s"

.globl _main_palette
.globl _spr_vidasboss_5
.globl _spr_vidasboss_4
.globl _spr_vidasboss_3
.globl _spr_vidasboss_2
.globl _spr_vidasboss_1
.globl _spr_vidasboss_0

;; ===============================
;; INICIALIZA LA PNTALLA
;; Destroy: AF, DE, BC, HL
;; Input: IX --> Pointers to the entity
;; ===============================
render_init:

    ;; Establecemos el Modo de video
    ld c, #0
    call cpct_setVideoMode_asm

    ;; Establecemos la paleta
    ld hl, #_main_palette
    ld de, #16
    call cpct_setPalette_asm

    ;; Ponemos el color del borde igual que el fondo
    ld l, #16    ;; color del borde en la paleta
    ld h, #20     ;; color del fondo
    call cpct_setPALColour_asm
ret

_tilemap1:
_tilemap2:
_tilemap3:
_tilemap4:
_tilemap5:
_tilemap6:
_tilemap7:
_tilemap8:
_tilemap9:


;; ===============================
;; INICIALIZA Y PINTA EL TILEMAP
;; Destroy: AF, DE, BC, HL
;; ===============================
tilset_ptr = 0x2b6 + 500
tilemap_ptr = 0x2b6
render_tilemap:

    ld b, #_tilemap1_H ;;height en tiles de la ventana
    ld c, #_tilemap1_W
    ld de, #_tilemap1_W
    ld hl, #tilset_ptr
    call cpct_etm_setDrawTilemap4x8_ag_asm
;;
    ;;ld hl, #0xC000
    ;;ld de, #_tilemapM
    ld hl, #0xC000
    ld de, #tilemap_ptr
    call cpct_etm_drawTilemap4x8_ag_asm
ret

;; ===============================
;; DIBUJA UNA ENTIDAD
;; Destroy: AF, DE, BC, HL
;; Input: IX --> Pointers to the entity
;; ===============================
render_draw_one:
    ;;si la entidad esta muerta pinto un cubo encima
    ld    de, #0xC000      ;; Starting adrees of screen
    ld    c, e_x(ix)       ;; C = entity_X
    ld    b, e_y(ix)       ;; B = entity_Y
    call  cpct_getScreenPtr_asm ;; returns the position in hl
    ex de, hl

    

    ;; si esta en mitad de una animacion no miro el tipo
    ld a, e_anim_especial(ix)
    cp #1
    jp z, pinto
    
    ld a, e_tipo(ix)
    cp #e_type_dead
    jp z, noPinto
        pinto:

        ;ex de, hl            ;; DrawSolidSprite needs the position in DE
        ld l, e_spriteL(ix)
        ld h, e_spriteH(ix)

        ld b,  e_h(ix)       ;; Height
        ld c,  e_w(ix)       ;; Width
        call cpct_drawSprite_asm
        
        call render_draw_boss
        ret

    noPinto:
        ld a, #0x0C
        ld b,  e_h(ix)       ;; Height
        ld c,  e_w(ix)       ;; Width
        call cpct_drawSolidBox_asm
ret

;; ===============================
;; RENDERIZAR VIDAS BOSS
;; Input: IX-> entidad boss
;; Destroy: bc, de, af, hl
;; ===============================
render_draw_boss:
    ld b, #e_cmp_vidaextra ;para saber que es el boss
    ld a, e_cmps(ix) 
    and b
    cp b
    jr nz, noBoss
        ;; es el boss
        ld    de, #0xC000      ;; Starting adrees of screen
        ld    c, e_x(ix)       ;; C = entity_X

        ld a, e_y(ix)
        sub #0x08
        ld b, a
        call  cpct_getScreenPtr_asm ;; returns the position in hl
        ex de, hl
        
        ld a, e_hp(ix)
        cp #5
        jp nz, no_es_5
            ld hl, #_spr_vidasboss_5
            jr pinto_vida
        no_es_5:
        cp #4
        jp nz, no_es_4
            ld hl, #_spr_vidasboss_4
            jr pinto_vida
        no_es_4:
        cp #3
        jp nz, no_es_3
            ld hl, #_spr_vidasboss_3
            jr pinto_vida
        no_es_3:
        cp #2
        jp nz, no_es_2
            ld hl, #_spr_vidasboss_2
            jr pinto_vida
        no_es_2:
            ld hl, #_spr_vidasboss_1

        pinto_vida:
        ld bc, #0x0C0C
        call cpct_drawSprite_asm
    noBoss:
ret


;; ===============================
;; EJECUTA PARA TODAS LAS ENTIDADES
;; Input:
;; Output:
;; ===============================
render_update:
    ld hl, #render_draw_one
    ld b, #e_cmp_render
    call entity_doForAll_matching
ret


