110 lines
2.6 KiB
GDScript
110 lines
2.6 KiB
GDScript
extends CanvasLayer
|
|
|
|
@onready var container = %PartsContainer as GridContainer
|
|
@onready var whole = %Whole as Sprite2D
|
|
# from part 0 to 3, rotated by 0, 90, 180, 270 degrees
|
|
var rotations = [0, 0, 0, 0]
|
|
|
|
var selected := 0:
|
|
set(value):
|
|
selected = value
|
|
_display_selected()
|
|
|
|
|
|
func _ready() -> void:
|
|
layer = GlobalConfig.LAYER_LITTLE_GAME
|
|
for i in range(4):
|
|
var part = container.get_child(i)
|
|
part.pressed.connect(_select_part.bind(part))
|
|
_shuffle()
|
|
_display_selected()
|
|
whole.visible = false
|
|
|
|
|
|
func _select_part(part) -> void:
|
|
selected = part.get_index()
|
|
_display_selected()
|
|
|
|
|
|
func _exchange_parts(a: int, b: int) -> void:
|
|
var part_a = container.get_child(a)
|
|
var part_b = container.get_child(b)
|
|
container.move_child(part_a, b)
|
|
container.move_child(part_b, a)
|
|
# reassign answer
|
|
container.move_child(part_a, b)
|
|
|
|
|
|
func _rotate_part(direction := 1) -> void:
|
|
var part = container.get_child(selected) as TextureButton
|
|
var id = int(str(part.name))
|
|
rotations[id] = wrapi(rotations[id] + direction, 0, 4)
|
|
var image = part.texture_normal.get_image()
|
|
image.rotate_90(direction)
|
|
part.texture_normal = ImageTexture.create_from_image(image)
|
|
|
|
|
|
func _shuffle() -> void:
|
|
# shuffle answer
|
|
for i in range(30):
|
|
var a = randi() % 4
|
|
var b = randi() % 4
|
|
_exchange_parts(a, b)
|
|
# shuffle rotations
|
|
for i in range(4):
|
|
selected = i
|
|
for j in range(randi() % 4):
|
|
_rotate_part(1)
|
|
selected = 0
|
|
|
|
|
|
func _display_selected() -> void:
|
|
for i in range(4):
|
|
if i == selected:
|
|
container.get_child(i).modulate = Color(1, 1, 1)
|
|
else:
|
|
container.get_child(i).modulate = Color(0.7, 0.7, 0.7)
|
|
|
|
|
|
func _check_answer() -> void:
|
|
var success = true
|
|
for i in range(4):
|
|
var part = container.get_child(i)
|
|
var id = int(str(part.name))
|
|
if rotations[id] != 0 or id != i:
|
|
success = false
|
|
if success:
|
|
whole.visible = true
|
|
container.visible = false
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
var handled = false
|
|
if event.is_action_pressed("up"):
|
|
if selected > 1:
|
|
_exchange_parts(selected, selected - 2)
|
|
selected -= 2
|
|
handled = true
|
|
elif event.is_action_pressed("down"):
|
|
if selected < 2:
|
|
_exchange_parts(selected, selected + 2)
|
|
selected += 2
|
|
handled = true
|
|
elif event.is_action_pressed("left"):
|
|
if selected % 2 == 1:
|
|
_exchange_parts(selected, selected - 1)
|
|
selected -= 1
|
|
handled = true
|
|
elif event.is_action_pressed("right"):
|
|
if selected % 2 == 0:
|
|
_exchange_parts(selected, selected + 1)
|
|
selected += 1
|
|
handled = true
|
|
elif event.is_action_pressed("interact"):
|
|
_rotate_part()
|
|
handled = true
|
|
if handled:
|
|
_display_selected()
|
|
_check_answer()
|
|
get_viewport().set_input_as_handled()
|