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
extends Node2D
class_name Piece, "res://assets/california/wP.png"

var real_position = Vector2.ZERO
var white := true
var realname = "pawn"
var has_moved = false
var sprite

var black_holder

onready var colorrect = $ColorRect
onready var frame = $Frame


func _ready():
	frame.position = Globals.grid.piece_size / 2
	frame.modulate = Globals.grid.overlay_color
	colorrect.color = Globals.grid.overlay_color
	colorrect.rect_size = Globals.grid.piece_size


func clicked():
	colorrect.show()
	create_circles()
	print(realname, " was clicked")


func clear_clicked():  # TODO: fix this shit
	colorrect.hide()
	Globals.grid.clear_circles()
	Globals.grid.clear_frames()


func move(newpos: Vector2):  # dont use directly; use moveto
	has_moved = true
	global_position = newpos * Globals.grid.piece_size


func moveto(position):
	Globals.grid.matrix[real_position.y][real_position.x] = null
	Globals.grid.matrix[position.y][position.x] = self
	real_position = position
	move(position)
	Globals.turn = not Globals.turn
	Globals.turns += 1


func pos_around(around_vector):
	return real_position + around_vector


func all_dirs():
	return [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT, Vector2(1, 1), Vector2(1, -1), Vector2(-1, 1), Vector2(-1, -1)]


func create_circles():
	# for motion
	match realname:
		"pawn":
			var carry = [pos_around(Vector2.UP)] if has_moved else [pos_around(Vector2.UP), pos_around(Vector2.UP * 2)]
			if !white:
				carry = ([pos_around(Vector2.DOWN)] if has_moved else [pos_around(Vector2.DOWN), pos_around(Vector2.DOWN * 2)])
			set_circle(carry)
			# deal with the take logic
			carry = []
			var takes = [pos_around(Vector2(-1, -1)), pos_around(Vector2(1, -1))]
			if !white:
				takes = [pos_around(Vector2(-1, 1)), pos_around(Vector2(1, 1))]
			for i in takes:
				i = clamp_vector(i)
				if i == null:
					continue
				carry.append(i)
			set_circle(carry, "take")
		"king":
			var carry = [
				pos_around(Vector2.UP),
				pos_around(Vector2.DOWN),
				pos_around(Vector2.LEFT),
				pos_around(Vector2.RIGHT),
				pos_around(Vector2(1, 1)),
				pos_around(Vector2(1, -1)),
				pos_around(Vector2(-1, 1)),
				pos_around(Vector2(-1, -1))
			]
			set_circle(carry)
			set_circle(carry, "take")  # king ez
		"knight":
			var carry = [
				pos_around(Vector2(-2, -1)),
				pos_around(Vector2(-2, 1)),
				pos_around(Vector2(2, -1)),
				pos_around(Vector2(2, 1)),
				pos_around(Vector2(-1, -2)),
				pos_around(Vector2(1, -2)),
				pos_around(Vector2(-1, 2)),
				pos_around(Vector2(1, 2))
			]
			set_circle(carry)
			set_circle(carry, "take")
		"rook":
			var carry = traverse(all_dirs().slice(0, 4))
			set_circle(carry)
			set_circle(carry, "take")
		"bishop":
			var carry = traverse(all_dirs().slice(4, 8))
			set_circle(carry)
			set_circle(carry, "take")
		"queen":
			var carry = traverse(all_dirs())
			set_circle(carry)
			set_circle(carry, "take")


func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]):
	var carry = []
	for i in arr:
		black_holder = false
		var pos = real_position
		while true:
			pos = pos + i
			pos = clamp_vector(pos)
			if !traverse_helper(pos):
				break
			carry.append(pos)
	black_holder = false
	return carry


func traverse_helper(pos):
	if pos == null:
		return null
	pos = at_pos(pos)
	if pos:
		if pos.white != Globals.turn and !black_holder:
			black_holder = true
			return true
		return null
	return true


func at_pos(vector):
	return Globals.grid.matrix[vector.y][vector.x]


func set_circle(positions: Array, type := "move"):
	for i in range(len(positions)):
		var pos = clamp_vector(positions[i])
		if pos == null:
			continue
		var spot = at_pos(pos)
		if type == "move":
			if spot:
				continue
			Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)
		elif type == "take":
			if spot and spot.white != Globals.turn:
				spot.set_frame(true)


func set_frame(boolean):
	frame.visible = boolean


func clamp_vector(vector: Vector2):
	if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7:
		return null
	vector.x = clamp(vector.x, 0, 7)
	vector.y = clamp(vector.y, 0, 7)
	return vector


func take(piece: Piece):
	var piecepos = piece.real_position
	piece.queue_free()
	moveto(piecepos)