.include "sys/input_system.h.s"
.include "sys/physics_system.h.s"
.include "sys/render_system.h.s"
.include "entity_manager.h.s"
.include "assets/assets.h.s"
.include "cpctelera_functions.h.s"
.include "sys/collision.h.s"
.include "cpctelera.h.s"

.globl _entity_array
.globl _num_entities
.globl _player_lives

bytes_array = max_entities*sizeof_e

; Puntuacion almacenada digito a digito
score:: .dw 0x0000

DefineEntity player, 35, 0xA0, 8, 16, 1, 1, __sprite_fighter, e_cmp_default, e_ai_st_noAI
DefineEntity enemy2, 27, 35, 4, 8, 1, 1, __sprite_asteroid, e_cmp_default, e_ai_st_stand_by
DefineEntity enemy3, 27, 80, 4, 8, 1, 1, __sprite_asteroid, e_cmp_default, e_ai_st_stand_by
DefineEntity enemy4, 55, 40, 4, 8, 1, 1, __sprite_asteroid, e_cmp_default, e_ai_st_stand_by
DefineEntity enemy5, 12, 40, 4, 8, 1, 1, __sprite_asteroid, e_cmp_default, e_ai_st_stand_by
DefineEntity enemy6, 42, 38, 4, 8, 1, 1, __sprite_asteroid, e_cmp_default, e_ai_st_stand_by
DefineEntity vida, 70, 17, 4, 6, 0, 0, __sprite_corazon, e_cmp_alive|e_cmp_render, e_ai_st_noAI

game_manager_playGame::

    loop:
            call   game_manager_update
            
            call    cpct_waitVSYNC_asm
            jr    loop

game_manager_init_systems::
    ld a,#3
    ld (_player_lives),a
    call    render_system_init
    call    input_system_init
    call    entity_manager_getEntityArray
    jp      physics_system_init

game_manager_init_entities::

    ;; Player
    ld       hl, #player
    call     entity_manager_create

    ; Vidas
    ld       hl, #vida
    call     entity_manager_create

    ; Enemigos
    ld       hl, #enemy2
    call     entity_manager_create
    ld       hl, #enemy3
    call     entity_manager_create
    ld       hl, #enemy4
    call     entity_manager_create
    ld       hl, #enemy5
    jp     entity_manager_create


game_manager_update::
    ;;  Dibujar entidades
    ;cpctm_setBorder_asm HW_RED
    ld      hl, #render_system_update
    ld      b, #e_cmp_render
    call    entity_manager_forall_matching
    ;cpctm_setBorder_asm HW_WHITE

    ;;  Input del jugador
    call     entity_manager_getEntityArray  
    call     input_system_update
    
    ;; Sistema de physics
    ld      hl, #physics_system_update
    ld      b, #e_cmp_physics
    call    entity_manager_forall_matching

    ; Sumar puntuacion en formato BCD
    ld hl,(score)
    ld a, l
    add #1
    daa
    ld l,a
    ld a,h
    adc #0
    daa
    ld h,a
    ld (score),hl

    ;; Sistema de colisiones con player
    ld hl,  #_entity_array
    jp    sys_collision_update


game_manager_endGame::
    jp    render_system_end


;; Establecemos la posición de inicio del jugador
game_manager_setPlayerPosition::

    ld  hl, #0xA023
    ld  e_x(ix), l
    ld  e_y(ix), h
    ret

;; Ponemos la posición de los asteroides
game_manager_level2::
    ;;27, 20
    ;;27, 80
    ;;55, 40
    ;;12, 40
    ld a,#3
    ld (_player_lives),a
    ld de,#0x000
    ld (score),de
    push ix
    call render_system_numlives
    pop ix

    ;;Aumentamos ix para que apunte al primer asteroide
    ld      de, #sizeof_e
    add     ix, de
    add     ix, de

    ;;ld      ix, #enemy2
    ;;ld      hl, #0x2720
    ;;ld  e_x(ix), l
    ;;ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;ld      ix, #enemy3;;;
    ld      hl, #0x2733
    ld  e_x(ix), l
    ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;ld      ix, #enemy4
    ld      hl, #0x5540
    ld  e_x(ix), l
    ld  e_y(ix), h

    ld      de, #sizeof_e
    add     ix, de

    ;;ld      ix, #enemy5
    ld      hl, #0x2440
    ld  e_x(ix), l
    ld  e_y(ix), h

    ret

;; Establecemos la posición inicial de los asteroides
;game_manager_setEntitiesPosition::
    ;ld b, a
    ;inc b
;
    ;_loop_update:
    ;    ; Comprueba num entidades
    ;    dec b
    ;    ret z
;
;
    ;    call    cpct_getRandom_glfsr16_u16_asm
    ;    ;;call    entity_manager_getEntityArray
    ;    ld  e_x(ix), l
    ;    ld  e_y(ix), h
    ;    
;
    ;    _next_e:
    ;        ld      de, #sizeof_e
    ;        add     ix, de
    ;        jr      _loop_update

    ;ret
