.include "./manager/entities.h.s"
.include "./manager/rounds.h.s"

.globl cpct_getRandom_lcg_u8_asm


;; ----------------------------------
;; SPRITES
;;
;;
.globl _sprite_e1_1
.globl _sprite_e2_1
.globl _sprite_calabaza
.globl _sprite_calabaza_woke
.globl _sprite_murcielago
.globl _sprite_ovni
.globl _sprite_zombie_arcoiris 
.globl _sprite_arana 
.globl _sprite_esqueleto
.globl _sprite_burger

;; ----------------------------------
;; COMPONENTES
;;
;;
en_cmps == e_cmps_position | e_cmps_alive | e_cmps_physics | e_cmps_render

;; ENTIDAD DE ENEMIGO
enemy_entity: .db en_cmps
.ds sizeof_e-1

;-------
spwn1_x =  39
spwn1_y =  30 ;arriba
spwn1_vx = 0
spwn1_vy = 1

spwn2_x =  39
spwn2_y =  177 ;abajo
spwn2_vx = 0
spwn2_vy = -1

spwn3_x =  2
spwn3_y =  60 ;izq arr
spwn3_vx = 1
spwn3_vy = 0

spwn4_x =  74
spwn4_y =  60 ;der arr
spwn4_vx = -1
spwn4_vy = 0

spwn5_x =  2
spwn5_y =  140 ;izq abaj
spwn5_vx = 1
spwn5_vy = 0

spwn6_x =  74
spwn6_y =  140 ;der abaj
spwn6_vx = -1
spwn6_vy = 0
;--------


spawn_enemy2::
   call spawn_enemy1
   ld hl, #is_double_spawn
   ld (hl), #1
   call spawn_enemy1
ret

is_double_spawn: .db #0
last_door: .db #0
last_door_tries: .db #20 ;;5 intentos para cambiar de puerta (para evitar bucle infinito)
spawn_enemy1::
   ld ix, #enemy_entity

   ld e_prv_ptr(ix), #0x64
   ld e_prv_ptr+1(ix), #0xE5

   ld hl, #last_door_tries
   ld (hl), #20

   ;----
   ld l, #0
   call cpct_getRandom_lcg_u8_asm
   push hl
   ld a,l
   and #7
   select_door:
   call get_current_doors

   bucle_spawn_select:
   inc hl
   inc (hl)
   dec (hl)
   call z, get_current_doors
   dec a
   jr nz, bucle_spawn_select

   ld a, (hl) ;se mete en a la puerta actual

   ;se debe comprobar que si es doublespawn y a es igual a last_door, se salte a select_door

      ld hl, #is_double_spawn
      inc (hl)
      dec (hl)
      jr z, no_es_double_spawn
      ;;si es doble spawn
      ld hl, #last_door
      cp (hl)
      jr nz, no_es_double_spawn ;;si la puerta es distinta a last door, se continua
      ;;si es la misma puerta:
      ld hl, #last_door_tries
      inc (hl)
      dec (hl)
      jr z, no_es_double_spawn ;;si el numero de intentos restantes es 0, se continua
      dec (hl) ;;Se disminuye el numero de intentos

      call cpct_getRandom_lcg_u8_asm
      ld a,l
      and #7
      jr select_door

   no_es_double_spawn:
   ld (last_door), a ;se guarda cual fue la ultima puerta

   cp #1
   jp z, es_0 ;biem

   cp #2
   jp z, es_3

   cp #3
   jp z, es_5

   cp #4
   jp z, es_1 ;bien

   cp #5
   jp z, es_4

   cp #6
   jp z, es_2 ;bien

   es_0:
   ld e_x(ix), #spwn1_x
   ld e_y(ix), #spwn1_y
   ld e_vx(ix), #0
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0x80
   jr after_calc_enem

   es_1:
   ld e_x(ix), #spwn2_x
   ld e_y(ix), #spwn2_y
   ld e_vx(ix), #0
   ld e_vy(ix), #-1
   ld e_vy+1(ix), #0x80
   jr after_calc_enem

   es_2:
   ld e_x(ix), #spwn3_x
   ld e_y(ix), #spwn3_y
   ld e_vx(ix), #0
   ld e_vx+1(ix), #0x40
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0
   jr after_calc_enem

   es_3:
   ld e_x(ix), #spwn4_x
   ld e_y(ix), #spwn4_y
   ld e_vx(ix), #-1
   ld e_vx+1(ix), #0xC0
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0
   jr after_calc_enem


   es_4:
   ld e_x(ix), #spwn5_x
   ld e_y(ix), #spwn5_y
   ld e_vx(ix), #0
   ld e_vx+1(ix), #0x40
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0
   jr after_calc_enem


   es_5:
   ld e_x(ix), #spwn6_x
   ld e_y(ix), #spwn6_y
   ld e_vx(ix), #-1
   ld e_vx+1(ix), #0xC0
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0
   jr after_calc_enem

   after_calc_enem:
   ;----

   call get_round
   ld e_enemlevel(ix), #0
   cp #5
   jr c, skip_level_calc
   ld e_enemlevel(ix), #1
   cp #16
   jr c, skip_level_calc
   ld e_enemlevel(ix), #2

   skip_level_calc:
   pop hl

   call get_enems_left_to_spawn
   cp #5
   jr nz, no_burger
      call get_round ;; 5, 8, 11, 14, 17, 19
      cp #5
      jr z, create_burger
      cp #8
      jr z, create_burger
      cp #11
      jr z, create_burger
      cp #14
      jr z, create_burger
      cp #17
      jr z, create_burger
      cp #19
      jr z, create_burger
      jr no_burger
      create_burger:
      ld e_w(ix), #4
      ld e_h(ix), #18
      ld e_type(ix), #e_type_food
      ld e_enemy_ctr_retroceso(ix), #1
      call create_enemy_food
      jp end_creation
   no_burger:
   
   ld a,l
   and #15

   call get_current_enemies

   bucle_spawn_enem_select:
   inc hl
   inc (hl)
   dec (hl)
   call z, get_current_enemies
   dec a
   jr nz, bucle_spawn_enem_select

   ld e_w(ix), #4
   ld e_h(ix), #18
   ld e_type(ix), #e_type_enemy
   ld e_deathanimCtr(ix), #30
   ld e_enemy_ctr_retroceso(ix), #1
   ld e_enemy_last_vel_x(ix), #0
   ld e_enemy_last_vel_y(ix), #0
   ld e_enemy_bullet(ix), #0

   ld a, (hl)

   cp #1
   call z, create_enemy_zombie
   cp #2
   call z, create_enemy_ghost
   cp #3
   call z, create_enemy_ovni
   cp #4
   call z, create_enemy_bat
   cp #5
   call z, create_enemy_calabaza
   cp #6
   call z, create_enemy_spider
   cp #7
   call z, create_enemy_witch
   cp #8
   call z, create_enemy_food

   end_creation:

   ld hl, #enemy_entity
   call man_entity_create
   call dec_enems_left_to_spawn
   ld hl, #is_double_spawn
   ld (hl), #0
ret


create_enemy_bat::
   ;;NECESITA VELOCIDADES EN X E Y
   push af
   ld e_vx(ix), #0
   ld e_vx+1(ix), #0x40
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0x80
   ld e_h(ix), #14
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_bat

   ld hl, #_sprite_murcielago
   ld (#enemy_entity+e_sprite), hl

   ld a, e_enemlevel(ix)
   
   cp #1
   jr z, enem3_1
   cp #2
   jr z, enem3_2


   enem3_0:
   ld e_lifes(ix), #2
   jp final_bat
   enem3_1:
   ld e_lifes(ix), #2
   jp final_bat
   enem3_2:
   ld e_lifes(ix), #3
   final_bat:
   pop af
ret

create_enemy_calabaza::
   push af
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_calabaza
   ld e_h(ix), #14
   ld e_calabaza_ctr(ix), #151
   ld e_calabaza_x(ix), #0
   ld e_calabaza_y(ix), #0

   ld hl, #_sprite_calabaza
   ld (#enemy_entity+e_sprite), hl

   ld a, e_enemlevel(ix)
   
   cp #1
   jr z, enem2_1
   cp #2
   jr z, enem2_2

   enem2_0:
   ld e_lifes(ix), #1
   jp final_calabaza
   enem2_1:
   ld e_lifes(ix), #1
   jp final_calabaza
   enem2_2:
   ld e_lifes(ix), #2
   final_calabaza:
   pop af
ret

create_enemy_zombie::

   push af

   ld e_vx(ix), #0
   ld e_vx+1(ix), #0
   ld e_vy(ix), #0
   ld e_vy+1(ix), #0

   ld e_ai_st(ix), #e_ai_st_move_to_food
   ld e_type_enemy_ai(ix), #e_type_enemy_zombie


   call get_round
   cp #4
   jr c, zombie_normal

   ld l, #2
   call cpct_getRandom_lcg_u8_asm

   ld a, l
   and #3
   cp #1
   jr z, rainbow

   zombie_normal:

   ld hl, #_sprite_e1_1
   ld (#enemy_entity+e_sprite), hl
   ld e_zombie_raimbow(ix), #0
   jr after_rb

   rainbow:

   ld hl, #_sprite_zombie_arcoiris
   ld (#enemy_entity+e_sprite), hl
   ld e_zombie_raimbow(ix), #1
   
   after_rb:

   
   ld e_foodctr(ix), #1 ;;contador comida

   ld a, e_enemlevel(ix)
   cp #1
   jr z, enem0_1
   cp #2
   jr z, enem0_2

   
   enem0_0:
   ld e_lifes(ix), #2
   jp final_zombie
   enem0_1:
   ld e_lifes(ix), #3
   jp final_zombie
   enem0_2:
   ld e_lifes(ix), #4

   final_zombie:
   pop af
ret

create_enemy_ghost::
   push af

   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_ghost
   ld a, #15
   ld e_h(ix), a

   ld hl, #_sprite_e2_1
   ld (#enemy_entity+e_sprite), hl

   ld a, e_enemlevel(ix)
   
   cp #1
   jr z, enem1_1
   cp #2
   jr z, enem1_2

   
   enem1_0:
   ld e_lifes(ix), #2
   jp final_ghost
   enem1_1:
   ld e_lifes(ix), #3
   jp final_ghost
   enem1_2:
   ld e_lifes(ix), #3
   final_ghost:
   pop af
ret

create_enemy_ovni::
   push af
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_ovni
   ld e_h(ix), #14
   ld hl, #_sprite_ovni
   ld (#enemy_entity+e_sprite), hl

   ld a, e_enemlevel(ix)
   
   cp #1
   jr z, enem4_1
   cp #2
   jr z, enem4_2

   
   enem4_0:
   ld e_lifes(ix), #3
   jp final_ovni
   enem4_1:
   ld e_lifes(ix), #4
   jp final_ovni
   enem4_2:
   ld e_lifes(ix), #4
   final_ovni:
   pop af
ret

create_enemy_spider::
   ;;NECESITA VELOCIDADES EN X E Y
   push af
   ld a, e_x(ix)
   cp #spwn1_x
   jr z, left_right
      cp #2
      jr z, spaw_spider_left
      ld e_x(ix), #72
      ld e_spider_ctr(ix), #251
      ld e_spider_move(ix), #1
      ld e_spider_move_h(ix), #1
      jp end_movement_spider
      spaw_spider_left:
      ld e_x(ix), #4
      ld e_spider_ctr(ix), #251
      ld e_spider_move(ix), #1
      ld e_spider_move_h(ix), #0
   jp end_movement_spider
   left_right:
      ld e_spider_ctr(ix), #251
      ld e_spider_move(ix), #2
      ld e_spider_move_h(ix), #2
   end_movement_spider:
   ld e_vx(ix), #0x00
   ld e_vx+1(ix), #0x00
   ld e_vy(ix), #0x00
   ld e_vy+1(ix), #0
   ld e_h(ix), #14
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_spider

   ld hl, #_sprite_arana
   ld (#enemy_entity+e_sprite), hl

   ld a, e_enemlevel(ix)
   
   cp #1
   jr z, enem5_1
   cp #2
   jr z, enem5_2

   ld e_lifes(ix), #2
   jp final_spider
   enem5_1:
   ld e_lifes(ix), #2
   jp final_spider
   enem5_2:
   ld e_lifes(ix), #3
   final_spider:
   pop af
ret

create_enemy_witch::
   ;;NECESITA VELOCIDADES EN X E Y
   push af
   ld a, e_x(ix)
   cp #spwn1_x
   jr z, left_right_witch
      cp #2
      jr z, spaw_witch_left
      ld e_x(ix), #72
      ld e_witch_ctr(ix), #251
      ld e_witch_move(ix), #1
      ld e_witch_move_h(ix), #1
      jp end_movement_witch
      spaw_witch_left:
      ld e_x(ix), #4
      ld e_witch_ctr(ix), #251
      ld e_witch_move(ix), #1
      ld e_witch_move_h(ix), #0
   jp end_movement_witch
   left_right_witch:
      ld e_witch_ctr(ix), #251
      ld e_witch_move(ix), #2
      ld e_witch_move_h(ix), #2
   end_movement_witch:
   ld e_vx(ix), #0x00
   ld e_vx+1(ix), #0x00
   ld e_vy(ix), #0x00
   ld e_vy+1(ix), #0
   ld e_h(ix), #14
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_witch

   ld hl, #_sprite_esqueleto
   ld (#enemy_entity+e_sprite), hl

   ld e_lifes(ix), #2
   pop af
ret

create_enemy_food::
   push af
   ld a, e_x(ix)
   cp #spwn1_x
   jr nz, not_center
      ld a, e_y(ix)
      cp #spwn1_y
      jr nz, food_abajo
         ld e_vx(ix), #1
         ld e_vx+1(ix), #0x00
         ld e_vy(ix), #0x00
         ld e_vy+1(ix), #0
         ld e_food_decision_enemy(ix), #0
      jp end_food_decision
      food_abajo:
         ld e_vx(ix), #-1
         ld e_vx+1(ix), #0x00
         ld e_vy(ix), #0x00
         ld e_vy+1(ix), #0
         ld e_food_decision_enemy(ix), #0
      jp end_food_decision
   not_center:
   cp #spwn3_x
   jr nz, right_side_food
      ld e_vx(ix), #0x00
      ld e_vx+1(ix), #0x00
      ld e_vy(ix), #-1
      ld e_vy+1(ix), #0
      ld e_food_decision_enemy(ix), #1
   jp end_food_decision
   right_side_food:
      ld e_vx(ix), #0x00
      ld e_vx+1(ix), #0x00
      ld e_vy(ix), #1
      ld e_vy+1(ix), #0
      ld e_food_decision_enemy(ix), #1


   end_food_decision:

   ld e_type(ix), #e_type_food
   ld e_death(ix), #1
   ld e_food_ctr_enemy(ix), #10
   ld e_h(ix), #14
   ld e_ai_st(ix), #e_ai_st_move_to
   ld e_type_enemy_ai(ix), #e_type_enemy_food
   ld e_food_ctr_destroy(ix), #50
   ld e_food_ctr_destroy_counter(ix), #25
   ld e_food_final_reder(ix), #125

   ld hl, #_sprite_burger
   ld (#enemy_entity+e_sprite), hl

   ld e_lifes(ix), #1
   pop af
ret