.include "cmp/entity.h.s"
.include "man/entity.h.s"
.include "man/entity_collision.h.s"
.include "cpctelera.h.s"
.include "man/menu.h.s"
.include "sys/collision.h.s"
.include "sys/render.h.s"



sys_collision_init::

	call man_entity_collision_getArrayHL
	ld (_ent_array_ptr), hl
	ret


.macro saveSetBorder _color
	push hl
	cpctm_setBorder_asm _color
	pop hl
.endm

sys_collision_update::


	;;ld a, e_cmp_type(ix)
	;;xor #e_cmp_Invalid
	;;jr z, _exit
	

_ent_array_ptr = . + 1
	ld hl,#0x0000

	;;saveSetBorder HW_GREEN

_next_ix:


	ld e, (hl)
	inc hl
	ld d, (hl)
	inc hl

;;;;	ld a, e;;
;;;;	or d
;;;;	jr z, _exit

	push hl

	ld__ixl_e
	ld__ixh_d

_next_iy:

	

	ld e, (hl)
	inc hl
	ld d, (hl)
	inc hl

	ld a, e
	or d
	jr z, _trozo


	ld__iyl_e
	ld__iyh_d


	ld 	a,e_cmp_type(iy)
	or a
	jr z, _no_collision
	;;ret z

	



	call sys_collision_check
	jr c, _no_collision
_collision:
	ld     a,e_cmp_type(iy)
    and #e_cmp_Enemy
    sub #e_cmp_Enemy
    jr nz,_sigue
    ld a, #0xFF
    ld (0xc000), a
    call man_entity_set4destruction_all
    push iy
    call man_menu_init
    ;;call man_menu_punt
    call man_menu_play
    call sys_tilemap_init
    pop iy
_sigue:

	ld a,e_cmp_type(iy)
    and #e_cmp_Enemy
    sub #e_cmp_Enemy
    jr z,_next_iy
	push ix
	ld__ix_iy
	call man_entity_set4destruction
	pop ix
	;;ld 	a,(puntuacion)
	;;inc a
	;;inc a
	;;inc a
	;;inc a
	;;inc a
	;;ld  (puntuacion),a
	;;saveSetBorder HW_RED
	jr _next_iy
_no_collision:
	;;saveSetBorder HW_GREEN
	jr _next_iy

_trozo:
	pop hl
;;;;	jr _next_ix
_exit:
	;;saveSetBorder HW_WHITE
	ret



sys_collision_check::
	ld 	a,e_cmp_type(ix)
	or a

	
	ld a, e_x(ix)
	add e_w(ix)
	sub e_x(iy)
	;;jr c, __no_collision
	ret c

	ld a, e_x(iy)
	add e_w(iy)
	sub e_x(ix)
	;;jr c, __no_collision
	ret c


	ld a, e_y(ix)
	add e_h(ix)
	sub e_y(iy)
	;;jr c, __no_collision
	ret c

	ld a, e_y(iy)
	add e_h(iy)
	sub e_y(ix)
	;;jr c, __no_collision
	

	;;ld a, #0xFF
	;;ld (0xC000), a
	ret

;;__no_collision:
;;	ld a, #0x00
;;	ld (0xC000), a


;;	ret