spaceship/planets/PlanetField.gd
Loïc Wisniewski 2f161621c3 #84: move 215 root files into a folder hierarchy
Everything lived in one flat directory, so a diff gave no clue what area it
touched and finding the file you wanted meant knowing its exact name. Twelve
folders now, grouped by what a thing IS rather than what type it is - a
vessel's definition, its component scenes and its upgrade resources sit
together, because that is the set you read when you change one of them.

  buildings  core  controllers  enemies  logistics  planets
  resources  shaders  surface  ui  vessels  vfx  world

332 files moved and every res:// reference rewritten - 306 files touched,
including the autoload and main-scene paths in project.godot. Nothing renamed:
this is purely where files sit, so the diff is a pile of pure moves and no
behaviour is buried in it. ui_theme.tres, icon.svg and web_shell.html stay at
the root, where Godot and the export preset expect them.

Verified three ways, because a suite alone would not have caught all of it:
the full 117 pass, the web export builds clean, and the game renders.

The export is why that matters. The FIRST export after the move failed with
"Attempt to open script 'res://GameState.gd'" - the old path - while
project.godot on disk plainly said res://core/GameState.gd. That is this
project's documented Android FUSE mtime trap: Godot served a cached copy of a
file whose content had changed. A `touch` fixed it. Nothing in the test suite
would have found that, because the suite runs from a copy on a native
filesystem - the one place the bug cannot happen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-21 19:44:14 +02:00

96 lines
3.7 KiB
GDScript

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://planets/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.planet_id = data.planet_id
planet.shape_seed = data.shape_seed
planet.definition_id = String(data.get("definition_id", "rocky"))
# 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]