working with new sprites
This commit is contained in:
@@ -1,82 +1,55 @@
|
||||
@tool
|
||||
extends Sprite2D
|
||||
class_name PlantPartScene
|
||||
class_name PlantPartBuilder
|
||||
|
||||
@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 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 = ResourceLoader.load(destination)
|
||||
if plant_part is PlantPart:
|
||||
root.position = plant_part.root
|
||||
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]
|
||||
attaches_children[i].position = plant_part.attaches[i].position
|
||||
elif i >= attaches_children.size():
|
||||
var new_child = Node2D.new()
|
||||
var new_child = PlantAttachBuilder.new()
|
||||
new_child.name = "attach" + str(i)
|
||||
new_child.position = plant_part.attaches[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")
|
||||
|
||||
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"
|
||||
print("Saving: ", part_name, " at: ", destination)
|
||||
var attaches_vec2: Array[Vector2]
|
||||
var attaches_vec2: Array[PlantAttach]
|
||||
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)
|
||||
attaches_vec2.append(PlantAttachBuilder.to_plant_attach(attach))
|
||||
|
||||
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)
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user