Files
millimeters-of-aluminum/scenes/map_drawer.gd
olof.pettersson 99e032f303 initial commit
2025-09-12 15:54:10 +02:00

71 lines
2.0 KiB
GDScript

class_name MapDrawer
extends Node2D
# A reference to the StarSystemGenerator node, which will be set by the MapCanvas.
var star_system_generator: Node
func _process(_delta: float) -> void:
# This forces the canvas to redraw every frame.
queue_redraw()
func _draw() -> void:
if not star_system_generator:
return
# Draw a black background to obscure the main simulation view.
var viewport_size = get_viewport().get_visible_rect().size
draw_rect(Rect2(Vector2.ZERO, viewport_size), Color("3d3d3d"))
var max_distance = 0.0
var all_bodies = star_system_generator.get_all_bodies()
# Find the furthest body from the star.
for body in all_bodies:
var distance = body.position.length()
if distance > max_distance:
max_distance = distance
# Determine the scale based on the viewport size.
var system_diameter = max_distance * 2.0
var draw_scale = min(viewport_size.x, viewport_size.y) / system_diameter
draw_scale *= 0.8 # Add some padding to the view.
# Draw each celestial body on the canvas.
for body in all_bodies:
# Get the scaled position.
var scaled_position = body.position * draw_scale + viewport_size / 2
# Define visual size based on body type.
var radius = 0.0
var color = Color.WHITE
match body.get_class_name():
"Star":
color = Color.GOLD
"Planet":
color = Color.BLUE
"Moon":
color = Color.PURPLE
"Station":
color = Color.WHITE
"Asteroid":
color = Color.BROWN
_:
color = Color.ORANGE_RED
if body is Star:
radius = 10.0 # Make the star smaller for the map.
elif body is Planet:
radius = 8.0
elif body is Moon:
radius = 5.0
elif body is Asteroid:
radius = 4.0 # Make asteroids bigger for visibility.
elif body is Station:
radius = 6.0
# Draw the celestial body.
draw_circle(scaled_position, radius, color)
# Position the text slightly to the side of the circle.
draw_string(ThemeDB.fallback_font, scaled_position + Vector2(radius + 2, -5), body.name, HORIZONTAL_ALIGNMENT_LEFT, -1, ThemeDB.fallback_font_size, color)