plant builder

This commit is contained in:
Altaezio
2026-02-10 20:09:34 +01:00
parent 4b8e59ee56
commit ab5089ad6c
129 changed files with 2377 additions and 21 deletions

View File

@@ -6,17 +6,19 @@ class_name PlantArchetype
@export var texture_builder = TextureBuilder.new()
@export var plant_area_radius = 20
@export var plant_influence_radius = 100
@export var growing_time= 2
@export var growing_time = 2
@export var lifetime = 8
@export var base_score = 1
@export var seed_number = 2
@export var seed_random_loose = 1
@export var available_mutations : Array[PlantMutation] = [
@export var available_mutations: Array[PlantMutation] = [
AncientMutation.new(),
PrecociousMutation.new(),
QualityMutation.new(),
QuickMutation.new()
]
@export var bases: Array[PlantPart]
@export var branches: Array[PlantPart]
static func get_all() -> Array[PlantArchetype]:
return [

View File

@@ -2,6 +2,8 @@ extends Resource
class_name PlantMutation
@export var level : int = 1
@export var possible_parts: Array[PlantPart]
@export var part_amount: int = 1
var name : String : get = get_mutation_name

View 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);

View File

@@ -0,0 +1 @@
uid://daod4korkrm2r

View 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

View File

@@ -0,0 +1 @@
uid://b3jwglylqdqtw

View 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))

View File

@@ -0,0 +1 @@
uid://c360ic1aost1n

View File

@@ -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
_: