From 9f7495fa83d70027f6a234be81c56515d915819a Mon Sep 17 00:00:00 2001 From: Dragos Stefan Cirstian Date: Fri, 12 Jul 2024 12:16:01 +0300 Subject: [PATCH] Upload files to "/" --- .gitignore | 2 + Chess_Board.gd | 107 +++++++++++++++++++++++++++++++++++++++++++++++ Chess_Board.tscn | 6 +++ project.godot | 20 +++++++++ tile_area.gd | 9 ++++ 5 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 Chess_Board.gd create mode 100644 Chess_Board.tscn create mode 100644 project.godot create mode 100644 tile_area.gd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4709183 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Godot 4+ specific ignores +.godot/ diff --git a/Chess_Board.gd b/Chess_Board.gd new file mode 100644 index 0000000..8b7794c --- /dev/null +++ b/Chess_Board.gd @@ -0,0 +1,107 @@ +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)) diff --git a/Chess_Board.tscn b/Chess_Board.tscn new file mode 100644 index 0000000..4a90661 --- /dev/null +++ b/Chess_Board.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://cmug8v3m7rkax"] + +[ext_resource type="Script" path="res://Chess_Board.gd" id="1_ltbw5"] + +[node name="ChessBoard" type="Node2D"] +script = ExtResource("1_ltbw5") diff --git a/project.godot b/project.godot new file mode 100644 index 0000000..21aa6b0 --- /dev/null +++ b/project.godot @@ -0,0 +1,20 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="Gpt" +config/features=PackedStringArray("4.2", "Forward Plus") +config/icon="res://icon.svg" + +[display] + +window/size/viewport_width=960 +window/size/viewport_height=960 diff --git a/tile_area.gd b/tile_area.gd new file mode 100644 index 0000000..1be3eaf --- /dev/null +++ b/tile_area.gd @@ -0,0 +1,9 @@ +extends Area2D + + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +