online multiplayer chess game (note server currently down)
*en passant*
bendn 2022-04-29
parent 5c19a5c · commit b1ac81f
-rw-r--r--Grid.gd9
-rw-r--r--Utils.gd4
-rw-r--r--pieces/King.gd61
-rw-r--r--pieces/Pawn.gd58
-rw-r--r--pieces/Piece.gd6
-rw-r--r--project.godot1
6 files changed, 105 insertions, 34 deletions
diff --git a/Grid.gd b/Grid.gd
index 423a0b8..e9f1bb6 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -222,6 +222,15 @@ func handle_move(position):
castle_data[1].moveto(castle_data[2])
turn_over()
return
+ if last_clicked is Pawn and last_clicked.enpassant:
+ print(last_clicked.enpassant)
+ for i in range(len(last_clicked.enpassant)):
+ var en_passant_data = last_clicked.enpassant[i]
+ if en_passant_data[0] == position:
+ en_passant_data[1].took() # kill the unfortunate
+ last_clicked.passant(en_passant_data[0])
+ turn_over()
+ return
last_clicked.moveto(position)
turn_over()
diff --git a/Utils.gd b/Utils.gd
new file mode 100644
index 0000000..89094af
--- /dev/null
+++ b/Utils.gd
@@ -0,0 +1,4 @@
+extends Node
+
+func is_pawn(inode):
+ return inode is Pawn \ No newline at end of file
diff --git a/pieces/King.gd b/pieces/King.gd
index 465a8e0..1bd4487 100644
--- a/pieces/King.gd
+++ b/pieces/King.gd
@@ -16,24 +16,30 @@ func get_moves():
continue
moves.append(spot)
if castle_check and !has_moved and !Globals.in_check: # make sure this is only called when clicking
- var rooks = [pos_around(Vector2.RIGHT * 3), pos_around(Vector2.LEFT * 4)]
- var rook_motion = [pos_around(Vector2.RIGHT), pos_around(Vector2.LEFT)]
- var king_moveto_spots = [Vector2.RIGHT, Vector2.LEFT] # O-O and O-O-O respectivel
- for i in range(len(rooks)):
- var rook = at_pos(rooks[i])
- if !rook is Rook:
- continue
- if rook.has_moved:
- continue
- var direction = king_moveto_spots[i]
- var posx2 = pos_around(direction * 2)
- var pos = pos_around(direction)
- if at_pos(posx2) or at_pos(pos):
- continue
- if checkcheck(posx2) or checkcheck(pos):
- continue
- can_castle.append([posx2, rook, rook_motion[i]])
- moves.append(posx2)
+ moves.append_array(castleing())
+ return moves
+
+
+func castleing():
+ var moves = []
+ var rooks = [pos_around(Vector2.RIGHT * 3), pos_around(Vector2.LEFT * 4)]
+ var rook_motion = [pos_around(Vector2.RIGHT), pos_around(Vector2.LEFT)]
+ var king_moveto_spots = [Vector2.RIGHT, Vector2.LEFT] # O-O and O-O-O respectivel
+ for i in range(len(rooks)):
+ var rook = at_pos(rooks[i])
+ if !rook is Rook:
+ continue
+ if rook.has_moved:
+ continue
+ var direction = king_moveto_spots[i]
+ var posx2 = pos_around(direction * 2)
+ var pos = pos_around(direction)
+ if at_pos(posx2) or at_pos(pos):
+ continue
+ if checkcheck(posx2) or checkcheck(pos):
+ continue
+ can_castle.append([posx2, rook, rook_motion[i]])
+ moves.append(posx2)
return moves
@@ -49,22 +55,13 @@ func _ready():
func can_move(): # checks if you can legally move
castle_check = false
- if get_moves().size() != 0 or get_attacks().size() != 0:
- castle_check = true
- return true
- castle_check = false
- return false
+ var can = can_move()
+ castle_check = true
+ return can
-func get_attacks(): # @Override
+func get_attacks():
castle_check = false
- 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
- if check_spots_check and checkcheck(i):
- continue
- final.append(i)
+ var final = .get_attacks()
castle_check = true
return final
diff --git a/pieces/Pawn.gd b/pieces/Pawn.gd
index 3e7e8d9..a183eb4 100644
--- a/pieces/Pawn.gd
+++ b/pieces/Pawn.gd
@@ -3,6 +3,30 @@ class_name Pawn, "res://assets/california/wP.png"
onready var whiteint = 1 if white else -1
+var twostepfirstmove = false
+var just_set = false
+var enpassant = []
+
+
+func moveto(position, real = true):
+ # 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
+ .moveto(position, real)
+
+
+func _on_turn_over():
+ if just_set:
+ just_set = false
+ return
+ if twostepfirstmove:
+ twostepfirstmove = false
+
func get_moves():
var points = [Vector2.UP, Vector2.UP * 2]
@@ -18,9 +42,15 @@ func get_moves():
continue
if is_on_board(point):
moves.append(point)
+ moves.append_array(en_passant())
return moves
+func passant(position):
+ enpassant.clear()
+ moveto(position)
+
+
func get_attacks():
var points = [Vector2.UP + Vector2.RIGHT, Vector2.UP + Vector2.LEFT]
var moves = []
@@ -34,9 +64,37 @@ func get_attacks():
continue
if at_pos(point) != null and at_pos(point).white != white:
moves.append(point)
+ moves.append_array(en_passant("attacks"))
+ return moves
+
+
+func en_passant(type: String = "moves"): # in passing
+ var passants = [pos_around(Vector2.LEFT), pos_around(Vector2.RIGHT)]
+ var moves = []
+ for i in passants:
+ if !is_on_board(i):
+ continue
+ var spot = at_pos(i)
+ if !spot:
+ continue
+ if spot.white == white:
+ continue
+ if !Utils.is_pawn(spot):
+ continue
+ if !spot.twostepfirstmove:
+ continue
+ if check_spots_check and checkcheck(i):
+ continue
+ if type == "moves":
+ var position = i + (Vector2.UP * whiteint)
+ if !at_pos(position):
+ moves.append(position)
+ elif type == "attacks":
+ enpassant.append([i + (Vector2.UP * whiteint), spot])
return moves
func _ready():
+ Events.connect("turn_over", self, "_on_turn_over")
._ready()
shortname = "p" + team
diff --git a/pieces/Piece.gd b/pieces/Piece.gd
index 9570ac1..bb9604b 100644
--- a/pieces/Piece.gd
+++ b/pieces/Piece.gd
@@ -38,7 +38,6 @@ func clear_clicked():
func move(newpos: Vector2): # dont use directly; use moveto
- has_moved = true
tween.interpolate_property(
self,
"global_position",
@@ -58,6 +57,7 @@ func moveto(position, real = true):
if real:
real_position = position
move(position)
+ has_moved = true
func pos_around(around_vector):
@@ -96,7 +96,7 @@ func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]):
return circle_array
-func at_pos(vector):
+func at_pos(vector: Vector2):
return Globals.grid.matrix[vector.y][vector.x]
@@ -174,6 +174,8 @@ func take(piece: Piece):
func took(): # called when piece is taken
+ print(shortname, "was killed")
+ Globals.grid.matrix[real_position.y][real_position.x] = null
anim.play("Take")
diff --git a/project.godot b/project.godot
index b53f041..6da6899 100644
--- a/project.godot
+++ b/project.godot
@@ -75,6 +75,7 @@ config/icon="res://icon.png"
Globals="*res://Globals.gd"
Events="*res://Events.gd"
+Utils="*res://Utils.gd"
[debug]