extends Node2D const TILE_SIZE = 120 const BOARD_SIZE = 8 var board_size : int var cell_size : int var grid_pos : Vector2i var grid_data : Array # matrice 8 pe 8 @onready var white_pawn_scene = preload("res://White_Pawn.tscn") var selected_piece = null func _ready(): draw_board() setup_pieces() setup_tile_areas() func _draw(): var white = Color(1, 1, 1) var black = Color(0, 0, 0) for row in range(BOARD_SIZE): for col in range(BOARD_SIZE): var color = white if (row + col) % 2 == 0 else black draw_rect(Rect2(col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE), color) func draw_board(): queue_redraw() # Declanșează funcția _draw() pentru a desena tabla func setup_pieces(): # Instanțierea pionilor albi for i in range(BOARD_SIZE): var white_pawn = white_pawn_scene.instantiate() add_child(white_pawn) white_pawn.position = Vector2(i * TILE_SIZE + 60, 6 * TILE_SIZE + 60) # Pionii albi încep pe rândul 6 func setup_tile_areas(): for row in range(BOARD_SIZE): for col in range(BOARD_SIZE): var tile_area = Area2D.new() var collision_shape = CollisionShape2D.new() var shape = RectangleShape2D.new() shape.extents = Vector2(TILE_SIZE / 2, TILE_SIZE / 2) collision_shape.shape = shape tile_area.add_child(collision_shape) tile_area.position = Vector2(col * TILE_SIZE + TILE_SIZE / 2, row * TILE_SIZE + TILE_SIZE / 2) tile_area.set_meta("grid_position", Vector2(col, row)) # Setăm coordonatele grilei add_child(tile_area) tile_area.connect("input_event", Callable(self, "_on_tile_input_event")) func _on_piece_input_event(viewport, event, shape_idx, piece): if event is InputEventMouseButton and event.pressed: if event.button_index == MOUSE_BUTTON_LEFT: if selected_piece == null: selected_piece = piece print("Pion selectat la: ", selected_piece.position) else: print("Click pe pion, selectează o casuță pentru a muta pionul.") func _on_tile_input_event(viewport, event, shape_idx): if event is InputEventMouseButton and event.pressed: if event.button_index == MOUSE_BUTTON_LEFT: var tile_area = pick_nearest_tile(event.position) if tile_area != null: var grid_position = tile_area.get_meta("grid_position") # Obține coordonatele grilei if selected_piece != null: if move_piece(selected_piece, grid_position): print("Pion mutat la: ", grid_to_position(grid_position)) else: print("Mutare invalidă") selected_piece = null func pick_nearest_tile(screen_position): var row = int(screen_position.y / TILE_SIZE) var col = int(screen_position.x / TILE_SIZE) var tile_area = null for child in get_children(): if child is Area2D and child.get_meta("grid_position", null) == Vector2(col, row): tile_area = child break return tile_area func move_piece(piece, grid_position): if piece.type == "pawn" and piece.color == "white": var current_grid_position = position_to_grid(piece.position) if is_valid_pawn_move(current_grid_position, grid_position): piece.position = grid_to_position(grid_position) return true return false func is_valid_pawn_move(current_position, target_position): if target_position.x == current_position.x: if target_position.y == current_position.y - 1: return true # Mutare cu un pătrat înainte if current_position.y == 6 and target_position.y == current_position.y - 2: return true # Mutare cu două pătrate înainte din poziția inițială return false func grid_to_position(grid_position): return grid_position * TILE_SIZE + Vector2(60,60) func position_to_grid(screen_position): return Vector2(floor(screen_position.x / TILE_SIZE), floor(screen_position.y / TILE_SIZE))