FaC random placement
This commit is contained in:
parent
3232182a9f
commit
dbdb9a7bf2
10 changed files with 106 additions and 7 deletions
67
scenes/find_a_couple/tile_grid.gd
Normal file
67
scenes/find_a_couple/tile_grid.gd
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
extends GridContainer
|
||||
|
||||
@export var pair_count: int = 3 # Количество пар
|
||||
@export var tile_textures: Array[Texture2D] # Массив изображений
|
||||
@export var tile_size: Vector2 = Vector2(100, 100)
|
||||
|
||||
var tiles_array: Array = []
|
||||
|
||||
func _ready() -> void:
|
||||
generate_tiles()
|
||||
|
||||
func generate_tiles():
|
||||
# Очищаем предыдущие плитки
|
||||
for child in get_children():
|
||||
child.queue_free()
|
||||
tiles_array.clear()
|
||||
|
||||
# Проверяем достаточно ли изображений
|
||||
if tile_textures.size() < pair_count:
|
||||
push_error("Not enough textures for pairs! Need: ", pair_count, ", Have: ", tile_textures.size())
|
||||
return
|
||||
|
||||
# Создаем пары индексов
|
||||
var pairs_indices = create_pairs_indices()
|
||||
|
||||
# Перемешиваем порядок
|
||||
pairs_indices.shuffle()
|
||||
|
||||
# Настраиваем GridContainer
|
||||
setup_grid_container(pairs_indices.size())
|
||||
|
||||
# Создаем плитки
|
||||
for i in range(pairs_indices.size()):
|
||||
var texture_index = pairs_indices[i]
|
||||
var tile = preload("res://scenes/find_a_couple/tile_btn.tscn").instantiate()
|
||||
|
||||
# Передаем данные в плитку
|
||||
tile.front_texture = tile_textures[texture_index]
|
||||
tile.back_texture = preload("res://assets/find_a_couple/back.png") # Рубашка
|
||||
tile.tile_id = texture_index # ID для сравнения пар
|
||||
tile.custom_minimum_size = tile_size
|
||||
|
||||
#tile.tile_clicked.connect(_on_tile_clicked)
|
||||
add_child(tile)
|
||||
tiles_array.append(tile)
|
||||
|
||||
func create_pairs_indices() -> Array:
|
||||
var indices = []
|
||||
# Создаем пары (каждое изображение повторяется 2 раза)
|
||||
for i in range(pair_count):
|
||||
indices.append(i)
|
||||
indices.append(i)
|
||||
return indices
|
||||
|
||||
func setup_grid_container(total_tiles: int):
|
||||
# Вычисляем оптимальное количество колонок
|
||||
columns = ceil(sqrt(total_tiles))
|
||||
|
||||
# Вычисляем количество строк
|
||||
var rows = ceil(float(total_tiles) / columns)
|
||||
|
||||
# Устанавливаем минимальный размер контейнера
|
||||
custom_minimum_size = Vector2(columns * tile_size.x, rows * tile_size.y)
|
||||
|
||||
#func _on_tile_clicked(clicked_tile):
|
||||
# Логика обработки клика на плитке
|
||||
#process_tile_click(clicked_tile)
|
||||
Loading…
Add table
Add a link
Reference in a new issue