summaryrefslogtreecommitdiff
path: root/Main.gd
blob: 72cbacb86a2dcaed27c780a2d929bb4075074ad9 (plain) (blame)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
extends Node

export (PackedScene) var piece_scene
export (PackedScene) var movetile_scene

var board = new_board2()
var board_cell = 128
const BOARD_WIDTH = 7 # starting at 0..7
const BOARD_HEIGHT = 7

var movement_layer = false
var movement_layer_piece = null

var safely_handle_movement = false

var team1 = 1
var team2 = 2

func _ready():
	make_player1("green")
	make_player2("red")
	print(OS.set_window_size(Vector2(700,700)))

# horrifying discovery: this occurs after the signal capturing functions
func _process(delta):
	# hack to prevent 'clicking' on a killed piece
	safely_handle_movement = false

func remove_movement_layer():
	movement_layer = false
	movement_layer_piece = null
	var movement_tiles = get_tree().get_nodes_in_group("tile")
	for e in movement_tiles:
		e.queue_free()

func piece_clicked(piece):
	if movement_layer:
		print("I was clicked on, but the movement layer is toggled")
		if movement_layer_piece == click_spot():
			remove_movement_layer()
	else:
		if ! safely_handle_movement:
			var piece_name = piece.get_piece()
			print("You clicked on a %s, team %s" % [piece_name, piece.get_team()])
			var location = click_spot()
			print("Spot: %s " % location)
			var pattern = get_move_pattern(piece, location)
			if can_chess_move(pattern, location):
				movement_layer = true
				movement_layer_piece = location

func click_spot():
	var square = get_viewport().get_mouse_position()
	square[0] = floor(square[0] / board_cell)
	square[1] = floor(square[1] / board_cell)
	return square

func make_player2(color):
	spawn_piece('rook', color, 0, 0, team2)
	spawn_piece('knight', color, 1, 0, team2)
	spawn_piece('bishop', color, 2, 0, team2)
	spawn_piece('queen', color, 3, 0, team2)
	spawn_piece('king', color, 4, 0, team2)
	spawn_piece('bishop', color, 5, 0, team2)
	spawn_piece('knight', color, 6, 0, team2)
	spawn_piece('rook', color, 7, 0, team2)
	for i in BOARD_WIDTH + 1:
		spawn_piece('pawn', color, i , 1, team2)

func make_player1(color):
	spawn_piece('rook', color, 0, 7, team1)
	spawn_piece('knight', color, 1, 7, team1)
	spawn_piece('bishop', color, 2, 7, team1)
	spawn_piece('queen', color, 3, 7, team1)
	spawn_piece('king', color, 4, 7, team1)
	spawn_piece('bishop', color, 5, 7, team1)
	spawn_piece('knight', color, 6, 7, team1)
	spawn_piece('rook', color, 7, 7, team1)
	for i in BOARD_WIDTH + 1:
		spawn_piece('pawn', color, i , 6, team1)

func spawn_piece(piece_name, color, x=0, y=0, team=0):
	var piece = piece_scene.instance()
	piece.init(piece.piece_map.get(piece_name), piece.piece_color.get(color), team)
	add_child(piece)
	piece.connect("clicked", self, "piece_clicked", [piece])
	board_add_piece(piece, x, y)
	piece.position = in_square(Vector2(x * board_cell, y * board_cell))
	return piece

func rand_pos():
	return Vector2(rand_range(0, get_viewport().size.x),rand_range(0, get_viewport().size.y))

# needs to be Vector2 as that is what object.position takes
func in_square(vect2):
	#print(vect2)
	vect2.x = ceil(vect2.x / board_cell)
	#print(vect2.x)
	vect2.x *= board_cell
	vect2.x += board_cell / 2
	
	vect2.y = ceil(vect2.y / board_cell)
	#print(vect2.y)
	vect2.y *= board_cell
	vect2.y += board_cell / 2
	
	#print(vect2)
	return vect2

func new_board2():
		#  x →
	# y
	# ↓
	# [0][0] = top left, [0][7] = bottom left, [7][0] = top right, [7][7] = bottom right
	var board = []
	for i in BOARD_HEIGHT + 1:
		board.append([])
		board[i].resize(BOARD_WIDTH + 1)
		for j in BOARD_WIDTH + 1:
			board[i][j] = 0
	return board

func new_board():
	return [[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0],
		[0,0,0,0,0,0,0,0]]

func board_add_piece(piece, x, y):
	board[x][y] = piece

enum {
	move_2_up_pawn,
	move_1_up_pawn,
	attack_1_nw,
	attack_1_ne,
	
	move_2_down_pawn,
	move_1_down_pawn,
	attack_1_sw,
	attack_1_se,
	
	move_1_up,
	move_1_down,
	move_1_left,
	move_1_right,
	move_1_nw,
	move_1_ne,
	move_1_sw,
	move_1_se,
	
	move_up_inf,
	move_down_inf,
	move_left_inf,
	move_right_inf,
	
	move_nw_inf,
	move_ne_inf,
	move_sw_inf,
	move_se_inf,
	
	knight,
}

func get_move_pattern(piece, coords):
	var piece_name = piece.get_piece()
	match (piece_name):
		"pawn":
			if piece.get_team() == team1:
				if coords[1] == 6:
					print("This pawn piece hasn't moved!")
					return [attack_1_nw, move_2_up_pawn, attack_1_ne]
				else:
					print("This pawn piece has moved and that is hecking brave!")
					return [attack_1_nw, move_1_up_pawn, attack_1_ne]
			else:
				if coords[1] == 1:
					return [attack_1_sw, move_2_down_pawn, attack_1_se]
				else:
					return [attack_1_sw, move_1_down_pawn, attack_1_se]
		"rook":
			return [move_up_inf, move_left_inf, move_right_inf, move_down_inf]
		"knight":
			return [knight]
		"bishop":
			return [move_ne_inf, move_nw_inf, move_sw_inf, move_se_inf]
		"queen":
			return [move_up_inf, move_down_inf, move_left_inf, move_right_inf, move_ne_inf, move_nw_inf, move_sw_inf, move_se_inf]
		"king":
			return [move_1_down, move_1_left, move_1_right, move_1_up, move_1_ne, move_1_nw, move_1_se, move_1_sw]
		_:
			print("no pattern for this one")

func can_chess_move(pattern, coords):
	var can_move = false
	var curr_team = board[coords[0]][coords[1]].get_team()
	for e in pattern:
		match (e):
			move_1_down_pawn:
				if make_tiles(coords, [0,1], 1, true, curr_team): can_move = true
			move_2_down_pawn:
				if make_tiles(coords, [0,1], 2, true, curr_team): can_move = true
			attack_1_sw:
				if make_tiles(coords, [-1,1], 1, false, curr_team, true): can_move = true
			attack_1_se:
				if make_tiles(coords, [1,1], 1, false, curr_team, true): can_move = true

			move_1_up_pawn:
				if make_tiles(coords, [0,-1], 1, true, curr_team): can_move = true
			move_2_up_pawn:
				if make_tiles(coords, [0,-1], 2, true, curr_team): can_move = true
			attack_1_nw:
				if make_tiles(coords, [-1,-1], 1, false, curr_team, true): can_move = true
			attack_1_ne:
				if make_tiles(coords, [1,-1], 1, false, curr_team, true): can_move = true

			move_up_inf:
				if make_tiles(coords, [0,-1], BOARD_HEIGHT, false, curr_team): can_move =true
			move_down_inf:
				if make_tiles(coords, [0,1], BOARD_HEIGHT, false, curr_team): can_move = true
			move_left_inf:
				if make_tiles(coords, [-1,0], BOARD_WIDTH, false, curr_team): can_move = true
			move_right_inf:
				if make_tiles(coords, [1,0], BOARD_WIDTH, false, curr_team): can_move = true

			move_ne_inf:
				if make_tiles(coords, [1,-1], 8, false, curr_team): can_move = true
			move_nw_inf:
				if make_tiles(coords, [-1,-1], 8, false, curr_team): can_move = true
			move_sw_inf:
				if make_tiles(coords, [-1,1], 8, false, curr_team): can_move = true
			move_se_inf:
				if make_tiles(coords, [1,1], 8, false, curr_team): can_move = true
			
			knight:
				if make_tiles(coords, [1,2], 1, false, curr_team): can_move = true
				if make_tiles(coords, [1,-2], 1, false, curr_team): can_move = true
				if make_tiles(coords, [-1,-2], 1, false, curr_team): can_move = true
				if make_tiles(coords, [-1,2], 1, false, curr_team): can_move = true
				if make_tiles(coords, [2,1], 1, false, curr_team): can_move = true
				if make_tiles(coords, [2,-1], 1, false, curr_team): can_move = true
				if make_tiles(coords, [-2,-1], 1, false, curr_team): can_move = true
				if make_tiles(coords, [-2,1], 1, false, curr_team): can_move = true
			
			move_1_down:
				if make_tiles(coords, [0,1], 1, false, curr_team): can_move = true
			move_1_up:
				if make_tiles(coords, [0,-1], 1, false, curr_team): can_move = true
			move_1_right:
				if make_tiles(coords, [1,0], 1, false, curr_team): can_move = true
			move_1_left:
				if make_tiles(coords, [-1,0], 1, false, curr_team): can_move = true
			move_1_ne:
				if make_tiles(coords, [1,-1], 1, false, curr_team): can_move = true
			move_1_nw:
				if make_tiles(coords, [-1,-1], 1, false, curr_team): can_move = true
			move_1_se:
				if make_tiles(coords, [1,1], 1, false, curr_team): can_move = true
			move_1_sw:
				if make_tiles(coords, [-1,1], 1, false, curr_team): can_move = true
	return can_move

func movetile_clicked(move_tile):
	print("Yep, I was clicked")
	var location = click_spot()
	var check = board[location[0]][location[1]]
	if ! check:
		pass
	else:
		check.kill()

	board[location[0]][location[1]] = board[movement_layer_piece[0]][movement_layer_piece[1]]
	board[movement_layer_piece[0]][movement_layer_piece[1]].position = \
		in_square(Vector2(location[0] * board_cell, location[1] * board_cell))
	board[movement_layer_piece[0]][movement_layer_piece[1]] = 0
	remove_movement_layer()
	safely_handle_movement = true


func spawn_move_tile(coords):
	var move_tile = movetile_scene.instance()
	add_child(move_tile)
	move_tile.connect("move_clicked", self, "movetile_clicked", [move_tile])
	move_tile.position = in_square(Vector2(coords[0] * board_cell, coords[1] * board_cell))
	print(move_tile.position)

func make_tiles(coords, pattern, go_range, cant_attack, curr_team, must_attack = false):
	var x = coords[0]
	var y = coords[1]
	var pattern0 = pattern[0]
	var pattern1 = pattern[1]
	var a = 0
	var b = 0
	var board_size_x = 7 # starting at 0
	var board_size_y = 7
	
	var made_tile = false
	
	for i in range(1,go_range+1):
		a += pattern0
		b += pattern1
		# dont go out of bounds: not bigger than board size but not smaller than 0
		if (x + a) <= board_size_x and (y + b) <= board_size_y and (x + a) >= 0 and (y + b) >= 0 :
			var check = board[x + a][y + b]
			if ! check and ! must_attack:
				spawn_move_tile([x + a, y + b])
				made_tile = true
			elif must_attack:
				if ! check:
					pass
				elif check.get_team() != curr_team:
					spawn_move_tile([x + a, y + b])
					made_tile = true
			elif ! cant_attack and check.get_team() != curr_team: 
				spawn_move_tile([x + a, y + b])
				made_tile = true
				break # rules of chess say pieces cant go past another
			else:
				break
	return made_tile