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
extends Node2D

export var PIECE_SET = "california"

export(Color) var board_color1 = Color.white
export(Color) var board_color2 = Color.black

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 = []

func init_matrix():
	for i in range(8):
		matrix.append([])
		for _j in range(8):
			matrix[i].append()
	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.sprite.flip_v = not white # this is not shogi you eejit
	if white:
		position -= Vector2(0, piece_size.y)  # boost it up
	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.name = "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)

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) * piece_size, "pawn", ASSETS_PATH + "bP.png", false
		)
		matrix[6][i] = instance_piece_at_position(
			Vector2(i, 7) * piece_size, "pawn", ASSETS_PATH + "wP.png", true
		)


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


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


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


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


func add_kings():
	matrix[0][4] = instance_piece_at_position(
		Vector2(4, 0) * piece_size, "king", ASSETS_PATH + "bK.png", false
	)
	matrix[7][4] = instance_piece_at_position(
		Vector2(4, 8) * piece_size, "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 typeof(c) != TYPE_INT:
				row += c.realname + ender
			else:
				row += "0" + ender
		print(row + "],")
	print("]")


func _ready():
	Globals.piece_size = piece_size
	init_board()
	init_matrix()


func square_clicked(position: Vector2):
	var spot = matrix[position.y][position.x]
	if typeof(spot) == TYPE_INT:
		if typeof(Globals.last_clicked) == TYPE_INT: # its 0
			return
		Globals.last_clicked.spot(position)
		Globals.last_clicked = null
	elif typeof(Globals.last_clicked) != typeof(spot) or Globals.last_clicked != spot:
		Globals.last_clicked = spot
		spot.clicked()