online multiplayer chess game (note server currently down)
testing
bendn 2022-04-26
parent 97f36b5 · commit 5a72319
-rw-r--r--Globals.gd4
-rw-r--r--Grid.gd17
-rw-r--r--Piece.gd64
-rw-r--r--World.tscn1
-rw-r--r--project.godot1
5 files changed, 67 insertions, 20 deletions
diff --git a/Globals.gd b/Globals.gd
index daab65b..6d449f0 100644
--- a/Globals.gd
+++ b/Globals.gd
@@ -2,7 +2,7 @@ extends Node
var grid: Grid = null
var turns := 0
-var black_incheck = false
-var white_incheck = false
+var in_check = false
+var checking_piece: Piece = null
var turn := true # true for white, false for black
# true cuz white goes first
diff --git a/Grid.gd b/Grid.gd
index 987d7f0..dda6eee 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -30,12 +30,23 @@ func _ready():
func _on_turn_over():
+ Globals.checking_piece = null
+ Globals.in_check = false
+ check_in_check()
+
+
+func check_in_check(real = true):
for i in range(0, 8):
for j in range(0, 8):
var spot = matrix[i][j]
if spot and spot.white != Globals.turn: # enemie
if matrix[i][j].create_circles(false):
- print("woaw")
+ Globals.in_check = true
+ Globals.checking_piece = matrix[i][j]
+ print("check")
+ if !real:
+ return true
+ return false
func _exit_tree():
@@ -135,9 +146,9 @@ func print_matrix_pretty(mat = matrix):
var c = r[i]
var ender = ", " if i < 7 else ""
if c:
- row += c.realname + ender
+ row += c.shortname + ender
else:
- row += "null" + ender
+ row += "NA" + ender
print(row + "],")
print("]")
diff --git a/Piece.gd b/Piece.gd
index b9e4418..348bacc 100644
--- a/Piece.gd
+++ b/Piece.gd
@@ -4,12 +4,11 @@ class_name Piece, "res://assets/california/wP.png"
var real_position = Vector2.ZERO
var white := true
var realname = "pawn"
+var shortname = ""
var has_moved = false
var sprite
var frameon
-
-
onready var tween = $Tween
onready var colorrect = $ColorRect
onready var frame = $Frame
@@ -20,6 +19,24 @@ func _ready():
frame.modulate = Globals.grid.overlay_color
colorrect.color = Globals.grid.overlay_color
colorrect.rect_size = Globals.grid.piece_size
+ var wh = "w" if white else "b"
+ shortname = short_name().to_lower() + wh.to_upper()
+
+
+func short_name():
+ match realname:
+ "pawn":
+ return "P"
+ "rook":
+ return "R"
+ "knight":
+ return "N"
+ "bishop":
+ return "B"
+ "queen":
+ return "Q"
+ "king":
+ return "K"
func clicked():
@@ -49,14 +66,15 @@ func move(newpos: Vector2): # dont use directly; use moveto
# global_position = newpos * Globals.grid.piece_size
-func moveto(position):
+func moveto(position, real = true):
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
- Events.emit_signal("turn_over")
+ if real:
+ real_position = position
+ move(position)
+ Globals.turn = not Globals.turn
+ Globals.turns += 1
+ Events.emit_signal("turn_over")
func pos_around(around_vector):
@@ -160,7 +178,7 @@ func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]):
break
if at_pos(pos) != null:
carry.append(pos)
- break;
+ break
carry.append(pos)
return carry
@@ -178,26 +196,43 @@ func set_circle(positions: Array, type := "move", real = true):
if type == "move":
if spot:
continue
- Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)
+ if checkcheck(pos):
+ Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)
elif type == "take":
var team = Globals.turn if real else !Globals.turn
if spot and spot.white != team:
+ if Globals.in_check and spot != Globals.checking_piece:
+ return
spot.set_frame(true)
if spot.realname == "king":
if real:
printerr("shit")
else:
- print("chec")
return true
return false
+func checkcheck(pos):
+ if Globals.in_check:
+ var mat = Globals.grid.matrix.duplicate(true)
+ moveto(pos, false)
+ print("moved " + realname + " to " + str(pos))
+ var retu = true
+ if !Globals.grid.check_in_check(false):
+ print("did not fix check")
+ retu = false
+ Globals.grid.print_matrix_pretty(mat)
+ Globals.grid.matrix = mat
+ return retu
+ return true
+
+
func pd(string, toprint):
if toprint:
print(string)
-func is_on_board(vector: Vector2) -> bool:
+func is_on_board(vector: Vector2): #-> bool: my syntax highlight doesnt like return annotation
if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7:
return false
return true
@@ -207,8 +242,9 @@ func take(piece: Piece):
var piecepos = piece.real_position
piece.queue_free()
moveto(piecepos)
-
-func set_frame(value, real=true):
+
+
+func set_frame(value, real = true):
frameon = value
if real:
frame.visible = value
diff --git a/World.tscn b/World.tscn
index d43dfb8..2a5f680 100644
--- a/World.tscn
+++ b/World.tscn
@@ -6,6 +6,5 @@
[node name="Grid" type="Node2D" parent="."]
script = ExtResource( 1 )
-board_color1 = Color( 0.870588, 0.890196, 0.901961, 1 )
[node name="Background" type="Node2D" parent="Grid"]
diff --git a/project.godot b/project.godot
index dcfea19..532f2e0 100644
--- a/project.godot
+++ b/project.godot
@@ -87,4 +87,5 @@ debug={
[rendering]
+quality/driver/driver_name="GLES2"
environment/default_clear_color=Color( 0.301961, 0.301961, 0.301961, 1 )