plant builder
This commit is contained in:
47
entities/plants/scripts/texture_builder/part_assembler.gd
Normal file
47
entities/plants/scripts/texture_builder/part_assembler.gd
Normal file
@@ -0,0 +1,47 @@
|
||||
extends Node
|
||||
|
||||
@export var n_plants_to_generate: int
|
||||
@export var space_between_plants: int
|
||||
@export var base_mutations: Array[PlantMutation]
|
||||
@export var all_mutations: Array[PlantMutation]
|
||||
@export var max_n_mutations := 2
|
||||
# @export var base_parts: Array[PackedScene]
|
||||
# @export var leaves_parts: Array[PackedScene]
|
||||
# @export var branches_parts: Array[PackedScene]
|
||||
# @export var flowers_parts: Array[PackedScene]
|
||||
|
||||
func _ready():
|
||||
generate_plants();
|
||||
|
||||
func generate_plants():
|
||||
var fail_safe := 0;
|
||||
for i in n_plants_to_generate:
|
||||
|
||||
|
||||
# func populate_part(parent: PlantPart, parent_node: Node2D, depth := 1):
|
||||
# if parent.attaches.size() == 0:
|
||||
# return
|
||||
|
||||
# if depth >= max_depth:
|
||||
# return
|
||||
|
||||
# var n_sub_parts: int = randi() % parent.attaches.size();
|
||||
# var available_parent_attaches: Array = range(parent.attaches.size());
|
||||
# for i in n_sub_parts:
|
||||
# var attach_ind_ind: int = randi() % available_parent_attaches.size();
|
||||
# var parent_attach_ind: int = available_parent_attaches[attach_ind_ind];
|
||||
# available_parent_attaches.pop_at(attach_ind_ind);
|
||||
# var attach := parent.attaches[parent_attach_ind];
|
||||
|
||||
# var next_packed_scene: PackedScene;
|
||||
# if parent.type == PlantPart.PartType.BASE_PART:
|
||||
# next_packed_scene =
|
||||
# elif parent.type == PlantPart.PartType.BRANCH_PART:
|
||||
# var selected_root: Vector2 = next_part.roots.pick_random().position;
|
||||
|
||||
# var sprite := Sprite2D.new();
|
||||
# sprite.texture = next_part.texture;
|
||||
# sprite.position = attach.position - selected_root;
|
||||
# parent_node.add_child(sprite);
|
||||
|
||||
# populate_part(next_part, sprite, depth + 1);
|
||||
@@ -0,0 +1 @@
|
||||
uid://daod4korkrm2r
|
||||
26
entities/plants/scripts/texture_builder/plant_part.gd
Normal file
26
entities/plants/scripts/texture_builder/plant_part.gd
Normal file
@@ -0,0 +1,26 @@
|
||||
extends Resource
|
||||
class_name PlantPart
|
||||
|
||||
enum PartType {BASE_PART, BRANCH_PART, LEAF_PART, FLOWER_PART}
|
||||
|
||||
@export var texture: Texture
|
||||
@export var root: Vector2
|
||||
@export var attaches: Array[Vector2]
|
||||
@export var bottom_attaches: Array[Vector2]
|
||||
@export var type: PartType
|
||||
|
||||
@export var is_back: bool
|
||||
@export var base_attachable: bool
|
||||
@export var bottom_attachable: bool
|
||||
@export var branch_attachable: bool
|
||||
|
||||
func _init(textute_arg: Texture, root_arg: Vector2, attaches_arg: Array[Vector2], bottom_attaches_arg: Array[Vector2], type_arg: PartType, is_back_arg: bool, base_attachable_arg: bool, bottom_attachable_arg: bool, branch_attachable_arg: bool) -> void:
|
||||
texture = textute_arg
|
||||
root = root_arg
|
||||
attaches = attaches_arg
|
||||
bottom_attaches = bottom_attaches_arg
|
||||
type = type_arg
|
||||
is_back = is_back_arg
|
||||
base_attachable = base_attachable_arg
|
||||
bottom_attachable = bottom_attachable_arg
|
||||
branch_attachable = branch_attachable_arg
|
||||
@@ -0,0 +1 @@
|
||||
uid://b3jwglylqdqtw
|
||||
30
entities/plants/scripts/texture_builder/plant_part_scene.gd
Normal file
30
entities/plants/scripts/texture_builder/plant_part_scene.gd
Normal file
@@ -0,0 +1,30 @@
|
||||
@tool
|
||||
extends Sprite2D
|
||||
class_name PlantPartScene
|
||||
|
||||
@export var root: Node2D
|
||||
@export var attaches: Array[Node2D]
|
||||
@export var bottom_attaches: Array[Node2D]
|
||||
@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 save_as_resource():
|
||||
var destination := "res://entities/plants/resources/plant_parts/" + name + ".tres"
|
||||
print("Saving: ", name, " at: ", destination)
|
||||
var attaches_vec2: Array[Vector2]
|
||||
for attach in attaches:
|
||||
attaches_vec2.append(attach.position)
|
||||
var bottom_attaches_vec2: Array[Vector2]
|
||||
for bottom_attach in bottom_attaches:
|
||||
bottom_attaches_vec2.append(bottom_attach.position)
|
||||
|
||||
var plant_part = PlantPart.new(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))
|
||||
@@ -0,0 +1 @@
|
||||
uid://c360ic1aost1n
|
||||
@@ -1,19 +1,97 @@
|
||||
extends Resource
|
||||
class_name TextureBuilder
|
||||
|
||||
const PLACEHOLDER_SEED_TEXTURE : Texture = preload("res://entities/plants/assets/sprites/default/seed.png")
|
||||
const PLACEHOLDER_MATURE_TEXTURE : Texture = preload("res://entities/plants/assets/sprites/default/mature.png")
|
||||
const PLACEHOLDER_GROWING_TEXTURE : Texture = preload("res://entities/plants/assets/sprites/default/growing.png")
|
||||
const PLACEHOLDER_SEED_TEXTURE: Texture = preload("res://entities/plants/assets/sprites/default/seed.png")
|
||||
const PLACEHOLDER_MATURE_TEXTURE: Texture = preload("res://entities/plants/assets/sprites/default/mature.png")
|
||||
const PLACEHOLDER_GROWING_TEXTURE: Texture = preload("res://entities/plants/assets/sprites/default/growing.png")
|
||||
|
||||
|
||||
func build_seed_texture(_random_seed : int) -> Texture:
|
||||
func build_seed_texture(_random_seed: int) -> Texture:
|
||||
return PLACEHOLDER_SEED_TEXTURE
|
||||
|
||||
func build_plant_texture(plant_data : PlantData) -> Texture:
|
||||
|
||||
func build_plant_texture(plant_data: PlantData) -> Texture:
|
||||
match plant_data.get_state():
|
||||
PlantData.State.MATURE:
|
||||
return PLACEHOLDER_MATURE_TEXTURE
|
||||
var mature_texture: Texture = PLACEHOLDER_MATURE_TEXTURE
|
||||
var mature_image: Image
|
||||
|
||||
var base_part: PlantPart = plant_data.archetype.bases.pick_random();
|
||||
|
||||
mature_image = base_part.texture.get_image()
|
||||
|
||||
var branch_parts: Array[PlantPart] = plant_data.archetype.branches
|
||||
|
||||
var parts_to_place: Array[PlantPart];
|
||||
for m in plant_data.mutations:
|
||||
var mutation := m;
|
||||
var mutation_possible_parts := mutation.possible_parts;
|
||||
for p in mutation.part_amount:
|
||||
parts_to_place.append(mutation_possible_parts.pick_random())
|
||||
|
||||
var available_base_attaches: Array[Vector2] = base_part.attaches.duplicate();
|
||||
|
||||
var available_base_bottom_attach: Array[Vector2] = base_part.bottom_attaches.duplicate();
|
||||
|
||||
var branch_attaches: Array[Vector2];
|
||||
var branch_attach_parent: Array[PlantPart];
|
||||
|
||||
assert(branch_parts.size() <= base_part.attaches.size());
|
||||
|
||||
for branch in branch_parts:
|
||||
if available_base_attaches.size() == 0:
|
||||
break
|
||||
|
||||
var ind: int = randi_range(0, available_base_attaches.size() - 1);
|
||||
var attach: Vector2 = available_base_attaches.pop_at(ind);
|
||||
|
||||
var branch_image: Image = branch.texture.get_image()
|
||||
mature_image.blend_rect(branch_image, Rect2i(Vector2.ZERO, branch.texture.get_size()), attach)
|
||||
|
||||
for branch_attach in branch.attaches:
|
||||
branch_attaches.append(branch_attach);
|
||||
branch_attach_parent.append(branch);
|
||||
|
||||
assert(parts_to_place.size() <= branch_attaches.size() + base_part.attaches.size() - branch_parts.size());
|
||||
|
||||
for part: PlantPart in parts_to_place:
|
||||
var attach: Vector2;
|
||||
var parent_part: PlantPart;
|
||||
var chosen_attach_type: int = 0;
|
||||
var attachables: Array[int];
|
||||
if part.base_attachable && available_base_attaches.size() > 0:
|
||||
attachables.append(1);
|
||||
if part.bottom_attachable && available_base_bottom_attach.size() > 0:
|
||||
attachables.append(2);
|
||||
if part.branch_attachable && branch_attaches.size() > 0:
|
||||
attachables.append(3);
|
||||
# assert(attachables.size() > 0);
|
||||
if attachables.size() == 0:
|
||||
continue
|
||||
chosen_attach_type = attachables.pick_random();
|
||||
|
||||
if chosen_attach_type == 1: # base attach
|
||||
var ind := randi_range(0, available_base_attaches.size() - 1);
|
||||
attach = available_base_attaches.pop_at(ind);
|
||||
parent_part = base_part;
|
||||
elif chosen_attach_type == 2: # bottom
|
||||
var ind := randi_range(0, available_base_bottom_attach.size() - 1);
|
||||
attach = available_base_bottom_attach.pop_at(ind);
|
||||
parent_part = base_part;
|
||||
elif chosen_attach_type == 3: # branch
|
||||
var ind := randi_range(0, branch_attaches.size() - 1);
|
||||
attach = branch_attaches.pop_at(ind);
|
||||
parent_part = branch_attach_parent.pop_at(ind);
|
||||
|
||||
var part_image: Image = part.texture.get_image()
|
||||
mature_image.blend_rect(part_image, Rect2i(Vector2.ZERO, part.texture.get_size()), attach)
|
||||
|
||||
|
||||
if plant_data.random_seed % 2 == 0:
|
||||
mature_image.flip_x()
|
||||
mature_texture = ImageTexture.create_from_image(mature_image)
|
||||
return mature_texture
|
||||
|
||||
|
||||
PlantData.State.GROWING:
|
||||
return PLACEHOLDER_GROWING_TEXTURE
|
||||
_:
|
||||
|
||||
Reference in New Issue
Block a user