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

var real_position = Vector2.ZERO
var white := true
var shortname = ""
var has_moved = false
var sprite
var frameon
var team = "w"
var check_spots_check = true

onready var tween = $Tween
onready var anim = $AnimationPlayer
onready var colorrect = $ColorRect
onready var frame = $Frame


func _ready():
	team = "W" if white else "B"
	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()
	set_circle(get_moves())
	set_circle(get_attacks(), "take")
	print(shortname, " was clicked")


func clear_clicked():
	colorrect.hide()
	Globals.grid.clear_fx()


func move(newpos: Vector2):  # dont use directly; use moveto
	has_moved = true
	tween.interpolate_property(
		self,
		"global_position",
		global_position,
		newpos * Globals.grid.piece_size,
		0.5,
		Tween.TRANS_BACK,
		Tween.EASE_IN_OUT
	)
	anim.play("Move")
	tween.start()
	# global_position = newpos * Globals.grid.piece_size


func moveto(position, real = true):
	Globals.grid.matrix[real_position.y][real_position.x] = null
	Globals.grid.matrix[position.y][position.x] = self
	if real:
		real_position = position
		move(position)


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 traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]):
	var circle_array = []
	for i in arr:
		var pos = real_position
		while true:
			pos += i
			if !is_on_board(pos):
				break
			if at_pos(pos) != null:  # only one black
				circle_array.append(pos)
				break
			if check_spots_check and checkcheck(pos):
				continue
			circle_array.append(pos)
	return circle_array


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


func get_moves():  # @Override
	pass


func get_attacks():  # @Override
	var moves = get_moves()  # assumes the attacks are same as moves
	var final = []
	for i in moves:
		if at_pos(i) != null:
			if at_pos(i).white != white:  # attack ze enemie
				final.append(i)
	return final


func can_check_king(king):
	check_spots_check = false
	for attack in get_attacks():
		if at_pos(attack) == king:
			check_spots_check = true
			return true
	check_spots_check = true
	return false


func create_move_circles(pos):
	Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)  # make the move circle


func create_take_circles(spot):  # create take circles
	spot.set_frame(true)  # turn on the little take frame on the piece, to show its takeable


func set_circle(positions: Array, type := "move"):
	for pos in positions:
		var spot = at_pos(pos)  # get the piece at the position
		if type == "move":  # if the type is move
			create_move_circles(pos)  # create the move circle
		elif type == "take":  # if the type is take
			create_take_circles(spot)  # if the king is in check, return true


func checkcheck(pos):  # moves to position, then checks if your king is in check
	if Globals.in_check:  # if you are in check
		var mat = Globals.grid.matrix.duplicate(true)  # make a copy of the matrix
		moveto(pos, false)  # move to the position
		var retu = false  # return true by default
		if Globals.grid.check_in_check():  # if you are still in check
			retu = true  # return false, but fix the matrix first
		# Globals.grid.print_matrix_pretty(mat)  # print the matrix
		Globals.grid.matrix = mat  # revert changes on the matrix
		return retu
	return false


func is_on_board(vector: Vector2):
	if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7:
		return false
	return true


func take(piece: Piece):
	clear_clicked()
	piece.took()
	moveto(piece.real_position)


func took():  # called when piece is taken
	anim.play("Take")


func set_frame(value):
	frameon = value
	frame.visible = value