;; GAME MANAGER
.include "entities.h.s"
.include "rounds.h.s"
.include "./entities/bullet.h.s"
.include "./system/physics.h.s"
.include "./system/input.h.s"
.include "./system/render.h.s"
.include "cpctelera.h.s"
.include "./system/cpct_globals.h.s"
.include "./system/ai_control.h.s"
.include "./system/collision.h.s"
.include "./system/collision_sys.h.s"
.include "screen.h.s"
.include "./system/ui.h.s"
.include "music.h.s"

.globl cpct_scanKeyboard_if_asm
.globl _sprite_char_D1
.globl spawn_enemy1

cmps == e_cmps_position | e_cmps_alive | e_cmps_render | e_cmps_physics | e_cmps_input

;; Structure of templates:
;;  cmps - x - y - vx - vy - w - h - color - e_type - sprite_ptr - prevpos
;mainchar_entity: .db cmps , #0, #0, #0, #0, #3, #12,#0x0F, #e_type_mainchar, #0, #0, #e_ai_st_noAI
;.dw #0x0000, #0x0000
               ;.ds sizeof_e-1
mainchar_entity: .db cmps
.ds sizeof_e-1

restart_game:: .db #0
end_game:: .db #0


pos_enemies: .db #0

build_player:
   ld ix, #mainchar_entity
   ld e_cmps(ix), #cmps
   ld e_x(ix), #20
   ld e_y(ix), #140
   ld e_prv_ptr(ix), #0x64
   ld e_prv_ptr+1(ix), #0xE5
   ld e_vx(ix), #0
   ld e_vy(ix), #0
   ld e_w(ix), #4
   ld e_h(ix), #18
   ld e_type(ix), #e_type_mainchar
   ld e_lifes(ix), #6
   ld e_playerctr(ix), #1
   ld e_player_moving(ix), #0
   ld e_player_anim_ctr(ix), #0   
   ld e_player_current_anim(ix), #0
   ;;HAY QUE PONERLO EN 1 CUANDO SE MUEREN TODOS LOS ENEMIGOS ENNIO

   ld hl, #_sprite_char_D1
   ld (#mainchar_entity+e_sprite), hl

   ld hl, #mainchar_entity 
   call man_entity_create

ret

create_enemies:
   ld b, #1
   bucle:
   ld a, (pos_enemies)
   
   ld h,#10;;coordenada x
   ld l,a;;coordenada y
   push af
   push bc
   call spawn_enemy1
   pop bc
   pop af
   add a,#30
   ld (pos_enemies),a

   dec b
   jr nz, bucle
ret

player_restart::
   ld a, #0
   call man_entity_get_from_idx

   ld e_cmps(ix), #cmps
   ld e_x(ix), #20
   ld e_y(ix), #140
   ld e_prv_ptr(ix), #0x64
   ld e_prv_ptr+1(ix), #0xE5
   ld e_vx(ix), #0
   ld e_vy(ix), #0
   ld e_w(ix), #4
   ld e_h(ix), #18
   ld e_type(ix), #e_type_mainchar
   ld e_lifes(ix), #6
   ld e_playerctr(ix), #1
   ;;HAY QUE PONERLO EN 1 CUANDO SE MUEREN TODOS LOS ENEMIGOS ENNIO

   ld hl, #_sprite_char_D1
   ld (#mainchar_entity+e_sprite), hl

   call restart_shoot_dir
ret

game_man_init::
   cpctm_setBorder_asm HW_BLACK
   call sys_render_init_palette
   call set_int_handler
   ld de, #_sfx
   call cpct_akp_SFXInit_asm
   halt
   halt
   halt

   title_screen::

   ld de, #_silence_song
   call play_song
   call screen_man_story_screen
   ld de, #_main_song
   call play_song
   call screen_man_title_screen
   call man_entity_init
   call sys_collision_control_init
   call build_player

   restart_init::
   call screen_man_first_lvl_screen 
   call init_round_1
   call sys_render_init 
   call sys_ui_init_score
   call sys_ui_init_rounds ;;PONERLO SIEMPRE
   call sys_ui_init
   call sys_ui_init_food_life
   ld a, #0
   ld (restart_game), a
   ld a, #0
   ld (end_game), a
ret

restart_game_func::
    ld a, #1
    ld (restart_game), a
ret

end_game_func::
    ld a, #1
    ld (end_game), a
ret

game_man_update::
   call sys_render_update
   call man_entity_update
   call sys_input_update
   call sys_ai_control_update

   cpctm_setBorder_asm HW_BLACK
   call sys_collision_update
   call sys_ai_control_update
   call sys_physics_update
   call sys_bullet_update
   call update_death_anim
   call update_rounds
   ld a, (restart_game)
   and a
   call nz, screen_man_death_screen

   ld a, (end_game)
   and a
   call nz, screen_man_end_screen
   
   wait_for_render_signal:
   ld a, (should_render)
   or a
   jr z, wait_for_render_signal
   xor a
   ld (should_render), a
ret

should_render: .db #0

int_handler1:;interrupciones
   push af
   push bc
   push de
   push hl
   ex af', af
   exx
   push af
   push bc
   push de
   push hl
   
   call cpct_akp_musicPlay_asm ;MUSICA
   call cpct_scanKeyboard_if_asm
   ld hl, #0x0039
   ld (hl), #<int_handler2
   inc hl
   ld (hl), #>int_handler2

   ;; PUSH Y POP DE TODOS LOS REGISTROS PORQUE SE LOS CARGA
   pop hl
   pop de
   pop bc
   pop af
   ex af', af
   exx
   pop hl
   pop de
   pop bc
   pop af
   ei
reti

int_handler2:;interrupciones
   push hl
   ld hl, #0x0039
   ld (hl), #<int_handler3
   inc hl
   ld (hl), #>int_handler3
   pop hl
   ei
reti

int_handler3:;interrupciones
   push hl
   ld hl, #0x0039
   ld (hl), #<int_handler4
   inc hl
   ld (hl), #>int_handler4
   pop hl
   ei
reti

int_handler4:;interrupciones
   push hl
   ld hl, #0x0039
   ld (hl), #<int_handler5
   inc hl
   ld (hl), #>int_handler5
   pop hl
   ei
reti
int_handler5:;interrupciones
   push hl
   ld hl, #0x0039
   ld (hl), #<int_handler6
   inc hl
   ld (hl), #>int_handler6
   ld hl, #should_render
   ld (hl), #1
   pop hl
   ei
reti

int_handler6:;interrupciones
   push hl
   call set_render_lower
   ld hl, #0x0039
   ld (hl), #<int_handler1
   inc hl
   ld (hl), #>int_handler1
   pop hl
   ei
reti

set_int_handler:
   call cpct_waitVSYNC_asm 
   halt
   halt
   call cpct_waitVSYNC_asm
   ld hl, #0x0038
   ld (hl), #0xC3
   inc hl
   ld (hl), #<int_handler1
   inc hl
   ld (hl), #>int_handler1
   inc hl
   ld (hl), #0xC9
ret
