Randoloot/scripts/levels/celestial_body.gd

77 lines
2.6 KiB
GDScript

extends Node2D
var body_texture: Texture2D
var light_source: Node2D
var display_name: String = ""
var radius := 100.0
var is_star := false
var is_protected := false
var is_safe := false
var earthlike := false
var planet_profile: Dictionary = {}
var _label: Label
var _sprite: Sprite2D
func _ready() -> void:
if not is_star:
add_to_group("scanner_planets")
var sprite := Sprite2D.new()
_sprite = sprite
sprite.name = "BodySprite"
sprite.texture = body_texture
sprite.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR
var longest_side := maxf(body_texture.get_width(), body_texture.get_height())
sprite.scale = Vector2.ONE * (radius * 2.0 / longest_side)
if is_star:
var material := CanvasItemMaterial.new()
material.light_mode = CanvasItemMaterial.LIGHT_MODE_UNSHADED
sprite.material = material
add_child(sprite)
if is_star:
var light_color := preload("res://scripts/levels/celestial_texture_catalog.gd").star_light_color(body_texture)
preload("res://scripts/game/star_lighting.gd").attach_to(self, light_color)
else:
orient_to_star()
_label = Label.new()
_label.light_mask = 4
_label.position = Vector2(-160, radius + 14)
_label.size = Vector2(320, 80)
_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
_label.add_theme_font_size_override("font_size", 14)
_update_label()
func orient_to_star() -> void:
if is_star or not is_instance_valid(light_source) or _sprite == null:
return
var toward_star := light_source.global_position - global_position
if toward_star.length_squared() > 0.001:
_sprite.global_rotation = toward_star.angle() - preload("res://scripts/levels/celestial_texture_catalog.gd").PLANET_LIT_DIRECTION.angle()
func set_safe(value: bool) -> void:
is_safe = value
if is_node_ready():
_update_label()
queue_redraw()
func _update_label() -> void:
if _label.get_parent() == null:
add_child(_label)
if is_star:
_label.text = display_name + "\nGwiazda centralna"
_label.add_theme_color_override("font_color", Color("ffdb98"))
elif is_protected:
_label.text = display_name + " — zamieszkana" + ("\nUSŁUGI DOSTĘPNE" if is_safe else "")
_label.add_theme_color_override("font_color", Color("77e8bc") if is_safe else Color("ffbd83"))
else:
_label.text = display_name
_label.add_theme_color_override("font_color", Color("9baec6"))
if earthlike:
_label.text += "\nEARTHLIKE · LĄDOWANIE"
func _draw() -> void:
if earthlike:
draw_arc(Vector2.ZERO, radius + 9.0, -0.9, 0.9, 24, Color("7ddac7"), 2.0, true)
if is_protected:
var color := Color("77e8bc") if is_safe else Color("ffbd83")
draw_arc(Vector2.ZERO, radius + 20.0, 0, TAU, 96, Color(color, 0.65), 2.0, true)