extends Node2D

var StarScene = preload("res://scenes/Star.tscn")
var PlanetScene = preload("res://scenes/Planet.tscn")

var PLANET_COUNT = randf_range(1,5)
const ORBIT_STEP = 120

func _ready():
	randomize()
	spawn_star()
	spawn_planets(PLANET_COUNT)
	var background = Sprite2D.new()
	background.texture = load("res://assets/background/space1.jpg")
	background.centered = true
	background.position = get_viewport_rect().size / 2

	# 🔥 automatyczne dopasowanie do ekranu
	var screen_size = get_viewport_rect().size
	var tex_size = background.texture.get_size()
	var scale_factor = max(screen_size.x / tex_size.x, screen_size.y / tex_size.y)
	background.scale = Vector2(scale_factor, scale_factor)

	add_child(background)
	background.z_index = -10  # żeby zawsze był za planetami

func spawn_star():
	var star = StarScene.instantiate()
	add_child(star)
	star.position = get_viewport_rect().size / 2
	star.set_texture(random_star_texture())

func spawn_planets(count: int):
	var center = get_viewport_rect().size / 2
	for i in range(count):
		var planet = PlanetScene.instantiate()
		add_child(planet)

		var orbit_radius = (i + 1) * ORBIT_STEP
		planet.orbit_radius = orbit_radius
		planet.orbit_center = center

		# 🔥 Tutaj zmieniasz prędkość i powinno działać
		planet.rotation_speed = randf_range(0.01, 0.04)  # wolno
		# planet.rotation_speed = 1.0  # szybciej
		# planet.rotation_speed = 5.0  # bardzo szybko

		planet.set_texture(random_planet_texture())

func random_star_texture() -> Texture2D:
	var id = randi_range(1, 16)
	return load("res://assets/stars/s%d.png" % id)

func random_planet_texture() -> Texture2D:
	var id = randi_range(1, 18)
	return load("res://assets/planets/p%d.png" % id)
