;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; ENTITY MANAGER ;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.include "cpctelera.h.s"
.include "man_entity.h.s"
.include "man_game.h.s"
.include "man_animation.h.s"
.include "../sys/collision.h.s"
.include "../assets/assets.h.s"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;  ===================================
;;  CREAMOS EL ARRAY DE ENTIDADES VACIO
;;  ===================================

        array_max_entities = 40
        CreateEntityArray array_max_entities

;;  ==========================================================================================================================
;;  DEFINIMOS LAS PLANTILLAS DE LAS ENTIDADES DEFAULT
;;  ==========================================================================================================================
;;  INPUTS:     NOMBRE ||   CMPS    ||      TIPO         ||    FUNC_COLLISION ||    SPRITE POINTER  ||  ANIMATION PTR
;;  ==========================================================================================================================

DefineEntity    player,     cmps_p,         type_p,             collision_player,   _sprite_player_00,  player_static_down_anim
DefineEntity    enemy1,     cmps_e,         type_e,             collision_enemy,    _sprite_enemy_2,    enemy1_anim
DefineEntity    enemy2,     cmps_e2,        type_e,             collision_enemy,    _sprite_enemy_4,    enemy2_anim
DefineEntity    wall,       cmps_w,         type_w,             nullptr,            nullptr,            nullptr
DefineEntity    ewall,      cmps_w,         type_ewall,         nullptr,            nullptr,            nullptr
DefineEntity    spikes,     cmps_spikes,    type_spikes,        nullptr,            nullptr,            nullptr         
DefineEntity    exit,       cmps_exit,      type_exit,          nullptr,            _sprite_misc,       nullptr
DefineEntity    coin,       cmps_coin,      type_coin,          nullptr,            nullptr,            nullptr
DefineEntity    bigcoin,    cmps_coin,      type_bigcoin,       nullptr,            nullptr,            nullptr
DefineEntity    rainbow,    cmps_coin,      e_type_rainbow,       nullptr,            nullptr,          nullptr

entity_man_cmps     = e_cmps_destructible
_player_destroyed_flag:: .db 0x00

;;---------------------------------------------------------------
;;  RESETEA EL FLAG DEL PLAYER DESTRUIDO
;;---------------------------------------------------------------

man_entity_init::
    ld a, #0x00
    ld (_player_destroyed_flag), a
ret

;;---------------------------------------------------------------
;;  Create a new entity: reserva espacio en el _entity_array
;;---------------------------------------------------------------
;;  INPUT   :   HL << Pointer a los bytes de inicializacion
;;---------------------------------------------------------------
;;  RETURNS :   IX -> Pointer de la nueva ent, BC -> Size of entity
;;---------------------------------------------------------------
;;  Modifies:   BC, DE , HL, IX
;;---------------------------------------------------------------
man_entity_create::
    
    ld  de, (_next_free_entity)
    ld  b, #0
    ld  c, #size_of_entity

    push bc
    ldir
    pop bc

    ;;actualizar puntero "_next_free_entity"
    ld  hl, (_next_free_entity)

    ;; Ponemos como ultima entidad del array la que acabamos de crear.
    ld (_last_entity_ptr), hl

    ld ix, (_last_entity_ptr) ;; Añade en IX el puntero a la entidad que se acaba de crear.

    add hl, bc
    ld  (_next_free_entity), hl

    ;;incrementamos _entity_number
    ld  hl, #_entity_number
    inc (hl) 

ret

;;------------------------------------------------------------
;;  Sets the entity manager in its initial state
;;------------------------------------------------------------
man_entity_reset::

    ld hl, #_entity_array
    ld a, (_entity_number)
    ld e, a
    
    reset_array_loop:           
    
        ld a, #size_of_entity
    
        reset_array_loop_2:
    
            
            ld c, #0x00
            ld (hl), c
    
            inc hl
            dec a
    
        jr nz, reset_array_loop_2
    
        dec e
    
    jr nz, reset_array_loop
    xor a                       ;;/ SETS ENTITY NUMBER TO "0"
    ld (_entity_number), a      ;;\
    
    ld de, #_entity_array
    
    ld hl, #_last_entity_ptr
    
    ld (hl), e
    inc hl
    
    ld (hl), d
    
    ld hl, #_next_free_entity
    ld (hl), e
    inc hl
    
    ld (hl), d

ret 

;;------------------------------------------------------------
;;  Creates all entities in the game using macros
;;------------------------------------------------------------
man_entity_create_all::
;;------------------TMPL , NAME,  HP ,  X , Y ,  W,  H,  VX, VY, SPRITE_PTR

    call man_game_getActualLevel

    ;; COMPROBACION DE NIVEL A CARGAR.

    ;; =========== NIVEL 1 ===========

    ld c, #1
    
    cp c
    
        jp z, man_entity_load_level_1
    ;; ================================

    ;; =========== NIVEL 2 ===========

    ld c, #2
    
    cp c

        jp z, man_entity_load_level_2

    ;; ================================

    ;; =========== NIVEL 3 ===========

    ld c, #3
    
    cp c

        jp z, man_entity_load_level_3

    ;; ================================
    

    ;; ========== NIVEL 4 =============

    ld c, #4

    cp c
    
        jp z, man_entity_load_level_4



    ;; =================================

    ;; ========== NIVEL 5 =============

    ld c, #5

    cp c
    
        jp z, man_entity_load_level_5

    ;; =================================

    ;; ========== NIVEL 6 =============

    ld c, #6

    cp c
    
        jp z, man_entity_load_level_6

    ;; =================================

        ;; ========== NIVEL 7 =============

    ld c, #7

    cp c
    
        jp z, man_entity_load_level_7

    ;; =================================

        ;; ========== NIVEL 8 =============

    ld c, #8

    cp c
    
        jp z, man_entity_load_level_8

    ;; =================================

        ;; ========== NIVEL 9 =============

    ld c, #9

    cp c
    
        jp z, man_entity_load_level_9

    ;; =================================

ret
man_entity_load_level_1:


        ;; PLAYER
        CreateEntity    player, 1_player,       36,  130,  7,  20, 0,  0

       ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  1_wall_left,     20,  48,   4,  104,  0,  0
        CreateEntity    wall,  1_wall_right,    56,  48,   4,  104,  0,  0

        CreateEntity    wall,  1_wall_up,       24,  40,   80,  8,  0,  0
        CreateEntity    wall,  1_wall_down,     24,  150,  80,  8,  0,  0

        ;; MONEDAS_NIVEL_1

        CreateEntity    bigcoin, 1_coin_1,      32, 72,  4, 8, 0, 0
        CreateEntity    coin, 1_coin_2,         36, 96,  4, 8, 0, 0
        CreateEntity    coin, 1_coin_3,         40, 96,  4, 8, 0, 0
        CreateEntity    coin, 1_coin_4,         44, 128, 4, 8, 0, 0

        ;; SALIDA_NIVEL_1

        CreateEntity    exit, 1_exit_1,         28, 56, 4, 8, 0, 0
ret

man_entity_load_level_2:

        ;; PLAYER
        CreateEntity    player, 2_player,       46,  128,  7,  20, 0,  0

        ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  2_wall_left,     16,  40,   4,  108,  0,  0
        CreateEntity    wall,  2_wall_right,    60,  40,   4,  108,  0,  0

        CreateEntity    wall,  2_wall_up,       20,  32,    80,  8,  0,  0
        CreateEntity    wall,  2_wall_down,     20,  160,   80,  8,  0,  0

        ;; WALLS DEL INTERIOR DEL NIVEL
        CreateEntity wall, 2_wall_middle_1, 40,  79, 16, 24, 0, 0
        CreateEntity wall, 2_wall_middel_2, 20, 136, 20, 24, 0, 0

        ;; MONEDAS_NIVEL_2

        CreateEntity    coin,    2_coin_1,      24, 72,  4, 8, 0, 0
        CreateEntity    bigcoin, 2_coin_2,      56, 40,  4, 8, 0, 0
        CreateEntity    coin,    2_coin_3,      32, 120,  4, 8, 0, 0
        

        ;; SPIKES_NIVEL_2

        CreateEntity    spikes, 2_spikes_1,     44, 64, 4, 8, 0, 0

        ;; SALIDA_NIVEL_2

        CreateEntity    exit, 2_exit_1,         20, 40, 4, 8, 0, 0


ret 

man_entity_load_level_3:

     ;; PLAYER
        CreateEntity    player, 3_player,       20,  40,  7,  20, 0,  0

     ;; ENEMIGOS
        CreateEntity    enemy1, 3_enemy,         48, 112, 7,  18, -1, 0

        ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  3_wall_left,     16,  40,   4,  108,  0,  0
        CreateEntity    wall,  3_wall_right,    60,  40,   4,  108,  0,  0

        CreateEntity    wall,  3_wall_up,       20,  32,    80,  8,  0,  0
        CreateEntity    wall,  3_wall_down,     20,  159,   80,  8,  0,  0

        ;; MUROS INTERIORES

        CreateEntity wall, 3_wall_middle, 44, 79, 16, 25, 0, 0

        ;; MUROS INVISIBLES DE LOS ENEMIGOS
        CreateEntity    ewall,  3_ewall_left,     18,  40,   4,  108,  0,  0
        CreateEntity    ewall,  3_ewall_right,    58,  40,   4,  108,  0,  0

        ;; MONEDAS_NIVEL_3

        CreateEntity    coin,    3_coin_1,      48, 56,  4, 8, 0, 0
        CreateEntity    coin,    3_coin_2,      28, 88,  4, 8, 0, 0
        CreateEntity    bigcoin, 3_coin_3,      28, 136,  4, 8, 0, 0
        CreateEntity    coin,    3_coin_4,      48, 136,  4, 8, 0, 0

        ;; SALIDA_NIVEL_3

        CreateEntity    exit, 3_exit_1, 56, 152, 4, 8, 0, 0

ret


man_entity_load_level_4:

;; PLAYER
        CreateEntity    player, 4_player,       20,  36,  7,  20, 0,  0

     ;; ENEMIGOS
        CreateEntity    enemy2, 4_enemy,         28, 112, 7,  18, 0, -2

        ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  4_wall_left,     12,  32,   4,  124,  0,  0
        CreateEntity    wall,  4_wall_right,    64,  32,   4,  124,  0,  0

        CreateEntity    wall,  4_wall_up,       16,  24,    80,  8,  0,  0
        CreateEntity    wall,  4_wall_down,     16,  167,   80,  8,  0,  0

        ;; MUROS INTERIORES

        CreateEntity wall, 4_wall_middle_1, 16,  55, 24, 33, 0, 0
        CreateEntity wall, 4_wall_middle_2, 16, 111, 12, 32, 0, 0


        ;; MUROS INVISIBLES DE LOS ENEMIGOS
        CreateEntity    ewall,  4_ewall_top,       28,  84,   16,  8,  0,  0
        CreateEntity    ewall,  4_ewall_bottom,    28,  166,  16,  8,  0,  0

        ;; MONEDAS_NIVEL_4

        CreateEntity    coin,    4_coin_1,      36, 40,  4, 8, 0, 0
        CreateEntity    coin,    4_coin_2,      52, 72,  4, 8, 0, 0
        CreateEntity    coin,    4_coin_3,      52, 144,  4, 8, 0, 0
        CreateEntity    bigcoin, 4_coin_4,      20, 96,  4, 8, 0, 0

        ;; SALIDA_NIVEL_4

        CreateEntity    exit, 4_exit_1, 20, 152, 4, 8, 0, 0
ret

man_entity_load_level_5:

    ;; PLAYER
        CreateEntity    player, 5_player,       20,  40,  7,  20, 0,  0

     ;; ENEMIGOS
        CreateEntity    enemy1, 5_enemy,         24, 136, 7,  18, 1, 0

        ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  5_wall_left,     12,  32,   4,  168,  0,  0
        CreateEntity    wall,  5_wall_right,    64,  32,   4,  168,  0,  0

        CreateEntity    wall,  5_wall_up,       16,  16,    80,  8,  0,  0
        CreateEntity    wall,  5_wall_down,     16,  175,   80,  8,  0,  0

        ;; MUROS INTERIORES

        CreateEntity wall, 5_wall_middle_1, 40,  79, 12, 49, 0, 0
        CreateEntity wall, 5_wall_middle_2, 52,  80, 12, 16, 0, 0


        ;; MUROS INVISIBLES DE LOS ENEMIGOS
        CreateEntity    ewall,  5_ewall_right,   62,  136,  4,   24,  0,  0
        CreateEntity    ewall,  5_ewall_left,    14,  136,  4,   24,  0,  0

        ;; SPIKES_NIVEL_5
        CreateEntity    spikes, 5_spikes_1,        40, 24, 4, 8, 0, 0
        CreateEntity    spikes, 5_spikes_2,        60, 24, 4, 8, 0, 0
        CreateEntity    spikes, 5_spikes_3,        40, 72, 4, 8, 0, 0
        CreateEntity    spikes, 5_spikes_4,        60, 72, 4, 8, 0, 0

        CreateEntity    spikes, 5_spikes_5,        20, 160, 4, 8, 0, 0

        ;; MONEDAS_NIVEL_5
        CreateEntity    coin,    5_coin_1,      48, 48,  4, 8, 0, 0
        CreateEntity    coin,    5_coin_2,      52, 48,  4, 8, 0, 0
        CreateEntity    coin,    5_coin_3,      24, 96,  4, 8, 0, 0
        CreateEntity    coin,    5_coin_4,      36, 96,  4, 8, 0, 0
        CreateEntity    coin,    5_coin_5,      40, 160,  4, 8, 0, 0
        CreateEntity    bigcoin, 5_coin_6,      56, 104, 4, 8, 0, 0


        ;; SALIDA_NIVEL_5

        CreateEntity    exit, 5_exit_1, 60, 160, 4, 8, 0, 0

ret

man_entity_load_level_6:

    ;; PLAYER
    CreateEntity    player, 6_player,       20,  24, 7,  20, 0,  0

    ;; ENEMIGOS
    CreateEntity    enemy2, 6_enemy_1,       20, 80, 7,  18, 0, -2
    CreateEntity    enemy2, 6_enemy_2,       40, 40, 7,  18, 0, -2

    ;; WALLS DEL BORDE DE LA PANTALLA
    CreateEntity    wall,  6_wall_left,     8,   32,   4,  168,  0,  0
    CreateEntity    wall,  6_wall_right,    68,  32,   4,  168,  0,  0

    CreateEntity    wall,  6_wall_up,       16,  16,    80,  8,  0,  0
    CreateEntity    wall,  6_wall_down,     16,  175,   80,  8,  0,  0

    ;; MUROS INTERIORES

    CreateEntity wall, 6_wall_middle_1, 16,  47, 20, 17, 0, 0
    CreateEntity wall, 6_wall_middle_2, 48,  47, 20, 17, 0, 0
    CreateEntity wall, 6_wall_middle_3, 48,  95, 16, 17, 0, 0
    CreateEntity wall, 6_wall_middle_4, 48,  96, 8, 55, 0, 0


    ;; MUROS INVISIBLES DE LOS ENEMIGOS
    CreateEntity    ewall,  6_ewall_e1_up,   20,  60,  12,   8,  0,  0
    CreateEntity    ewall,  6_ewall_e1_down, 20,  173,  4,   8,  0,  0

    CreateEntity    ewall,  6_ewall_e2_up,   40,  18,  4,   8,  0,  0
    CreateEntity    ewall,  6_ewall_e2_down, 40,  173,  4,   8,  0,  0

    ;; MONEDAS_NIVEL_6
    CreateEntity    coin,       6_coin_1,      60, 32,  4, 8, 0, 0
    CreateEntity    bigcoin,    6_coin_2,      60, 72,  4, 8, 0, 0
    CreateEntity    coin,       6_coin_3,      28, 112,  4, 8, 0, 0
    CreateEntity    bigcoin,    6_coin_4,      12, 112,  4, 8, 0, 0
    CreateEntity    coin,       6_coin_5,      60, 136,  4, 8, 0, 0

    ;; SALIDA_NIVEL_6

    CreateEntity    exit, 6_exit_1, 60, 112, 4, 8, 0, 0


ret 

man_entity_load_level_7:

    ;; PLAYER NIVEL 7
    CreateEntity player,        7_player,     36, 160, 7,  20, 0, 0

    ;; ENEMIGOS NIVEL 7
    CreateEntity enemy1,         7_enemy_1,    12, 128, 7,  18, 1, 0
    CreateEntity enemy1,         7_enemy_2,    24,  48, 7,  18, 1, 0

    ;; MUROS DE LOS ENEMIGOS DEL NIVEL 7
    CreateEntity ewall,         7_ewall_e1_left,   5, 120, 4, 24, 0, 0
    CreateEntity ewall,         7_ewall_e1_right, 71, 120, 4, 24, 0, 0

    CreateEntity ewall,         7_ewall_e2_left,   5,  40, 4, 24, 0, 0
    CreateEntity ewall,         7_ewall_e2_right, 71,  40, 4, 24, 0, 0

    ;; MUROS DEL NIVEL 7
    CreateEntity    wall,  7_wall_left,      4,   16,   4,  160,  0,  0
    CreateEntity    wall,  7_wall_right,     72,  16,   4,  160,  0,  0

    CreateEntity    wall,  7_wall_up,        8,    8,   80,  8,  0,  0
    CreateEntity    wall,  7_wall_down,      8,  183,   80,  8,  0,  0

    ;; MUROS INTERIORES DEL NIVEL 7
    CreateEntity    wall,  7_wall_middle_1,  8, 144, 24, 40, 0, 0
    CreateEntity    wall,  7_wall_middle_2, 48, 144, 24, 40, 0, 0
    CreateEntity    wall,  7_wall_middle_3, 36,  87,  8, 33, 0, 0
    CreateEntity    wall,  7_wall_middle_4,  8,  72, 28, 16, 0, 0
    CreateEntity    wall,  7_wall_middle_5, 60,  63, 12, 17, 0, 0
    CreateEntity    wall,  7_wall_middle_6, 24,  16,  8, 24, 0, 0
    CreateEntity    wall,  7_wall_middle_7, 44,  16,  8, 24, 0, 0

    ;; PINCHOS DEL NIVEL 7
    CreateEntity    spikes, 7_spikes_1,     20, 111, 4, 9, 0, 0
    CreateEntity    spikes, 7_spikes_2,      8,  65, 4, 9, 0, 0
    CreateEntity    spikes, 7_spikes_3,     68,  16, 4, 8, 0, 0


    ;; MONEDAS DEL NIVEL 7
    CreateEntity    bigcoin, 7_coin_1,   12,  96, 4, 8, 0, 0
    CreateEntity    coin,    7_coin_2,   56, 104, 4, 8, 0, 0
    CreateEntity    bigcoin, 7_coin_3,   60,  24, 4, 8, 0, 0
    CreateEntity    coin,    7_coin_4,   12,  24, 4, 8, 0, 0

    ;; SALIDA DEL NIVEL 7
    CreateEntity    exit, 7_exit, 36, 24, 4, 8, 0, 0

ret 

man_entity_load_level_8:

           ;; PLAYER
        CreateEntity    player, 8_player,  12,  16,  7,  20, 0,  0

        ;; ENEMIES
        CreateEntity    enemy1, 8_enemy1, 20,  96,    7,  18, 1,  0  
        CreateEntity    enemy2, 8_enemy2, 69, 112,   7,  18,  0, -2
        
        ;; MUROS DE LOS ENEMIGOS
        CreateEntity    ewall, 8_ewall_e1_left,   1, 96, 4, 32, 0, 0
        CreateEntity    ewall, 8_ewall_e1_right, 51, 96, 4, 32, 0, 0

        CreateEntity    ewall, 8_ewall_e2_up,   64,  58, 12,  8, 0, 0
        CreateEntity    ewall, 8_ewall_e2_down, 64, 190, 12,  8, 0, 0

        ;;SPIKES
        CreateEntity    spikes, 8_spike1,  20,  44,  4,    8,  0,  0   ;;SPIKE DE ARRIBA
        CreateEntity    spikes, 8_spike2,   8, 152,  4,    8,  0,  0 
        CreateEntity    spikes, 8_spike3,  24, 176,  4,    8,  0,  0  

        ;; SALIDA DEL NIVEL
        CreateEntity    exit, 8_exit, 72, 8, 4, 8, 0, 0

    ;; WALLS COLISIONES

        ;; WALLS DEL BORDE DE LA PANTALLA
        CreateEntity    wall,  8_wall_left,   0,  0,   4,  199,  0,  0 
        CreateEntity    wall,  8_wall_right,  76,   0,   4,  199,  0,  0
        CreateEntity    wall,  8_wall_up,     0,  0,     80,  8,  0,  0
        CreateEntity    wall,  8_wall_down,   0,  192,   80,  8,  0,  0

        ;; WALLS DE DENTRO DEL NIVEL

        CreateEntity    wall,  8_wall_middle_1, 32,  16,  8,  64, 0, 0     ;; LA DE ARRIBA VERTICAL LARGA
        CreateEntity    wall,  8_wall_middle_2, 16,  64, 16,  16, 0, 0     ;; LA HORIZONTAL QUE ESTA DEBAJO DE LA ANTERIOR
        CreateEntity    wall,  8_wall_middle_3,  4, 127, 20,  17, 0, 0     ;; LA DE ABAJO MAS A LA IZQUIERDA
        CreateEntity    wall,  8_wall_middle_4, 32, 127,  8,  64, 0, 0     ;; LA QUE ESTA AL LADO DE LA ANTERIOR VERTICAL
        CreateEntity    wall,  8_wall_middle_5, 52,  55, 24,   9, 0, 0
        CreateEntity    wall,  8_wall_middle_6, 52,  55,  4, 104, 0, 0
        CreateEntity    wall,  8_wall_middle_7, 52,  96, 12,  16, 0, 0
        CreateEntity    wall,  8_wall_middle_8, 52, 144, 12,  16, 0, 0

        CreateEntity    wall,  8_wall_middle_9, 52,  16,  8,  8, 0, 0      ;; LA VERTICAL CERCA DE LA SALIDA

    ;; MONEDAS

        CreateEntity coin,      8_coin_1,          24,  20,  4, 8, 0, 0
        CreateEntity bigcoin,   8_coin_2,           8, 176,  4, 8, 0, 0
        CreateEntity coin,      8_coin_3,          44, 160,  4, 8, 0, 0
        CreateEntity rainbow,   8_coin_4,          60,  72,  8, 8, 0, 0     ;;arcoiris
        CreateEntity coin,      8_coin_5,          44,  16,  4, 8, 0, 0

ret 

man_entity_load_level_9:

    ;; PLAYER NIVEL 9
    CreateEntity   player, 9_player, 4, 170, 7,  20, 0, 0

    ;; ENEMIGOS NIVEL 9
    CreateEntity   enemy2,  9_enemy_1, 23, 24, 7,  18, 0, -2
    CreateEntity   enemy2,  9_enemy_2, 51, 24, 7,  18, 0, -2

    ;; MUROS DE LOS ENEMIGOS DEL NIVEL 9
    CreateEntity   ewall,  9_ewall_e1_up,   20, 1,   12, 8, 0, 0
    CreateEntity   ewall,  9_ewall_e1_down, 20, 121, 12, 8, 0, 0
    CreateEntity   ewall,  9_ewall_e2_up,   48, 1,   12, 8, 0, 0
    CreateEntity   ewall,  9_ewall_e2_down, 48, 121, 12, 8, 0, 0

    ;; MUROS NIVEL 9
    CreateEntity   wall,   9_wall_up, 4, 0, 72, 8, 0, 0
    CreateEntity   wall,   9_wall_down, 4, 191, 72, 8, 0, 0
    CreateEntity   wall,   9_wall_left, 0, 8, 4, 184, 0, 0
    CreateEntity   wall,   9_wall_right, 76, 8, 4, 184, 0, 0

    ;; MUROS INTERIORES DEL NIVEL 9
    CreateEntity   wall,   9_wall_middle_1, 4, 160, 20, 8, 0 ,0 

    ;; PINCHOS DEL NIVEL 9
    CreateEntity   spikes, 9_spikes_1, 24, 160, 40,  8,  0,  0
    CreateEntity   spikes, 9_spikes_2, 16,  88,  4, 48,  0,  0
    CreateEntity   spikes, 9_spikes_3, 20, 127, 40,  9,  0,  0
    CreateEntity   spikes, 9_spikes_4, 60,  88, 4,  48,  0,  0
    CreateEntity   spikes, 9_spikes_5, 64,  88, 12,  8,  0,  0
    CreateEntity   spikes, 9_spikes_6, 32,  87, 16, 17,  0,  0
    CreateEntity   spikes, 9_spikes_7,  4,  31, 16, 33,  0,  0
    CreateEntity   spikes, 9_spikes_8, 32,   8, 16, 48,  0,  0
    CreateEntity   spikes, 9_spikes_9, 60,   8,  4, 16,  0,  0
    CreateEntity   spikes, 9_spikes_10,60,  56, 16,  8,  0,  0 


    ;; MONEDAS DEL NIVEL 9
    CreateEntity   coin,   9_coin_1,   68, 104, 4, 8, 0, 0
    CreateEntity   bigcoin,9_coin_2,    8,  16, 4, 8, 0, 0


    ;; SALIDA DEL NIVEL 9
    CreateEntity   exit,   9_exit,     68,  16,  4,  8,  0,  0

ret 



;; [A] >> FLAG DEL CHECKEO DE LA MUERTE DEL PLAYER
man_entity_getPlayerDeadFlag::

   ld a, (_player_destroyed_flag)
ret

;;[Z] >> 1 COMPRUEBA SI EL ARRAY DE ENTIDADES ESTA LLENO. (ACTIVA EL FLAG Z)
;;  REGISTROS DESTRUIDOS: A, HL
man_entity_checkArrayisFull:

    ld a, #entities_array_size
    ld hl, #_entity_number

    sub (hl)
ret

;; [HL] << PUNTERO A LA FUNCION QUE SE LLAMARA PARA CADA ENTIDAD.
;; [DE] << MASCARA
man_entity_for_all_matching::

    ld (_funcion), hl       ;; METEMOS LA FUNCION PASADA POR PARAMETRO EN LA ETIQUETA _FUNCION
    ld (_mascara), de       ;; METEMOS LA MASCARA PASADA POR PARAMETRO EN LA ETIQUETA _MASCARA
    ld ix, #_entity_array   ;; [IX] APUNTA A LA PRIMERA ENTIDAD DEL ARRAY

    ;; INICIO BUCLE FOR ALL
    entity_for_all_loop:

        ;; COMPRUEBA SI HE RECORRIDO TODAS LAS ENTIDADES Y SE SALE DE LA FUNCION CUANDO LAS RECORRO TODAS
        ld a,   e_type(ix)
        ld c,  #e_type_invalid

        cp c
        jr z, entity_for_all_end_loop
        ;; ============================================

        ;; SE REALIZA LA OPERACION AND CON LA MASCARA PARA VER SI LA ENTIDAD CUMPLE LOS REQUISITOS

            _mascara = . + 1
            ld bc, #(_mascara)

            ld a, c
            and e_cmp(ix)

            ld hl, #_mascara
            cp (hl)

        ;; ===========================================

        ;; SI SE CUMPLE LA OPERACION ANTERIOR SE ACTIVA EL FLAG Z. SI SE ACTIVA EL FLAG Z SE LLAMA A LA
        ;; FUNCION PASADA POR PARAMETRO

            _funcion = . + 1
            call z, (_funcion)

        ;; ===========================================

        ;; AL REGRESAR DE LA FUNCION SE LE SUMA A [IX] EL TAMAÑO DE LA ENTIDAD PARA PASAR A LA SIGUIENTE
        ;; QUE DEBE COMPROBAR.

            ld de, #size_of_entity
            add ix, de

        ;; ===========================================
         
    jr  entity_for_all_loop

    entity_for_all_end_loop:

ret

man_entity_for_all_matching_iy::

    ld (_funcion_iy), hl       ;; METEMOS LA FUNCION PASADA POR PARAMETRO EN LA ETIQUETA _FUNCION_iy
    ld (_mascara_iy), de       ;; METEMOS LA MASCARA PASADA POR PARAMETRO EN LA ETIQUETA _MASCARA_iy
    ld iy, #_entity_array   ;; [iy] APUNTA A LA PRIMERA ENTIDAD DEL ARRAY

    ;; INICIO BUCLE FOR ALL
    entity_for_all_loop_iy:

        ;; COMPRUEBA SI HE RECORRIDO TODAS LAS ENTIDADES Y SE SALE DE LA FUNCION CUANDO LAS RECORRO TODAS
        ld a,   e_type(iy)
        ld c,  #e_type_invalid

        cp c
        jr z, entity_for_all_end_loop_iy
        ;; ============================================

         ;; COMPRUEBA QUE EL COLLISIONABLE Y EL COLLIDER NO SEA LA MISMA ENTIDAD. SI LO ES VUELVE.

        ld__hl_iy
        ld__de_ix
        xor a
        sbc hl, de
        
        jr z, go_to_next_entity_iy

        ;; =========================================


        ;; COMPRUEBA QUE SI EL COLLIDER ES UN ENEMIGO, SOLO COMPRUEBE COLISIONES CON SUS MUROS.
        
        ld a, e_type(ix)
        and #e_type_instakill

            jr z, valid_iy

        ld a, e_type(iy)
        and #e_type_ewall

            jr nz, valid_iy

        jr go_to_next_entity_iy

        ;; =============================================

        valid_iy:

        ;; SE REALIZA LA OPERACION AND CON LA MASCARA PARA VER SI LA ENTIDAD CUMPLE LOS REQUISITOS

            _mascara_iy = . + 1
            ld bc, #(_mascara_iy)

            ld a, c
            and e_cmp(iy)

            ld hl, #_mascara_iy
            cp (hl)

        ;; ===========================================

        ;; SI SE CUMPLE LA OPERACION ANTERIOR SE ACTIVA EL FLAG Z. SI SE ACTIVA EL FLAG Z SE LLAMA A LA
        ;; FUNCION PASADA POR PARAMETRO

            _funcion_iy = . + 1
            call z, (_funcion_iy)

        ;; ===========================================

        go_to_next_entity_iy:

        ;; AL REGRESAR DE LA FUNCION SE LE SUMA A [iy] EL TAMAÑO DE LA ENTIDAD PARA PASAR A LA SIGUIENTE
        ;; QUE DEBE COMPROBAR.

            ld de, #size_of_entity
            add iy, de

        ;; ===========================================
         
    jr  entity_for_all_loop_iy

    entity_for_all_end_loop_iy:

ret

man_entity_set_player_destroyed_flag::

    ld hl, #_player_destroyed_flag
    inc (hl)

    call man_game_activateEndFlag
    
ret


;;[IX] << PUNTERO A LA ENTIDAD A MARCAR PARA DESTRUIR. (AÑADE EL COMPONENTE DESTRUCTIBLE A LA ENTIDAD)
man_entity_setForDestruction::

    ld a, e_type(ix)
    and #e_type_player

        call nz, man_entity_set_player_destroyed_flag

    ld a, e_cmp(ix)
    ld c, #e_cmps_destructible

    or c

    ld e_cmp(ix), a

ret

;;[IY] << PUNTERO A LA ENTIDAD A MARCAR PARA DESTRUIR. (AÑADE EL COMPONENTE DESTRUCTIBLE A LA ENTIDAD)
man_entity_setForDestruction_iy::

    ld a, e_type(iy)
    and #e_type_player

        call nz, man_entity_set_player_destroyed_flag

    ld a, e_cmp(iy)
    ld c, #e_cmps_destructible

    or c

    ld e_cmp(iy), a

ret

;;[IX] << PUNTERO A LA ENTIDAD A DESTRUIR.
man_entity_destroyEntity::

;; COMPROBACION DE QUE IX NO APUNTA A LA ULTIMA ENTIDAD.

    ld hl, (_last_entity_ptr)   ;; HL APUNTA A LA ULTIMA ENTIDAD.  
    ld__bc_ix

    or a
    sbc hl, bc

    jr z, desplazamiento_punteros  ;; SI LA ENTIDAD A BORRAR ES LA ULTIMA, NO HACE FALTA COPIAR

;; ==========================================

;; COPIA DE ENTIDAD

    ld hl, (_last_entity_ptr)   ;; HL APUNTA A LA ULTIMA ENTIDAD.
    ld__de_ix               ;; HACEMOS QUE DE APUNTE A LA ENTIDAD A ELIMINAR.
    ld bc, #size_of_entity  ;; METEMOS EN BC EL TAMAÑO DEL TIPO ENTIDAD.

        ldir 
            
;; ==========================

    desplazamiento_punteros:

        ld hl, (_last_entity_ptr)
        ld (_next_free_entity), hl  ;; PREPARAMOS EL PUNTERO NEXT PARA QUE APUNTE A LA ULTIMA ENTIDAD 

        inc hl
        ld (hl), #e_type_invalid

        ld hl, (_last_entity_ptr)

        ld bc, #size_of_entity

        or a                            ;; RESET CARRY FLAG ESTO ES PARA ASEGURARNOS QUE LA RESTA SE HACE BIEN.
        sbc hl, bc                      ;; RESTAMOS BC A HL PARA HACER QUE APUNTE A LA QUE SERA LA NUEVA ULTIMA ENTIDAD.
        ld (_last_entity_ptr), hl       ;; GUARDAMOS LO ANTERIOR EN MEMORIA.

        ld hl, #_entity_number
        dec (hl)

ret


man_entity_update::

    ld de, #entity_man_cmps
    ld hl, #man_entity_destroyEntity

    call man_entity_for_all_matching

ret