AppResidencia/empareja-las-sombras/number_tile.gd

64 lines
2.2 KiB
GDScript

class_name NumberTile
extends Label
var dragging = false
var drag_offset = Vector2()
var original_position = Vector2()
var correct_slot = null
func _input(event):
var is_touch = event is InputEventScreenTouch
var is_mouse_button = event is InputEventMouseButton
var is_mouse_motion = event is InputEventMouseMotion
var is_screen_drag = event is InputEventScreenDrag
# Iniciar arrastre (táctil o ratón)
if (is_touch and event.pressed) or (is_mouse_button and event.button_index == MOUSE_BUTTON_LEFT and event.pressed):
if not dragging:
var viewport_rect = get_viewport_rect()
var input_pos = event.position
if get_global_rect().intersects(Rect2(input_pos, Vector2(1, 1))):
dragging = true
drag_offset = global_position - input_pos
original_position = global_position
z_index = 10
# Soltar (táctil o ratón)
if (is_touch and not event.pressed and dragging) or (is_mouse_button and event.button_index == MOUSE_BUTTON_LEFT and not event.pressed and dragging):
if dragging:
dragging = false
z_index = 0
check_drop_on_board()
# Arrastrar (táctil o ratón)
if (is_screen_drag and dragging) or (is_mouse_motion and dragging and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)):
global_position = event.position + drag_offset
func check_drop_on_board():
if not get_parent() or not get_parent().get_parent() or not get_parent().get_parent().has_node("GameBoardContainer"):
return
var game_board = get_parent().get_parent().get_node("GameBoardContainer").get_node("GameBoard/GridContainer")
var tile_rect = get_global_rect()
var placed_on_board = false
for i in range(game_board.get_child_count()):
var slot = game_board.get_child(i)
if slot is Label:
var slot_rect = slot.get_global_rect()
if tile_rect.intersects(slot_rect) and text == slot.text and not slot.get("is_occupied"):
global_position = slot.global_position
slot.add_theme_stylebox_override("normal", StyleBoxFlat.new())
slot.get_theme_stylebox("normal").bg_color = Color(0.6, 0.9, 0.6)
set_process_input(false)
dragging = false
slot.set("is_occupied", true)
queue_free()
placed_on_board = true
break
if not placed_on_board:
global_position = original_position