;;============= GLOBALES DEL MANAGER DE ENTIDADES =============                          
                .globl man_entity_create
                .globl man_entity_create_all
                .globl man_entity_getPlayerDeadFlag
                .globl man_entity_for_all_matching
                .globl man_entity_for_all_matching_iy
                .globl man_entity_set_player_destroyed_flag
                .globl man_entity_setForDestruction
                .globl man_entity_update
                .globl man_entity_reset
                .globl man_entity_init
                .globl man_entity_setForDestruction_iy          
;;=============================================================

nullptr = 0xDEAD

;;=============================================================
;;                          ENTIDADES
;;  Definicion de las posiciones de los campos de las entidades 
;;=============================================================

    e_cmp               =   0   ;;  1 Byte para los bits componente
    e_type              =   1   ;;  1 Byte para el tipo de entidad
    e_posx              =   2   ;;  1 Byte para la posicion x
    e_posy              =   3   ;;  1 Byte para la posicion y
    e_width             =   4   ;;  1 Byte para el ancho de entidad
    e_height            =   5   ;;  1 Byte para la altura entidad
    e_vx                =   6   ;;  1 Byte para la velocidad en eje x
    e_vy                =   7   ;;  1 Byte para la velocidad en eje y
    e_last_x            =   8
    e_last_y            =   9

    e_videoPnter_l      =   10  ;;  2 Bytes para el puntero a la 
    e_videoPnter_h      =   11  ;;  posicion anterior

    e_sprite_ptr_l      =   12  ;;  2 Bytes para el puntero a la 
    e_sprite_ptr_h      =   13  ;;  direccion del sprite

    e_anim_ptr_l        =   14  ;;  2 Bytes para el puntero al 
    e_anim_ptr_h        =   15  ;;  array de animaciones
    e_animation_status  =   16  ;;  1 Byte para el estado de la animacion
    
    e_col_func_l        =   17
    e_col_func_h        =   18

;;--------------------------------------------------------------
;;                  Informacion de entidades
;;--------------------------------------------------------------

size_of_entity      =   e_col_func_h  + 1 ;;  Tamano de una entidad(Bytes)

;;---------------------------------------------------------------
;; Reservamos memoria -> max_entities * size_of_entities
;;---------------------------------------------------------------
.macro CreateEntityArray _N
    
    entities_array_size =   size_of_entity * _N

    ;;  1 Byte para el num de entidades actualmente en el array,empieza en 0
    _entity_number::    .db 0x00   

        ;; 2 Bytes para almacenar la direccion de la ultima entidad valida del array.
    _last_entity_ptr:   .dw _entity_array

    ;;  2 Bytes para almacenar la direccion de la primer entidad libre
    _next_free_entity:  .dw _entity_array
    
    ;;  max_entities * size_of_entities Bytes para el array
    _entity_array::     .ds entities_array_size

    ;; Al final del array se coloca un byte de 0 para indicar el final del array
    _end_array::        .db e_type_invalid
    
.endm

;;---------------------------------------------------------------
;;  Definimos entidades : llamamos a la macro
;;---------------------------------------------------------------
;;  INPUTS -->  NAME    CMPS    TYPE         HP   X  Y  COL   W  H   VX VY VPOINTER   SPRITE_PTR
;;---------------------------------------------------------------

cmps_p = (e_cmps_render | e_cmps_input | e_cmps_physics | e_cmps_collider | e_cmps_collisionable | e_cmp_animation)
cmps_e = (e_cmps_render | e_cmps_physics |  e_cmps_collisionable | e_cmps_collider | e_cmp_animation )
cmps_e2 = (e_cmps_render | e_cmps_physics |  e_cmps_collisionable | e_cmps_collider | e_cmp_animation | e_cmps_ai)
cmps_w = (e_cmps_collisionable)         ;;wall
cmps_spikes = (e_cmps_collisionable)    ;;pinchos
cmps_exit = (e_cmps_collisionable)      ;;salida
cmps_coin = (e_cmps_collisionable)      ;;monedas

type_p = (e_type_player)
type_e = (e_type_instakill)
type_w = (e_type_solid)
type_spikes = (e_type_instakill)
type_exit = (e_type_exit)
type_coin = (e_type_coin)
type_bigcoin = (e_type_bigcoin)
type_ewall = (e_type_ewall)

;;--------------------------------------------------------------
;;                  CAMPO COMPONENTES EN BITS
;;--------------------------------------------------------------

e_cmps_destructible     =    0b00000001  ;; 0x01
e_cmps_render           =    0b00000010  ;; 0x02
e_cmps_input            =    0b00000100  ;; 0x04
e_cmps_ai               =    0b00001000  ;; 0x08
e_cmps_collisionable    =    0b00010000  ;; 0x10
e_cmps_physics          =    0b00100000  ;; 0x20
e_cmps_collider         =    0b01000000  ;; 0x40
e_cmp_animation         =    0b10000000  ;; 0X80    

;;--------------------------------------------------------------
;;                      CAMPO TIPOS DE ENTIDAD
;;              Disponemos de un Byte para conocer el 
;;                tipo de entidad del que se trata
;;--------------------------------------------------------------

e_type_invalid      =   0b00000000     ;; 0x00
e_type_player       =   0b00000001     ;; 0x01
e_type_coin         =   0b00000010     ;; 0x02
e_type_solid        =   0b00000100     ;; 0x04
e_type_instakill    =   0b00001000     ;; 0x08
e_type_bigcoin      =   0b00010000     ;; 0x10
e_type_ewall        =   0b00100000     ;; 0x20
e_type_rainbow      =   0b01000000     ;; 0x40
e_type_exit         =   0b10000000     ;; 0x80
e_type_deleted      =   0xFF

;;--------------------------------------------------------------
;;     ENUMERACION DE POSIBLES ESTADOS DE ANIMACION
;;--------------------------------------------------------------

e_anim_st_standing_up       =   0b00000001      ;; 0X01
e_anim_st_standing_down     =   0b00000010      ;; 0X01
e_anim_st_standing_left     =   0b00000100      ;; 0X01
e_anim_st_standing_right    =   0b00001000      ;; 0X01
e_anim_st_walking_up        =   0b00010000      ;; 0X01
e_anim_st_walking_down      =   0b00100000      ;; 0X01
e_anim_st_walking_left      =   0b01000000      ;; 0X01
e_anim_st_walking_right     =   0b10000000      ;; 0X01

;;---------------------------------------------------------------
;;      AQUI SE DEFINEN LAS MACROS DEL ARRAY DE ENTIDADES
;;---------------------------------------------------------------

.macro  DefineEntity    _name, _e_cpms, _e_type, _col_func, _sprite_ptr, _anim_ptr

    _name::
    
        .db _e_cpms         ;;  ENTITY COMPONENTS
        .db _e_type         ;;  TYPE OF ENTITY
        .db 0               ;;  POSX
        .db 0               ;;  POSY
        .db 0               ;;  WIDTH
        .db 0               ;;  HEIGHT
        .db 0               ;;  VX
        .db 0               ;;  VY
        .db 0               ;;  LAST X
        .db 0               ;;  LAST Y
        .dw nullptr         ;;  LAST VIDEO POINTER
        .dw _sprite_ptr     ;;  SPRITE PTR
        .dw _anim_ptr       ;;  ANIMATION PTR
        .db 0x00            ;;  ANIMATION STATUS 
        .dw _col_func

.endm

.macro CreateEntity _tmpl, _name, _posx, _posy, _width, _height, _vx, _vy

    call man_entity_checkArrayisFull 
    jr z, cannot_create_'_name
    
    can_create_'_name:
     
        ld hl, #_tmpl
        call man_entity_create

        ld e_posx(ix),      #_posx
        ld e_posy(ix),      #_posy
        ld e_width(ix),     #_width
        ld e_height(ix),    #_height
        ld e_vx(ix),        #_vx
        ld e_vy(ix),        #_vy
        ld e_last_x(ix),    #_posx
        ld e_last_y(ix),    #_posy

        cpctm_screenPtr_asm de, CPCT_VMEM_START_ASM, _posx, _posy
        ld e_videoPnter_l(ix), e
        ld e_videoPnter_h(ix), d

    cannot_create_'_name:


.endm