Files
millimeters-of-aluminum/scenes/orrey_view/orrey_view.gd
2025-12-05 15:50:48 +01:00

75 lines
2.8 KiB
GDScript

# res://scenes/debug/orrery_view.gd
extends Node2D
@export_group("Generation Assets")
@export var star_scene: PackedScene
@export var planet_scene: PackedScene
@export var moon_scene: PackedScene
@export var barycenter_scene: PackedScene
@export var spaceship_scene: PackedScene # Add this even if unused for now
@export_group("Orrery Settings")
@export var debug_marker_scene: PackedScene # Link your new DebugMarker.tscn here
@onready var camera: Camera2D = $Camera2D
@onready var system_root: Node2D = $GeneratedSystem
@onready var info_label: RichTextLabel = $UI/InfoPanel/MarginContainer/InfoLabel
var markers: Dictionary = {} # Maps a body to its marker
func _ready():
# 1. Create and configure the generator tool.
var generator = StarSystemGenerator.new()
# Create a temporary StarSystem-like object to pass to the generator
var temp_system_node = Node2D.new()
temp_system_node.star_scene = star_scene
temp_system_node.planet_scene = planet_scene
temp_system_node.moon_scene = moon_scene
temp_system_node.barycenter_scene = barycenter_scene
temp_system_node.spaceship_scene = spaceship_scene
# 2. Generate the system under our dedicated root node.
var generation_result = generator.generate(temp_system_node)
system_root.add_child(temp_system_node) # Keep the generated hierarchy
# 3. Create a debug marker for every generated body.
var all_bodies = generation_result.all_bodies + generation_result.all_barycenters
for body in all_bodies:
if is_instance_valid(body):
var marker = debug_marker_scene.instantiate()
add_child(marker) # Add markers to the OrreryView, not the generated system
marker.initialize(body)
marker.marker_selected.connect(_on_marker_selected)
markers[body] = marker
func _process(_delta):
# Keep the markers synced with their body's global position.
for body in markers:
var marker = markers[body]
marker.global_position = body.global_position
func _unhandled_input(event: InputEvent):
# Simple camera controls
if event is InputEventMouseMotion and event.button_mask & MOUSE_BUTTON_MASK_MIDDLE:
camera.offset -= event.relative / camera.zoom.x
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
camera.zoom *= 1.2
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
camera.zoom /= 1.2
func _on_marker_selected(body):
# Update the info panel with the selected body's data.
var text = "[b]%s[/b]\n" % body.name
if body is OrbitalBody3D:
text += "Mass: %.2f\n" % body.mass
text += "Velocity: (%.2f, %.2f)\n" % [body.linear_velocity.x, body.linear_velocity.y]
text += "Position: (%.0f, %.0f)\n" % [body.global_position.x, body.global_position.y]
if body is Barycenter and is_instance_valid(body.get_parent()):
text += "Parent: %s" % body.get_parent().name
info_label.text = text