working with new sprites
This commit is contained in:
@@ -16,6 +16,20 @@ const PLACEHOLDER_GROWING_TEXTURE: Texture = preload("res://entities/plants/asse
|
||||
|
||||
var rng := RandomNumberGenerator.new()
|
||||
|
||||
func random_ind(array: Array) -> int:
|
||||
return rng.randi_range(0, array.size() - 1)
|
||||
|
||||
func pick_random(array: Array) -> Variant:
|
||||
return array[random_ind(array)]
|
||||
|
||||
func shuffle(array: Array):
|
||||
var available_ind := 0
|
||||
for i in range(array.size()):
|
||||
var temp = array[available_ind]
|
||||
var picked_ind := rng.randi_range(available_ind, array.size() - 1)
|
||||
array[available_ind] = array[picked_ind]
|
||||
array[picked_ind] = temp
|
||||
|
||||
func build_seed_texture(_random_seed: int) -> Texture:
|
||||
return PLACEHOLDER_SEED_TEXTURE
|
||||
|
||||
@@ -25,13 +39,10 @@ func build_plant_texture(plant_data: PlantData) -> Texture:
|
||||
var mature_texture: Texture = PLACEHOLDER_MATURE_TEXTURE
|
||||
var mature_image: Image = Image.create_empty(IMAGE_WIDTH, IMAGE_HEIGHT, false, Image.FORMAT_RGBA8)
|
||||
var mature_image_center: Vector2i = 0.5 * mature_image.get_size()
|
||||
var branch_parts: Array[PlantPart]
|
||||
var base_part: PlantPart
|
||||
var available_base_attaches: Array[Vector2]
|
||||
var available_base_bottom_attach: Array[Vector2]
|
||||
var branch_attaches: Array[Vector2]
|
||||
var branch_root: Array[Vector2]
|
||||
var branch_parent_attach: Array[Vector2]
|
||||
var base_image_coord: Vector2i
|
||||
var available_attaches: Array[PlantAttach]
|
||||
var parent_image_coords: Array[Vector2]
|
||||
var parts_to_place: Array[PlantPart]
|
||||
|
||||
match plant_data.get_state():
|
||||
@@ -46,13 +57,14 @@ func build_plant_texture(plant_data: PlantData) -> Texture:
|
||||
|
||||
var base_image = base_part.texture.get_image()
|
||||
var base_image_center: Vector2i = 0.5 * base_image.get_size()
|
||||
mature_image.blend_rect(base_image, Rect2i(Vector2i.ZERO, base_image.get_size()), mature_image_center - base_image_center - Vector2i(base_part.root))
|
||||
base_image_coord = mature_image_center - Vector2i(base_part.root.position)
|
||||
mature_image.blend_rect(base_image, Rect2i(Vector2i.ZERO, base_image.get_size()), base_image_coord - base_image_center)
|
||||
|
||||
if branches.size() == 0:
|
||||
printerr("No branches in archetype")
|
||||
# var branch_parts: Array[PlantPart] = parts_archetype_associations[plant_archetype].branches
|
||||
for i in n_branches:
|
||||
branch_parts.append(pick_random(branches))
|
||||
parts_to_place.append(pick_random(branches))
|
||||
|
||||
for m in plant_data.mutations:
|
||||
print("mutations: ", m.id)
|
||||
@@ -73,7 +85,8 @@ func build_plant_texture(plant_data: PlantData) -> Texture:
|
||||
|
||||
var base_image = base_part.texture.get_image()
|
||||
var base_image_center: Vector2i = 0.5 * base_image.get_size()
|
||||
mature_image.blend_rect(base_image, Rect2i(Vector2i.ZERO, base_image.get_size()), mature_image_center - base_image_center - Vector2i(base_part.root))
|
||||
base_image_coord = mature_image_center - Vector2i(base_part.root.position)
|
||||
mature_image.blend_rect(base_image, Rect2i(Vector2i.ZERO, base_image.get_size()), base_image_coord - base_image_center)
|
||||
|
||||
for m in plant_data.mutations:
|
||||
print("mutations: ", m.id)
|
||||
@@ -85,74 +98,34 @@ func build_plant_texture(plant_data: PlantData) -> Texture:
|
||||
print("Not handled state")
|
||||
return null
|
||||
|
||||
available_base_attaches = base_part.attaches.duplicate()
|
||||
available_base_bottom_attach = base_part.bottom_attaches.duplicate()
|
||||
|
||||
assert(branch_parts.size() <= base_part.attaches.size(),
|
||||
str("More branches (", branch_parts.size(), ") than base attaches (", base_part.attaches.size(), ")"))
|
||||
|
||||
for branch in branch_parts:
|
||||
if available_base_attaches.size() == 0:
|
||||
break
|
||||
|
||||
var ind: int = rng.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()
|
||||
var branch_image_center: Vector2i = 0.5 * branch_image.get_size()
|
||||
mature_image.blend_rect(branch_image, Rect2i(Vector2i.ZERO, branch.texture.get_size()), mature_image_center - branch_image_center + Vector2i(attach - branch.root - base_part.root))
|
||||
|
||||
for branch_attach in branch.attaches:
|
||||
branch_attaches.append(branch_attach)
|
||||
branch_root.append(branch.root)
|
||||
branch_parent_attach.append(attach)
|
||||
|
||||
if parts_to_place.size() > branch_attaches.size() + base_part.attaches.size() - branch_parts.size():
|
||||
printerr("ERROR generating : Parts to place : ", parts_to_place.size(),
|
||||
"; Branch Attaches : ", branch_attaches.size(),
|
||||
"; Base attaches : ", base_part.attaches.size(),
|
||||
"; Branch parts : ", branch_parts.size())
|
||||
available_attaches = base_part.attaches.duplicate()
|
||||
parent_image_coords.resize(available_attaches.size())
|
||||
parent_image_coords.fill(base_image_coord)
|
||||
|
||||
for part: PlantPart in parts_to_place:
|
||||
print("create part")
|
||||
var attach: Vector2
|
||||
var parent_root: Vector2
|
||||
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:
|
||||
print("No attach available")
|
||||
print("Add part : ", part.resource_name)
|
||||
var ind := find_random_matching_attach_ind(part.root, available_attaches)
|
||||
if ind == -1:
|
||||
printerr("No attach found")
|
||||
continue
|
||||
chosen_attach_type = pick_random(attachables)
|
||||
|
||||
if chosen_attach_type == 1: # base attach
|
||||
var ind := rng.randi_range(0, available_base_attaches.size() - 1)
|
||||
attach = available_base_attaches.pop_at(ind)
|
||||
parent_root = base_part.root
|
||||
elif chosen_attach_type == 2: # bottom
|
||||
var ind := rng.randi_range(0, available_base_bottom_attach.size() - 1)
|
||||
attach = available_base_bottom_attach.pop_at(ind)
|
||||
parent_root = base_part.root
|
||||
elif chosen_attach_type == 3: # branch
|
||||
var ind := rng.randi_range(0, branch_attaches.size() - 1)
|
||||
attach = branch_attaches.pop_at(ind) - branch_root.pop_at(ind) + branch_parent_attach.pop_at(ind)
|
||||
parent_root = base_part.root
|
||||
var attach: PlantAttach = available_attaches.pop_at(ind)
|
||||
var parent_image_coord: Vector2i = parent_image_coords.pop_at(ind)
|
||||
|
||||
var part_image: Image = part.texture.get_image()
|
||||
var part_image_center: Vector2i = 0.5 * part_image.get_size()
|
||||
var relative_root_centered_pos: Vector2 = Vector2(mature_image_center - part_image_center) - part.root
|
||||
mature_image.blend_rect(part_image, Rect2i(Vector2i.ZERO, part.texture.get_size()), relative_root_centered_pos + attach - parent_root)
|
||||
var part_image_coord: Vector2i = parent_image_coord + Vector2i(attach.position - part.root.position)
|
||||
mature_image.blend_rect(part_image, Rect2i(Vector2i.ZERO, part.texture.get_size()), part_image_coord - part_image_center)
|
||||
|
||||
for sub_attach in part.attaches:
|
||||
available_attaches.append(sub_attach)
|
||||
parent_image_coords.append(part_image_coord)
|
||||
|
||||
if rng.randi() % 2 == 0:
|
||||
mature_image.flip_x()
|
||||
mature_texture = ImageTexture.create_from_image(mature_image)
|
||||
return mature_texture
|
||||
|
||||
func pick_random(array: Array):
|
||||
return array[rng.randi_range(0, array.size() - 1)]
|
||||
## returns -1 if not found
|
||||
func find_random_matching_attach_ind(attach_to_match: PlantAttach, array: Array[PlantAttach]) -> int:
|
||||
var indices: Array = range(array.size())
|
||||
shuffle(indices)
|
||||
return indices.find_custom(func(i): return array[i].attach_types.any(func(type): return attach_to_match.attach_types.has(type)))
|
||||
|
||||
Reference in New Issue
Block a user