Files
seeding-planets/entities/plants/scripts/texture_builder/plant_part_builder.gd
2026-02-21 18:40:00 +01:00

58 lines
2.4 KiB
GDScript

@tool
extends Sprite2D
class_name PlantPartBuilder
@export var part_name: String
@export_tool_button("Load resource") var load_resource_button = load_resource
@export var type: PlantPart.PartType
@export var root: PlantAttachBuilder
@export var attaches: Node
@export_tool_button("Save as resource") var save_as_resource_button = save_as_resource
func load_resource():
var destination := "res://entities/plants/resources/plant_parts/" + part_name + ".tres"
print("Loading: ", part_name, " at: ", destination)
var plant_part: PlantPart = ResourceLoader.load(destination) as PlantPart
if plant_part:
texture = plant_part.texture
type = plant_part.type
root.position = plant_part.root.position
root.attach_types = plant_part.root.attach_types
var attaches_children := attaches.get_children()
for i in maxi(attaches_children.size(), plant_part.attaches.size()):
if i < attaches_children.size() && i < plant_part.attaches.size():
attaches_children[i].position = plant_part.attaches[i].position
attaches_children[i].attach_types = plant_part.attaches[i].attach_types
elif i >= attaches_children.size():
var new_child = PlantAttachBuilder.new()
new_child.name = "attach" + str(i)
new_child.position = plant_part.attaches[i].position
new_child.attach_types = plant_part.attaches[i].attach_types
attaches.add_child(new_child)
new_child.set_owner(self )
elif i >= plant_part.attaches.size():
attaches_children[i].free()
else:
printerr("Invalid code path")
func save_as_resource():
var destination := "res://entities/plants/resources/plant_parts/" + part_name + ".tres"
print("Saving: ", part_name, " at: ", destination)
var attaches_vec2: Array[PlantAttach]
for attach in attaches.get_children():
attaches_vec2.append(PlantAttachBuilder.to_plant_attach(attach))
var plant_part = PlantPart.new()
plant_part.init(texture, type, PlantAttachBuilder.to_plant_attach(root), attaches_vec2)
plant_part.resource_name = part_name
var err := ResourceSaver.save(plant_part, destination, ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
if err != OK:
printerr("Error saving resource: ", error_string(err))
else:
plant_part.take_over_path(destination)