online multiplayer chess game (note server currently down)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
extends Node2D
class_name Grid

export var PIECE_SET = "california"

export(Color) var board_color1 = Color.white
export(Color) var board_color2 = Color.black
export(Color) var overlay_color = Color(0.2, 0.345098, 0.188235, 0.592157)

onready var background = $Background

var ASSETS_PATH = "res://assets/" + PIECE_SET + "/"

const Piece = preload("res://Piece.tscn")
const Square = preload("res://Square.tscn")

const piece_size = Vector2(100, 100)

var matrix = []
var background_matrix = []
var last_clicked


func _ready():
	Globals.grid = self
	init_board()
	init_matrix()


func _exit_tree():
	Globals.grid = null


func init_matrix():
	for i in range(8):
		matrix.append([])
		for _j in range(8):
			matrix[i].append(null)
	add_pieces()


func instance_piece_at_position(position: Vector2, name: String, sprite: String, white: bool = true):
	var piece = Piece.instance()
	piece.sprite = piece.get_node("Sprite")
	piece.sprite.texture = load(sprite)
	piece.real_position = position
	position *= piece_size
	piece.global_position = position
	piece.realname = name
	piece.name = name
	piece.white = white
	add_child(piece)
	return piece


func init_board():
	for i in range(8):
		background_matrix.append([])
		for j in range(8):
			# var square = ColorRect.new()
			var square = Square.instance()
			square.rect_size = piece_size
			square.rect_position = Vector2(i, j) * piece_size
			square.realname = "square_" + str(i) + "_" + str(j)
			square.color = board_color1 if (i + j) % 2 == 0 else board_color2
			square.real_position = Vector2(i, j)
			background.add_child(square)
			square.connect("clicked", self, "square_clicked")
			background_matrix[i].append(square)
	print_matrix_pretty(background_matrix)


func add_pieces():
	add_pawns()
	add_rooks()
	add_knights()
	add_bishops()
	add_queens()
	add_kings()
	print_matrix_pretty()


func add_pawns():
	for i in range(8):
		matrix[1][i] = instance_piece_at_position(
			Vector2(i, 1), "pawn", ASSETS_PATH + "bP.png", false
		)
		matrix[6][i] = instance_piece_at_position(
			Vector2(i, 6), "pawn", ASSETS_PATH + "wP.png", true
		)


func add_rooks():
	matrix[0][0] = instance_piece_at_position(Vector2(0, 0), "rook", ASSETS_PATH + "bR.png", false)
	matrix[0][7] = instance_piece_at_position(Vector2(7, 0), "rook", ASSETS_PATH + "bR.png", false)
	matrix[7][0] = instance_piece_at_position(Vector2(0, 7), "rook", ASSETS_PATH + "wR.png", true)
	matrix[7][7] = instance_piece_at_position(Vector2(7, 7), "rook", ASSETS_PATH + "wR.png", true)


func add_knights():
	matrix[0][1] = instance_piece_at_position(
		Vector2(1, 0), "knight", ASSETS_PATH + "bN.png", false
	)
	matrix[0][6] = instance_piece_at_position(
		Vector2(6, 0), "knight", ASSETS_PATH + "bN.png", false
	)
	matrix[7][1] = instance_piece_at_position(Vector2(1, 7), "knight", ASSETS_PATH + "wN.png", true)
	matrix[7][6] = instance_piece_at_position(Vector2(6, 7), "knight", ASSETS_PATH + "wN.png", true)


func add_bishops():
	matrix[0][2] = instance_piece_at_position(
		Vector2(2, 0), "bishop", ASSETS_PATH + "bB.png", false
	)
	matrix[0][5] = instance_piece_at_position(
		Vector2(5, 0), "bishop", ASSETS_PATH + "bB.png", false
	)
	matrix[7][2] = instance_piece_at_position(Vector2(2, 7), "bishop", ASSETS_PATH + "wB.png", true)
	matrix[7][5] = instance_piece_at_position(Vector2(5, 7), "bishop", ASSETS_PATH + "wB.png", true)


func add_queens():
	matrix[0][3] = instance_piece_at_position(Vector2(3, 0), "queen", ASSETS_PATH + "bQ.png", false)
	matrix[7][3] = instance_piece_at_position(Vector2(3, 7), "queen", ASSETS_PATH + "wQ.png", true)


func add_kings():
	matrix[0][4] = instance_piece_at_position(Vector2(4, 0), "king", ASSETS_PATH + "bK.png", false)
	matrix[7][4] = instance_piece_at_position(Vector2(4, 7), "king", ASSETS_PATH + "wK.png", true)


func print_matrix_pretty(mat = matrix):
	print("[")
	for r in mat:
		var row = "	["
		for i in range(8):
			var c = r[i]
			var ender = ", " if i < 7 else ""
			if c:
				row += c.realname + ender
			else:
				row += "null" + ender
		print(row + "],")
	print("]")


func check_for_circle(position: Vector2):
	return background_matrix[position.x][position.y].circle.visible


func check_for_frame(position: Vector2):
	if !matrix[position.y][position.x]:
		return false
	return matrix[position.y][position.x].frame.visible


func square_clicked(position: Vector2):
	var spot = matrix[position.y][position.x]
	if !spot or !spot.white:  # spot is not a tile or spot is not white
		if !last_clicked:  # last clicked is null, so this is pointless
			return
		if check_for_circle(position):  # see if theres a circle at the position
			last_clicked.moveto(position)  # if there is, move there
		if check_for_frame(position):  # takeable
			last_clicked.take(matrix[position.y][position.x])  # eat
		last_clicked.clear_clicked()  # remove the circles
		last_clicked = null  # set it to null
	elif last_clicked != spot:  # we got a new piece (or pawn) clicked
		if last_clicked:  # remove the circles
			last_clicked.clear_clicked()
		last_clicked = spot  # set it to the new spot
		spot.clicked()  # tell the piece shit happeend


func clear_circles():
	for i in range(8):
		for j in range(8):
			var square = background_matrix[i][j]
			square.set_circle(false)


func clear_frames():
	for i in range(8):
		for j in range(8):
			var square = matrix[i][j]
			if square:
				square.set_frame(false)