extends Node2D # Spawns "Big Stable Asteroid" planets in ASTEROID_FIELD biome chunks near # the fleet where WorldGen says one belongs, and despawns them once the # fleet has moved far enough away - mirrors AsteroidField's chunk-lifecycle # pattern, but at a sparser, larger-radius scale since planets are rare # landmarks rather than common decoration. @export var check_interval: float = 1.0 @export var spawn_radius_chunks: int = 3 @export var despawn_radius_chunks: int = 6 @onready var fleet: Node2D = get_node("../Fleet") var planet_scene: PackedScene = preload("res://Planet.tscn") var populated_chunks: Dictionary = {} var timer: float = 0.0 func _process(delta: float) -> void: timer += delta if timer < check_interval: return timer = 0.0 update_chunks() func get_planet_fleet_is_on() -> Node2D: # Fleet centroid distance, not per-ship Area2D overlap - a single # stable number instead of many independently-noisy ship positions. # See Planet.gd for why this matters. var fleet_centroid: Vector2 = fleet.get_centroid() for chunk in populated_chunks.keys(): for planet in populated_chunks[chunk]: if is_instance_valid(planet) and planet.position.distance_to(fleet_centroid) <= planet.landing_radius: return planet return null func update_chunks() -> void: # Public (not just called from _process's own timer): also invoked # once synchronously right after the fleet is positioned on a fresh # Main.tscn load, so a nearby planet exists immediately instead of # only appearing ~check_interval seconds later - most noticeable as a # jarring pop-in right after returning from a planet's surface. var center_chunk: Vector2i = WorldGen.chunk_coords(fleet.get_centroid()) for dx in range(-spawn_radius_chunks, spawn_radius_chunks + 1): for dy in range(-spawn_radius_chunks, spawn_radius_chunks + 1): var chunk: Vector2i = center_chunk + Vector2i(dx, dy) if populated_chunks.has(chunk): continue if not WorldGen.has_planet_at_chunk(chunk): continue _spawn_chunk_planet(chunk) var to_remove: Array = [] for chunk in populated_chunks.keys(): if _chebyshev_chunk_distance(chunk, center_chunk) > despawn_radius_chunks: for planet in populated_chunks[chunk]: if is_instance_valid(planet): planet.queue_free() to_remove.append(chunk) for chunk in to_remove: populated_chunks.erase(chunk) func _chebyshev_chunk_distance(a: Vector2i, b: Vector2i) -> int: return max(abs(a.x - b.x), abs(a.y - b.y)) func _spawn_chunk_planet(chunk: Vector2i) -> void: # WorldGen.planet_data_at_chunk() is seeded purely from the chunk's own # coordinates, so a given chunk always produces the exact same planet - # name, position within the chunk, and shape - no matter how many times # it's despawned and respawned across a scene reload. It's also the # same function the world map uses to place a marker for a # not-currently-loaded explored chunk, so the two never disagree. var data: Dictionary = WorldGen.planet_data_at_chunk(chunk) var origin: Vector2 = WorldGen.chunk_origin(chunk) var planet: Node2D = planet_scene.instantiate() planet.position = origin + data.local_pos planet.planet_name = data.name planet.shape_seed = data.shape_seed # Deferred: update_chunks() can run synchronously from inside a # parent's own _ready() chain (forced once right after the fleet is # positioned, to avoid a pop-in on scene load) - Godot refuses a # direct add_child() while the tree is still "busy setting up # children" in that situation, so this must be deferred either way. get_parent().add_child.call_deferred(planet) populated_chunks[chunk] = [planet]