.include "cmp/array_structure.h.s"
.include "man/entity.h.s"
.include "cmp/entity.h.s"
.include "cpctelera.h.s"
.include "man/entity_collision.h.s"
.include "cpct_functions.h.s"

.include "assets/assets.h.s"

.module entity_manager


DefineComponentArrayStructure _entity, max_entities, DefineCmp_Entity_default

_position2: .db 0

ent: DefineCmp_entity 0x01, 70, _position2, 0xFF, 0, 10, 16, _spr_pez

man_entity_getArray::
	ld ix, #_entity_array
	ld a, (_entity_num)
	ret


man_entity_init::

	call man_entity_collision_init


	xor a
	ld (_entity_num), a

	ld hl, #_entity_array
	ld (_entity_pend), hl

	ret


man_entity_new::

	ld hl, #_entity_num
	inc (hl)


	ld hl, (_entity_pend)
	ld d, h
	ld e, l
	ld bc, #sizeof_e
	add hl, bc
	ld (_entity_pend), hl

	ret


;;void man_entity_update(){
;;	Entity_t* e=m_entities;
;;	while(e->type!=e_type_invalid){
;;		if(e->type & e_type_dead){
;;			man_entity_destroy(e);
;;		}else{
;;			++e;
;;		}
;;	}
;;}
;;
;;Entity_t* man_entity_create(){
;;	Entity_t* e=m_next_free_entity;
;;	m_next_free_entity=e+1;
;;	e->type=e_type_default;
;;	++m_num_entities;
;;	return e;
;;}
;;
;;void man_entity_destroy(Entity_t* dead_e){
;;	Entity_t* de=dead_e;
;;	Entity_t* last= m_next_free_entity;
;;	--last;
;;	if(de!=last)
;;		cpct_memcpy(de,last,sizeof(Entity_t));
;;	last->type=e_type_invalid;
;;	m_next_free_entity=last;
;;	--m_num_entities;
;;}

man_entity_destroy::
	ld hl, #_entity_num
	dec (hl)

	;;ld a, e_cmp_type(ix)
	;;and #e_cmp_Physics
	;;call nz, man_entity_collision_sub

	ld  e,e_lastVP_l(ix)
    ld  d,e_lastVP_h(ix)
    ld  a,#0x3c
    ld  c,e_w(ix)
    ld  b,e_h(ix)
    call cpct_drawSolidBox_asm

	ld 	hl,(_entity_pend)
	ld 	bc,#-sizeof_e
	add hl,bc
	ld 	(_entity_pend),hl

	ld__a_ixl
	sub l
	jr 	nz,_copy
	ld__a_ixh
	sub h
	jr 	z,_nocopy
_copy:
	ld__e_ixl
	ld__d_ixh
	ld 	bc,#sizeof_e
	ldir 
	ld 	bc,#-sizeof_e
	add hl,bc
	;copiar hl en de entonces de hay q ponerla a invalid
	
_nocopy:
	ld (hl),#e_cmp_Invalid

ret
;;
;;	ld hl, #_entity_num
;;	dec (hl)
;;
;;	ld hl, (_entity_pend)
;;	ld bc, #sizeof_e
;;	ld bc, #0xFFF5
;;	add hl, bc
;;	;;ld (_entity_pend), hl
;;
;;	push iy
;;	push de
;;
;;	ld d, h
;;	ld e, l
;;	ld__iyh_d
;;	ld__iyl_e
;;
;;	ld a, e_y(ix)
;;	ld h, e_y(iy)
;;
;;	and a, h;;sub de ix e iy y mirar si es 0?
;;	ld__de_ix;; ld de, ix
;;	ld__hl_iy;; ld hl iy
;;	ld bc, #sizeof_e
;;	call z, cpct_memcpy_asm
;;
;;	pop iy
;;	pop de
;;
;;	ld a, #e_cmp_Invalid
;;	ld e_cmp_type(ix), a
;;
;;
;;	ld hl, (_entity_pend)
;;	ld bc, #sizeof_e
;;	ld bc, #0xFFF5
;;	add hl, bc
;;	ld (_entity_pend), hl
;;
;;	ret


man_entity_create::
	push hl
	call man_entity_new

	ld__ixh_d
	ld__ixl_e

	pop hl
	ldir


	ld a, e_cmp_type(ix)
	and #e_cmp_Physics
	call nz, man_entity_collision_add

	ret









man_entity_set4destruction::

	ld 	a,e_cmp_type(ix)
	or #e_cmp_Dead
	ld 	e_cmp_type(ix),a

	ret

man_entity_set4destruction_all::
	ld 	a,(_entity_num)
	ld 	(destructor),a
	ld 	ix,#_entity_array

	ld a, #40
	ld e_x(ix), a
	ld a, #180
	ld e_y(ix), a

	ld de, #sizeof_e
	add ix, de

_looped:
	ld a,e_cmp_type(ix)
    and #e_cmp_Enemy
    sub #e_cmp_Enemy
    jr z,_otra

	ld 	a,e_cmp_type(ix)
	or #e_cmp_Dead
	ld 	e_cmp_type(ix),a

_otra:
	ld 	a,(destructor)
	dec a
	ret z
	ld  (destructor),a
	
	ld de, #sizeof_e
	add ix, de
	jr _looped
ret 



;;	;;ld a, e_cmp_type(ix)
;;	;;xor #e_cmp_Dead
;;	;;jr z, _algo
;;	ld a, #e_cmp_Invalid
;;	ld e_cmp_type(ix), a
;;
;;;;_algo:
;;	ld a, e_cmp_type(ix)
;;	xor #e_cmp_Invalid
;;	jr nz, _sus
;;	ld a, #0xFF
;;	ld (0xC000), a
;;_sus:
;;	ret



man_entity_update::
	ld 	a,(_entity_num)
	;;inc a
	;;ld 	a,#max_entities
	ld 	(contador),a
	ld 	ix,#_entity_array

_bucle:
	;;ld 	a,e_cmp_type(ix)
	;;or a
	;;jr z, not_to_destroy

	ld 	a,e_cmp_type(ix)
	and #e_cmp_Dead
	sub #e_cmp_Dead
	jr nz,not_to_destroy

	call man_entity_destroy

not_to_destroy:
	ld 	a,(contador)
	dec a
	ret z

	ld  (contador),a

	;;ld 	a,e_cmp_type(ix)
	;;or a
	;;call z, man_entity_generate

	ld de, #sizeof_e
	add ix, de
jr _bucle

ret


;;man_entity_generate::
;;
;;	ld hl, #_entity_num
;;	ld a, #max_entities-1
;;	sub a, (hl)
;;	or a
;;	ret z
;;	;;jr z, _salto
;;
;;	ld hl, #ent
;;	call man_entity_create
;;	ld a, (_position2)
;;	ld e_y(ix), a
;;
;;_salto:
;;
;;
;;	ld a, (_position2)
;;	add a, #40
;;
;;	ld (_position2), a
;;
;;
;;	sub #200
;;	or a
;;
;;;;	ld a, #0xFF
;;;;	ld (0xc000), a
;;
;;	jr z, _sumar
;;
;;	ret
;;
;;_sumar:
;;	
;;;;	ld a, #0xFF
;;;;	ld (0xc000), a
;;
;;	ld a, #0
;;	ld (_position2), a
;;	ret




man_entity_generate::


	ld hl, #_entity_num
	ld a, #max_entities-1
	sub a, (hl)
	or a
	jr z, _salto



	ld hl, #ent
	call man_entity_create
	ld a, (_position2)
	ld e_y(ix), a

_salto:


	ld a, (_position2)
	add a, #40

	ld (_position2), a


	sub #200
	or a

;;	ld a, #0xFF
;;	ld (0xc000), a

	jr z, _sumar

	ret

_sumar:
	
;;	ld a, #0xFF
;;	ld (0xc000), a

	ld a, #0
	ld (_position2), a
	ret
