extends Node2D

# Constants for game configuration
const SPAWN_INTERVAL = 2.0
const MIN_SPAWN_X = 50
const MAX_SPAWN_X = 350
const SPAWN_Y = 700

# Initial velocity ranges
const INITIAL_Y_VELOCITY = -900
const INITIAL_X_VELOCITY_RANGE = Vector2(-10, 10)

# Preload scenes
var Fruit = preload("res://scenes/fruit.tscn")

# Game state
var score = 0
var is_game_active = true
var time = 0.0

func _ready():
	# Start spawning fruits
	spawn_fruit()
	$SpawnTimer.wait_time = SPAWN_INTERVAL
	$SpawnTimer.start()

func _process(delta):
	if is_game_active:
		time += delta

func spawn_fruit():
	if not is_game_active:
		return

	var fruit = Fruit.instantiate()
	add_child(fruit)

	# Random spawn position
	var spawn_x = randf_range(MIN_SPAWN_X, MAX_SPAWN_X)
	fruit.position = Vector2(spawn_x, SPAWN_Y)

	# Random rotation
	fruit.rotation = randf_range(-PI/4, PI/4)
	fruit.rotation_speed = randf_range(-10, 10)

	# Set initial velocity
	fruit.velocity = Vector2(
		randf_range(INITIAL_X_VELOCITY_RANGE.x, INITIAL_X_VELOCITY_RANGE.y),
		INITIAL_Y_VELOCITY
	)

func _on_spawn_timer_timeout():
	spawn_fruit()
