extends CharacterBody3D

@export var min_speed = 10
@export var max_speed = 18

signal squashed

func _physics_process(delta):
	move_and_slide()
	
func initialize(start_position, player_position):
	# Look to the player
	look_at_from_position(start_position, player_position, Vector3.UP)
	# But rotate a little bit
	rotate_y(randf_range(-PI / 4, PI / 4))
	# Set random speed
	var random_speed = randi_range(min_speed, max_speed)
	# Apply the velocity
	velocity = Vector3.FORWARD * random_speed
	velocity = velocity.rotated(Vector3.UP, rotation.y)
	
func squash():
	squashed.emit()
	queue_free()

func _on_visibility_notifier_screen_exited():
	queue_free()
