;;
;; RENDER SYSTEM
;;
.include "cpctelera.h.s"
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area _CODE


rendersys_init::
    ;cpct_setVideoMode(0);
	ld	c, #0
	call	cpct_setVideoMode_asm
    ;cpct_setBorder(HW_BLACK);
	ld	hl, #0x1410
	call	cpct_setPALColour_asm
    ;cpct_setPalette(palette,16);
	ld  de, #16
	ld	hl, #_g_palette
	call	cpct_setPalette_asm
	ret 

;;INPUT:
;;  IX: Pointer to entity to render
render_player::

	ld b, #_e_type_dead
    ld a, e_tipo(ix) 
    and b      
    cp a,b      
    jr nz, inicio_render
	call render_clean
	call render_dead
	ret

	inicio_render:

	ld b, #_e_type_render
    ld a, e_tipo(ix) 
    and b
	ret z      
   	;cp a,b      
    ;ret nz


	;; SE PUEDE OPTIMIZAR SI QUITO ESTO Y PONGO QUE LA POS BORRADO SEA LA POS INICIAL DE BASE
	;ld b, e_posicion_borrado+1(ix)
	;ld a, e_posicion_borrado(ix)
	;or b
	;jr z, no_render_clean
	;call render_clean
	;no_render_clean:

	call render_clean

	ld a, e_cont_no_hit(ix)
	ld b, #1
	and b
	ret nz

	
	;;Calculate a video memory location 
    ld      de, #0xC000
    ld      c, e_x(ix) ;;x
    ld      b, e_y(ix) ;;y
    call    cpct_getScreenPtr_asm
	ex 		de, hl 
	
    ld      b, e_h(ix) ;;Height
    ld      c, e_w(ix) ;;Width
    ld 		l ,e_puntero(ix) ;; POSICION SPRITE
	ld		h, e_puntero+1(ix)
	call    cpct_drawSprite_asm


	
	
salir:
ret 


render_enemigo::

	;;Calculate a video memory location 
    ld      de, #0xC000
    ld      c, e_x(ix) ;;x
    ld      b, e_y(ix) ;;y
    call    cpct_getScreenPtr_asm
	ex 		de, hl 
	
    ld      b, e_h(ix) ;;Height
    ld      c, e_w(ix) ;;Width
    ld		l ,e_puntero(ix) ;; POSICION SPRITE
	ld		h, e_puntero+1(ix)
	call    cpct_drawSprite_asm

ret 






render_atack:

	call entity_man_getEntityArray_IX

	ld 		b, e_at_dir(ix)
	ld 		a, #0
	or a, b
	ret z
	
	
	
	

	call determinat_pos_atack
	dec e
	ret z

	ld b, e_atack_pos_antx(ix)
	ld a, e_atack_pos_anty(ix)
	or b
	jr z, no_render_clean_at
	call render_clean_atack
	no_render_clean_at:


	ld      de, #0xC000
	ld a,e_atack_posy(ix)
	ld b,a
	ld a,e_atack_posx(ix)
	ld c,a


	dibujar_atq:

    call    cpct_getScreenPtr_asm

	ld a, e_at_dir(ix)
	dec a
	jr z, der_izq_at
	dec a
	jr z, abajo_arriba_at
	dec a
	jr z, der_izq_at
	dec a
	jr z, abajo_arriba_at


	abajo_arriba_at:
		ld      b, #16
		ld      c, #8
	jp f_r_at

	der_izq_at:
		ld      b, #16
		ld      c, #8


	
	f_r_at:
	ex 		de, hl 
    ld 		l, e_atack_spr(ix)
	ld 		h, e_atack_spr+1(ix)
	
	call    cpct_drawSprite_asm

ret



rendersys_update::
	call render_atack
    ld hl, #render_player
    call man_entity_forall
	
ret





render_clean::

	;call entity_man_getEntityArray_IX
	ld b, #_e_type_render
    ld a, e_tipo(ix) 
    and b      
    cp a,b      
    ret nz

	ld      de, #0xC000
    ld      c, e_posicion_borrado(ix)   ;; X
	dec c
    ld      b, e_posicion_borrado+1(ix) ;; Y
	dec b
    call    cpct_getScreenPtr_asm

	;ld e,e_posicion_borrado(ix)
	;ld d,e_posicion_borrado+1(ix)

	ex		de, hl

    ld      a, #0x3F
    ld      c, e_w(ix)
	inc c
	inc c
	ld 		b, e_h(ix)
	inc b 
	inc b
	inc b 



	call    cpct_drawSolidBox_asm

ret



render_dead::

	;call entity_man_getEntityArray_IX
	
	ld      de, #0xC000
    ld      c, e_x(ix)
	dec c
    ld      b, e_y(ix)
	dec b
	dec b
	dec b
	dec b
    call    cpct_getScreenPtr_asm

	;ld e,e_posicion_borrado(ix)
	;ld d,e_posicion_borrado+1(ix)

	ex		de, hl

    ld      a, #0x3F
    ld      c, e_w(ix)
	inc c
	inc c

	ld 		b, e_h(ix)
	inc b 
	inc b
	inc b 
	inc b
	inc b 
	inc b
	inc b 
	inc b

	call    cpct_drawSolidBox_asm



	
ret







render_clean_atack_end::

	

	;call entity_man_getEntityArray_IX
	
	ld      de, #0xC000
    ld      c, e_atack_posx(ix)
    ld      b, e_atack_posy(ix)
    call    cpct_getScreenPtr_asm

	;ld e,e_posicion_borrado(ix)
	;ld d,e_posicion_borrado+1(ix)

	ex		de, hl

    ld      a, #0x3F
    ld      c, e_atack_w(ix)
	ld 		b, e_atack_h(ix)

	call    cpct_drawSolidBox_asm



	
ret



render_clean_atack::

	;call entity_man_getEntityArray_IX
	
	ld      de, #0xC000
    ld      c, e_atack_pos_antx(ix)
    ld      b, e_atack_pos_anty(ix)
    call    cpct_getScreenPtr_asm

	;ld e,e_posicion_borrado(ix)
	;ld d,e_posicion_borrado+1(ix)

	ex		de, hl

    ld      a, #0x3F
    ld      c, e_atack_w(ix)
	ld 		b, e_atack_h(ix)

	call    cpct_drawSolidBox_asm



	
ret














;; MAPAS
cargar_mapa_malo::

	ld hl, #_g_tileset
	push hl
	call cpct_etm_setTileset2x4_asm

	ld hl, #_g_level0
	push hl
	ld hl, #0xC000
	push hl
	
	ld bc, #0000
	ld de, #0x2020

	ld a, #32

	call cpct_etm_drawTileBox2x4_asm
ret


cargar_mapa::
	ex de,hl 
	ld hl, #_g_tileset
	call cpct_etm_setTileset2x4_asm

	ld a, #40
	ld c, #50
	ex de,hl
	ld de, #0xC000
	;ld hl, #_g_level0
	

	call cpct_etm_drawTilemap2x4_f_asm
ret
