extends Area2D

var enemy_type: String = "horse"

var bullet_scene = preload("res://scenes/boss_bullet.tscn")
var start_pos = Vector2.ZERO
var speed = 0
var tween_speed: float = 1.4
  
var exploding = false

var rotate_speed: int = 100
var rotate_direction: int = 0
var shoot_timer_wait_time: float = 0.1
var spawn_point_count: int = 4
var spawn_radius = 100

var horse_phase: bool = false


@onready var boss = $AnimationPlayer
@onready var shoot_timer = $ShootTimer
@onready var rotater = $Rotater
@onready var screensize  = get_viewport_rect().size

#func _ready():
   
    
  
  #var step = 2 * PI / spawn_point_count
  
  #for i in range(spawn_point_count):
    #var spawn_point = Node2D.new()
    #var pos = Vector2(spawn_radius, 0).rotated(step * i)
    #spawn_point.position = pos
    #spawn_point.rotation = pos.angle()
    #rotater.add_child(spawn_point)
    
  #shoot_timer.wait_time = shooter_timer_wait_time
  #shoot_timer.start()
  
#func _process(delta: float) -> void:
  #var new_rotation = rotater.ro  tation_degrees + rotate_speed * delta
  #rotater.rotation_degrees - fmod(new_rotation, 360)
  
func start():
  $HorseWinnie.play()
  shoot_timer.start()
  rotate_direction = randi_range(0,1)
  print("Shoot timer started!")

func end():
  shoot_timer.stop()  
  

func _process(_delta):
  
  if rotate_direction == 0:
    rotater.rotation_degrees += 2.5  # Adjust rotation speed
  if rotate_direction == 1:
    rotater.rotation_degrees -= 2.5  # Adjust rotation speed

func spawn_bullet():
  var bullet = bullet_scene.instantiate()
  $Pew.play()
  get_tree().root.add_child(bullet)
  bullet.position = rotater.global_position
  bullet.rotation = rotater.global_rotation
  shoot_timer.wait_time = shoot_timer_wait_time
  shoot_timer.start()




    
func _on_shoot_timer_timeout() -> void:
  spawn_bullet()
  #for s in rotater.get_children():
    #var b = bullet_scene.instantiate()
    #get_tree().root.add_child(b)
    ##b.position = s.global_position
    ##b.rotation = s.global_rotation
    
    
  
    

  
func boss_horse():
  await get_tree().create_timer(2).timeout
  EventBus.flash_screen.emit(.25)
  $Sprite2D.frame = 6
  $Horse.show()
  $CollisionShape2D.show()
  var horse_player = $Horse/AnimationPlayer
  horse_player.play("run")
  await get_tree().create_timer(5).timeout
  EventBus.flash_screen.emit(.25)
  $Horse.hide()
  $CollisionShape2D.show()
  
func hit_detection():
  print("Horse hit!")
