online multiplayer chess game (note server currently down)
-rw-r--r--Grid.gd19
-rw-r--r--pieces/Pawn.gd53
-rw-r--r--ui/GameUI.tscn14
-rw-r--r--ui/Lobby.gd31
4 files changed, 58 insertions, 59 deletions
diff --git a/Grid.gd b/Grid.gd
index ea64bbc..b104128 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -65,7 +65,7 @@ func _exit_tree() -> void:
func _input(event) -> void: # input
if event.is_action_released("debug"): # if debug
- print_matrix_pretty() # print the matrix
+ print_matrix_pretty(matrix) # print the matrix
if event.is_action_released("kill"):
if last_clicked and OS.is_debug_build(): # last clicked isnt null and were in debug
last_clicked.took() # kill the piece
@@ -73,7 +73,7 @@ func _input(event) -> void: # input
clear_fx() # clear the circles
-static func print_matrix_pretty(mat = matrix) -> void: # print the matrix
+static func print_matrix_pretty(mat) -> void: # print the matrix
for j in range(8): # for each row
var r: Array = mat[j] # get the row
if j == 0:
@@ -86,7 +86,7 @@ static func print_matrix_pretty(mat = matrix) -> void: # print the matrix
if c: # if there is a piece
row += c.mininame + ender # add the shortname
else: # if there is no piece
- row += " " + ender # add 00
+ row += " " + ender
print(row) # print the string
print("%s\n%s\n%s" % [middish_heads, letter_header, smaller_heads])
@@ -285,7 +285,7 @@ func square_clicked(position: Vector2) -> void: # square clicked
return
if check_for_frame(position): # takeable
handle_take(position)
- if check_for_circle(position): # see if theres a circle at the position
+ elif check_for_circle(position): # see if theres a circle at the position
handle_move(position) # move
last_clicked.clear_clicked() # remove the circles
last_clicked = null # set it to null
@@ -301,8 +301,7 @@ func handle_take(position) -> void:
var pawn = last_clicked
if check_promote(pawn, position, "take"):
return
- turn_over()
- Globals.network.send_move_packet([last_clicked.real_position, position], Network.MOVEHEADERS.take) # piece taking piece
+ Globals.network.send_move_packet([last_clicked.real_position, position], Network.MOVEHEADERS.move) # piece taking piece
func handle_move(position) -> void:
@@ -320,7 +319,7 @@ func handle_move(position) -> void:
},
Network.MOVEHEADERS.castle
)
- turn_over()
+
return
if last_clicked is Pawn:
var pawn = last_clicked
@@ -333,11 +332,9 @@ func handle_move(position) -> void:
[pawn.real_position, position, en_passant_data[1].real_position],
Network.MOVEHEADERS.passant
)
- turn_over()
return
if check_promote(pawn, position):
return
- turn_over()
Globals.network.send_move_packet([last_clicked.real_position, position], Network.MOVEHEADERS.move) # piece moving
@@ -349,10 +346,6 @@ func check_promote(pawn, position, calltype: String = "move") -> bool:
return false
-func turn_over() -> void:
- promoting = null
-
-
func clear_fx() -> void: # clear the circles
for i in range(8): # for each row
for j in range(8): # for each column
diff --git a/pieces/Pawn.gd b/pieces/Pawn.gd
index 298e00a..949819a 100644
--- a/pieces/Pawn.gd
+++ b/pieces/Pawn.gd
@@ -9,7 +9,6 @@ var enpassant := []
var promoteposition := Vector2()
var promotetake := false
-var promote_prev_pos := Vector2()
onready var whiteint := 1 if white else -1
onready var sprites := []
@@ -40,13 +39,14 @@ func _exit_tree() -> void:
func moveto(position, real = true, take = false, override_moveto = false) -> void:
# check if 2 step
- if real and !twostepfirstmove and !has_moved:
- if white and real_position.y - position.y == 2:
- twostepfirstmove = true
- just_set = true
- if !white and position.y - real_position.y == 2:
- twostepfirstmove = true
- just_set = true
+ if real:
+ if !twostepfirstmove and !has_moved:
+ if white and real_position.y - position.y == 2:
+ twostepfirstmove = true
+ just_set = true
+ if !white and position.y - real_position.y == 2:
+ twostepfirstmove = true
+ just_set = true
.moveto(position, real, take, override_moveto)
if real:
Globals.reset_halfmove()
@@ -121,12 +121,9 @@ func en_passant(turncheck = true) -> Array: # in passing
func promote(position, type) -> void:
- promote_prev_pos = real_position
if type == "take":
- take(at_pos(position), true)
- promotetake = true
- else:
- moveto(position, true, false, true) # dont add the algebraic position
+ at_pos(position).hide()
+ move(position) # only move the visuals
promoteposition = position
darken.show()
for i in range(len(promotables)):
@@ -134,29 +131,29 @@ func promote(position, type) -> void:
sprites[i].show()
-func take(piece: Piece, overridemoveto = false) -> void:
- clear_clicked()
- piece.took()
- moveto(piece.real_position, true, true, overridemoveto)
- Globals.reset_halfmove()
-
-
func handle_sprite_input_event(node) -> void:
darken.hide()
- var script = piece(promotables[sprites.find(node)][0])
+ var promote_to = promotables[sprites.find(node)][0]
var first = (
algebraic_move_notation(promoteposition)
if !promotetake
- else algebraic_take_notation(promoteposition, promote_prev_pos)
+ else algebraic_take_notation(promoteposition, real_position)
+ )
+ var notation = "%s=%s" % [first, promote_to]
+ Globals.grid.promoting = null
+ Globals.network.send_move_packet(
+ {
+ "start_position": real_position,
+ "destination": promoteposition,
+ "become": promote_to,
+ "notation": notation,
+ "white": white
+ },
+ Network.MOVEHEADERS.promote
)
- Utils.add_move("%s=%s" % [first, promotables[sprites.find(node)][0]])
- Globals.grid.make_piece(real_position, script, white)
- Globals.grid.turn_over()
- clear_clicked()
- queue_free()
-func piece(string) -> String:
+static func piece(string) -> String:
match string:
"Q":
return "res://pieces/Queen.gd"
diff --git a/ui/GameUI.tscn b/ui/GameUI.tscn
index b59d354..f79b3a4 100644
--- a/ui/GameUI.tscn
+++ b/ui/GameUI.tscn
@@ -123,7 +123,6 @@ __meta__ = {
}
[node name="BlackTime" type="Label" parent="Holder/Back/VBox"]
-visible = false
margin_top = 105.0
margin_right = 400.0
margin_bottom = 188.0
@@ -147,9 +146,9 @@ color = Color( 0, 0, 0, 1 )
[node name="Status" type="Label" parent="Holder/Back/VBox"]
visible = false
-margin_top = 259.0
+margin_top = 264.0
margin_right = 400.0
-margin_bottom = 290.0
+margin_bottom = 295.0
custom_fonts/font = SubResource( 1 )
text = "hi there"
align = 1
@@ -170,10 +169,9 @@ custom_constants/vseparation = 0
columns = 3
[node name="WhiteTime" type="Label" parent="Holder/Back/VBox"]
-visible = false
-margin_top = 421.0
+margin_top = 488.0
margin_right = 400.0
-margin_bottom = 504.0
+margin_bottom = 571.0
custom_fonts/font = ExtResource( 2 )
text = "00:00.0"
align = 1
@@ -197,9 +195,9 @@ color = Color( 0, 0, 0, 1 )
script = ExtResource( 5 )
[node name="FENlabel" type="LineEdit" parent="Holder/Back/VBox"]
-margin_top = 488.0
+margin_top = 621.0
margin_right = 400.0
-margin_bottom = 562.0
+margin_bottom = 695.0
custom_colors/font_color_uneditable = Color( 1, 1, 1, 1 )
custom_fonts/font = SubResource( 8 )
text = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
diff --git a/ui/Lobby.gd b/ui/Lobby.gd
index 1d314b9..b2cbd7f 100644
--- a/ui/Lobby.gd
+++ b/ui/Lobby.gd
@@ -133,23 +133,34 @@ func _on_data(data: Dictionary) -> void:
match data["movetype"]:
Network.MOVEHEADERS.passant:
# en passant
- var end_pos = Vector2(dict2vec(data["positions"][1]))
- var start_piece = Piece.at_pos(Vector2(dict2vec(data["positions"][0])))
- Piece.at_pos(Vector2(dict2vec(data["positions"][2]))).took() # kill the unfortunate
+ var end_pos = dict2vec(data["positions"][1])
+ var start_piece = Piece.at_pos(dict2vec(data["positions"][0]))
+ Piece.at_pos(dict2vec(data["positions"][2])).took() # kill the unfortunate
start_piece.passant(end_pos)
- Network.MOVEHEADERS.take:
- var start_piece = Piece.at_pos(Vector2(dict2vec(data["positions"][0])))
- var end_piece = Piece.at_pos(Vector2(dict2vec(data["positions"][1])))
- start_piece.take(end_piece)
Network.MOVEHEADERS.move:
- var end_pos = Vector2(dict2vec(data["positions"][1]))
- var start_piece = Piece.at_pos(Vector2(dict2vec(data["positions"][0])))
- start_piece.moveto(end_pos)
+ var start_piece = Piece.at_pos(dict2vec(data["positions"][0]))
+ var end_pos = dict2vec(data["positions"][1])
+ var end_piece = Piece.at_pos(end_pos)
+ if end_piece != null:
+ start_piece.take(end_piece)
+ else:
+ start_piece.moveto(end_pos)
Network.MOVEHEADERS.castle:
var king = Piece.at_pos(dict2vec(data["positions"]["king"]))
var rook = Piece.at_pos(dict2vec(data["positions"]["rook"]))
rook.moveto(dict2vec(data["positions"]["rookdestination"]), true, false, true)
Utils.add_move(king.castle(dict2vec(data["positions"]["kingdestination"])))
+ Network.MOVEHEADERS.promote:
+ var dict_data = data["positions"] # positions is a dict for readability sometimes
+ var pawn = Piece.at_pos(dict2vec(dict_data["start_position"]))
+ var dest = dict2vec(dict_data["destination"])
+ if Piece.at_pos(dest) != null:
+ Piece.at_pos(dest).took() # move the pawn to the new place, killing if necessary
+ pawn.clear_clicked()
+ Globals.grid.make_piece(dest, Pawn.piece(dict_data["become"]), dict_data["white"]) # create the promotion
+ pawn.took() # kill the pawn
+ Utils.add_move(dict_data["notation"]) # add a move
+ Globals.grid.print_matrix_pretty(Globals.grid.matrix)
func dict2vec(dict: Dictionary) -> Vector2: