xiandie/scene/little_game/书架.gd

150 lines
3.9 KiB
GDScript

extends Control
const NON_SELECTED = [-1, -1]
# 3 layers, original order is the correct answer
@export var current_answer := [[0, 1, 2, 3, 4], [0, 1, 2, 3], [0, 1, 2, 3]]
@onready var sfx_select = $SfxSelect as Sfx
@onready var sfx_interchange = $SfxInterchange as Sfx
var book_width_by_row := []
var suffling := false
# [row, col]
var selected_book := NON_SELECTED:
set(value):
if selected_book == value:
return
if suffling:
selected_book = value
return
if selected_book != NON_SELECTED:
_toggle_book(false, selected_book[0], selected_book[1])
if value != NON_SELECTED:
_toggle_book(true, value[0], value[1])
selected_book = value
func _ready() -> void:
_measure_width_by_row()
_connect_signals()
# shuffle at the end
_shuffle_books()
func _measure_width_by_row() -> void:
for row in range(3):
var length = current_answer[row].size()
var width_arr = []
for col in range(length):
var book_btn = _get_book_by_id(row, col).get_node("BookButton") as TextureButton
# book_btn.position.y = -book_btn.texture_normal.get_height()
# book_btn.size = book_btn.texture_normal.get_size()
# add 1 to avoid the gap between books
width_arr.append(book_btn.texture_normal.get_width() + 1)
book_width_by_row.append(width_arr)
func _shuffle_books() -> void:
selected_book = NON_SELECTED
suffling = true
for row in range(3):
# shuffle each row 20 times
var r_size = current_answer[row].size()
for _i in range(20):
var col_1 = randi() % r_size
var col_2 = randi() % r_size
selected_book = [row, col_1]
_interchange_book(row, col_2, false)
_relocate_books(row)
# turn off initilazing after shuffle
suffling = false
func _connect_signals() -> void:
for row in range(3):
var r_size = current_answer[row].size()
for id in range(r_size):
var book = _get_book_by_id(row, id)
book.get_node("BookButton").pressed.connect(_on_book_pressed.bind(row, id))
func _on_book_pressed(row: int, id: int) -> void:
var col = current_answer[row].find(id)
if selected_book == NON_SELECTED:
selected_book = [row, col]
else:
if selected_book == [row, col]:
selected_book = NON_SELECTED
elif selected_book[0] == row:
_interchange_book(row, col)
else:
selected_book = [row, col]
func _get_book_by_id(row: int, id: int) -> Node2D:
return get_node("./Shelf/Layer" + str(row) + "/Book" + str(id))
# func _get_book_by_pos(row: int, col: int) -> Node2D:
# var layer = get_node("./Shelf/Layer" + str(row))
# return layer.get_child(col)
func _toggle_book(selected: bool, row: int, col: int) -> void:
var id = current_answer[row][col]
var book = _get_book_by_id(row, id)
var book_tween = create_tween()
sfx_select.play()
if selected:
book_tween.parallel().tween_property(book, "position:y", -10.0, 0.2)
else:
book_tween.parallel().tween_property(book, "position:y", 0.0, 0.2)
func _interchange_book(row: int, col: int, relocate := true) -> void:
if selected_book == NON_SELECTED or selected_book[0] != row:
return
var col2 = selected_book[1]
sfx_interchange.play()
# interchange current_answer
var temp = current_answer[row][col]
current_answer[row][col] = current_answer[row][col2]
current_answer[row][col2] = temp
# reset selected_book
selected_book = NON_SELECTED
# relocate after reset selected_book
if relocate:
_relocate_books(row)
# check answer
_check_answer()
func _relocate_books(row: int) -> void:
selected_book = NON_SELECTED
var r_size = current_answer[row].size()
var x = 0
for col in range(r_size):
var id = current_answer[row][col]
var book = _get_book_by_id(row, id)
book.position.x = x
book.position.y = 0.0
x += book_width_by_row[row][id]
func _check_answer() -> void:
for row in range(3):
var r_size = current_answer[row].size()
for col in range(r_size):
if current_answer[row][col] != col:
return
# success
_success()
func _success() -> void:
print("Success!")
$Success.visible = true
#TODO
pass