82 lines
3.4 KiB
GDScript
82 lines
3.4 KiB
GDScript
@tool
|
|
extends Sprite2D
|
|
class_name PlantPartScene
|
|
|
|
@export var part_name: String
|
|
|
|
@export_tool_button("Load resource") var load_resource_button = load_resource
|
|
|
|
@export var root: Node2D
|
|
@export var attaches: Node
|
|
@export var bottom_attaches: Node
|
|
@export var type: PlantPart.PartType
|
|
|
|
@export var is_back: bool
|
|
@export var base_attachable: bool
|
|
@export var bottom_attachable: bool
|
|
@export var branch_attachable: bool
|
|
|
|
@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"
|
|
var plant_part = ResourceLoader.load(destination)
|
|
if plant_part is PlantPart:
|
|
root.position = plant_part.root
|
|
texture = plant_part.texture
|
|
|
|
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]
|
|
elif i >= attaches_children.size():
|
|
var new_child = Node2D.new()
|
|
new_child.name = "attach" + str(i)
|
|
new_child.position = plant_part.attaches[i]
|
|
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")
|
|
|
|
var bottom_attaches_children := bottom_attaches.get_children()
|
|
for i in maxi(bottom_attaches_children.size(), plant_part.bottom_attaches.size()):
|
|
if i < bottom_attaches_children.size() && i < plant_part.bottom_attaches.size():
|
|
bottom_attaches_children[i].position = plant_part.bottom_attaches[i]
|
|
elif i >= bottom_attaches_children.size():
|
|
var new_child = Node2D.new()
|
|
new_child.name = "bottom_attach" + str(i)
|
|
new_child.position = plant_part.bottom_attaches[i]
|
|
bottom_attaches.add_child(new_child)
|
|
new_child.set_owner(self )
|
|
elif i >= plant_part.bottom_attaches.size():
|
|
bottom_attaches_children[i].free()
|
|
else:
|
|
printerr("Invalid code path")
|
|
|
|
type = plant_part.type
|
|
is_back = plant_part.is_back
|
|
base_attachable = plant_part.base_attachable
|
|
bottom_attachable = plant_part.bottom_attachable
|
|
branch_attachable = plant_part.branch_attachable
|
|
else:
|
|
printerr("Error loading ", part_name)
|
|
|
|
func save_as_resource():
|
|
var destination := "res://entities/plants/resources/plant_parts/" + part_name + ".tres"
|
|
var attaches_vec2: Array[Vector2]
|
|
for attach in attaches.get_children():
|
|
attaches_vec2.append(attach.position)
|
|
var bottom_attaches_vec2: Array[Vector2]
|
|
for bottom_attach in bottom_attaches.get_children():
|
|
bottom_attaches_vec2.append(bottom_attach.position)
|
|
|
|
var plant_part = PlantPart.new()
|
|
plant_part.init(texture, root.position, attaches_vec2, bottom_attaches_vec2, type, is_back, base_attachable, bottom_attachable, branch_attachable)
|
|
var err := ResourceSaver.save(plant_part, destination)
|
|
if err != OK:
|
|
printerr("Error saving resource: ", error_string(err))
|
|
else:
|
|
plant_part.take_over_path(destination)
|