-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.gd
More file actions
90 lines (78 loc) · 3 KB
/
board.gd
File metadata and controls
90 lines (78 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
extends GridContainer
@export var square_scene : PackedScene
signal square_clicked(grid_pos: Vector2)
var grid = []
var square_size = 64
var board_size = 8
var occupied_squares = {}
var occupied_by_piece = {}
var preview_opacity = 0.5
var highlight_color = Color(0.2, 0.8, 0.2, 0.3) # Semi-transparent green
var original_colors = {}
func _ready():
create_grid()
func create_grid():
for row in range(board_size):
for col in range(board_size):
var square_instance = square_scene.instantiate()
square_instance.name = str(Vector2(col,row))
var color = Color.WHITE if (row + col) % 2 == 0 else Color.DARK_GRAY
# Update to reference ColorRect directly
var color_rect = square_instance.get_node("ColorRect")
color_rect.color = color
# Get button as child of ColorRect
var button = color_rect.get_node("Button")
button.connect("pressed", Callable(self, "on_square_pressed").bind(Vector2(col, row)))
button.connect("mouse_entered", Callable(self, "_on_button_mouse_entered").bind(Vector2(col, row)))
button.connect("mouse_exited", Callable(self, "_on_button_mouse_exited").bind(Vector2(col, row)))
# Set position for each square
square_instance.position = Vector2(col * square_size, row * square_size)
add_child(square_instance)
grid.append(square_instance)
occupied_squares[Vector2(col,row)] = false
occupied_by_piece[Vector2(col,row)] = "none"
func on_square_pressed(grid_pos: Vector2):
square_clicked.emit(grid_pos)
print("Square clicked at: ", grid_pos)
func get_square(grid_pos: Vector2):
var index = grid_pos.y * board_size + grid_pos.x
if index >= 0 and index < grid.size():
return grid[index]
return null
func highlight_squares(positions: Array[Vector2]):
clear_highlights()
for pos in positions:
var square = get_square(pos)
if square:
var color_rect = square.get_node("ColorRect")
original_colors[pos] = color_rect.color
color_rect.color = highlight_color
func clear_highlights():
for pos in original_colors.keys():
var square = get_square(pos)
if square:
var color_rect = square.get_node("ColorRect")
color_rect.color = original_colors[pos]
original_colors.clear()
func _on_button_mouse_entered(grid_pos: Vector2):
var square = get_square(grid_pos)
if square:
var main = get_tree().get_root().get_node("Main")
if main.active_piece_type != "none":
# Show piece preview
if !occupied_squares[grid_pos]:
var texture_rect = square.get_node("ColorRect/ContentTextureRect")
texture_rect.texture = main.textures[main.active_piece_type]
texture_rect.visible = true
texture_rect.modulate.a = preview_opacity
# Show move highlights
var moves = main.get_moves(main.active_piece_type, grid_pos)
highlight_squares(moves)
func _on_button_mouse_exited(grid_pos: Vector2):
var square = get_square(grid_pos)
if square and !occupied_squares[grid_pos]:
var texture_rect = square.get_node("ColorRect/ContentTextureRect")
texture_rect.texture = null
texture_rect.visible = false
texture_rect.modulate.a = 1
clear_highlights()