@tool

extends Node3D
class_name Planet

@export var planet_data : Resource = PlanetData.new():
	get:
		return planet_data
	set(value):
		planet_data = value
		on_data_changed()
		if planet_data != null and not planet_data.is_connected("changed", on_data_changed):
			planet_data.connect("changed", on_data_changed)

func _ready():
	planet_data = PlanetData.new()
	planet_data.noise_map = FastNoiseLite.new()
	planet_data.noise_map.set_noise_type(FastNoiseLite.NoiseType.TYPE_SIMPLEX)
	planet_data.noise_map.frequency = 1
	planet_data.noise_map.fractal_octaves = 10
	planet_data.planet_color = load("res://Earth-LikeGradient.tres")
	on_data_changed()

func on_data_changed():
	var i : int = 0
	for child in get_children():
		if i < 6:
			(child as PlanetMeshFace).regenerate_mesh(planet_data)
		else:
			(child as CSGSphere3D).radius = planet_data.radius
		i += 1

func _process(delta):
	var i : int = 0
	for child in get_children():
		if i < 6:
			var face := child as PlanetMeshFace
			face.update_from_camera_pos(planet_data)
		i += 1
