online multiplayer chess game (note server currently down)
bendn 2022-04-25
parent c2cd4d8 · commit 108e9a3
-rw-r--r--Grid.gd36
-rw-r--r--Piece.gd85
-rw-r--r--Piece.tscn8
-rw-r--r--World.tscn1
-rw-r--r--frame.png3
-rw-r--r--frame.png.import35
6 files changed, 136 insertions, 32 deletions
diff --git a/Grid.gd b/Grid.gd
index 48fe5d7..9f7a43c 100644
--- a/Grid.gd
+++ b/Grid.gd
@@ -144,24 +144,29 @@ func print_matrix_pretty(mat = matrix):
func check_for_circle(position: Vector2):
- var circle = background_matrix[position.x][position.y].circle.visible
- return circle
+ return background_matrix[position.x][position.y].circle.visible
+func check_for_frame(position:Vector2):
+ if !matrix[position.y][position.x]:
+ return false
+ return matrix[position.y][position.x].frame.visible
func square_clicked(position: Vector2):
var spot = matrix[position.y][position.x]
- if !spot or !spot.white:
- if !last_clicked: # its null
+ if !spot or !spot.white: # spot is not a tile or spot is not white
+ if !last_clicked: # last clicked is null, so this is pointless
return
- if check_for_circle(position):
- last_clicked.moveto(position)
- last_clicked.clear_clicked()
- last_clicked = null
- elif last_clicked != spot:
- if last_clicked:
+ if check_for_circle(position): # see if theres a circle at the position
+ last_clicked.moveto(position) # if there is, move there
+ if check_for_frame(position): # takeable
+ last_clicked.take(matrix[position.y][position.x]) # eat
+ last_clicked.clear_clicked() # remove the circles
+ last_clicked = null # set it to null
+ elif last_clicked != spot: # we got a new piece (or pawn) clicked
+ if last_clicked: # remove the circles
last_clicked.clear_clicked()
- last_clicked = spot
- spot.clicked()
+ last_clicked = spot # set it to the new spot
+ spot.clicked() # tell the piece shit happeend
func clear_circles():
@@ -169,3 +174,10 @@ func clear_circles():
for j in range(8):
var square = background_matrix[i][j]
square.set_circle(false)
+
+func clear_frames():
+ for i in range(8):
+ for j in range(8):
+ var square = matrix[i][j]
+ if square:
+ square.set_frame(false)
diff --git a/Piece.gd b/Piece.gd
index 56218ca..d4a2dc2 100644
--- a/Piece.gd
+++ b/Piece.gd
@@ -8,51 +8,100 @@ var has_moved = false
var sprite
onready var colorrect = $ColorRect
+onready var frame = $Frame
func _ready():
+ 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 move(newpos: Vector2):
- has_moved = true
- global_position = newpos * Globals.grid.piece_size
-
-
+
+
func clicked():
colorrect.show()
create_circles()
print(realname, " was clicked")
+
-
-func clear_clicked():
+func clear_clicked(): # TODO: fix this shit
colorrect.hide()
Globals.grid.clear_circles()
+ Globals.grid.clear_frames()
+
+func move(newpos: Vector2): # dont use directly; use moveto
+ has_moved = true
+ global_position = newpos * Globals.grid.piece_size
-
-func moveto(position): # called when already clicked, and clicked again
+func moveto(position):
Globals.grid.matrix[real_position.y][real_position.x] = null
Globals.grid.matrix[position.y][position.x] = self
real_position = position
move(position)
+func pos_around(around_vector):
+ return real_position + around_vector
+
func create_circles():
# for motion
match realname:
"pawn":
- var carry = [real_position - Vector2(0, 1)]
+ var carry = [pos_around(Vector2.UP)]
if !has_moved:
- carry.append(real_position - Vector2(0, 2))
+ carry.append(pos_around(Vector2(0, -2)))
set_circle(carry)
+ # deal with the take logic
+ carry = []
+ var takes = [pos_around(Vector2(-1, -1)), pos_around(Vector2(1, -1))]
+ for i in takes:
+ i = clamp_vector(i)
+ if i == null:
+ continue
+ var pos = Globals.grid.matrix[i.y][i.x]
+ if pos and !pos.white:
+ carry.append(i)
+ print("takeable: ", carry)
+ set_circle(carry, "take")
+ "king":
+ var carry = [pos_around(Vector2.UP), pos_around(Vector2.DOWN), pos_around(Vector2.LEFT), pos_around(Vector2.RIGHT)]
+ # add diagonals
+ carry.append(pos_around(Vector2(-1, -1)))
+ carry.append(pos_around(Vector2(1, -1)))
+ carry.append(pos_around(Vector2(-1, 1)))
+ carry.append(pos_around(Vector2(1, 1)))
+ set_circle(carry)
+ set_circle(carry, "take") # king ez
-
-func set_circle(positions: Array):
+func set_circle(positions: Array, type: = "move"):
for i in range(len(positions)):
- var pos = positions[i]
- if Globals.grid.matrix[pos.y][pos.x]:
- print(Globals.grid.matrix[pos.y][pos.x], " is in the way")
+ var pos = clamp_vector(positions[i])
+ if pos == null:
+ continue
+ var spot = Globals.grid.matrix[pos.y][pos.x]
+ if spot and type == "move":
continue
- print("creating circle at", pos)
- Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)
+ if type == "move":
+ # print("creating move circle at", pos)
+ Globals.grid.background_matrix[pos.x][pos.y].set_circle(true)
+ elif type == "take":
+ print("creating take circle at", pos)
+ if spot and !spot.white:
+ spot.set_frame(true)
+
+func set_frame(boolean):
+ frame.visible = boolean
+
+func clamp_vector(vector :Vector2):
+ if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7:
+ return null
+ vector.x = clamp(vector.x, 0, 7)
+ vector.y = clamp(vector.y, 0, 7)
+ return vector
+
+func take(piece:Piece):
+ var piecepos = piece.real_position
+ piece.queue_free()
+ moveto(piecepos)
diff --git a/Piece.tscn b/Piece.tscn
index 12161c4..5fc374c 100644
--- a/Piece.tscn
+++ b/Piece.tscn
@@ -1,7 +1,8 @@
-[gd_scene load_steps=3 format=2]
+[gd_scene load_steps=4 format=2]
[ext_resource path="res://Piece.gd" type="Script" id=1]
[ext_resource path="res://assets/california/wP.png" type="Texture" id=2]
+[ext_resource path="res://frame.png" type="Texture" id=3]
[node name="Piece" type="Node2D"]
script = ExtResource( 1 )
@@ -15,3 +16,8 @@ color = Color( 0.2, 0.345098, 0.188235, 0.592157 )
[node name="Sprite" type="Sprite" parent="."]
position = Vector2( 50, 50 )
texture = ExtResource( 2 )
+
+[node name="Frame" type="Sprite" parent="."]
+visible = false
+position = Vector2( 50, 50 )
+texture = ExtResource( 3 )
diff --git a/World.tscn b/World.tscn
index cb50f55..3e33970 100644
--- a/World.tscn
+++ b/World.tscn
@@ -8,6 +8,5 @@
script = ExtResource( 1 )
board_color1 = Color( 0.870588, 0.890196, 0.901961, 1 )
board_color2 = Color( 0.54902, 0.635294, 0.678431, 1 )
-overlay_color = Color( 0.2, 0.345098, 0.188235, 0.592157 )
[node name="Background" type="Node2D" parent="Grid"]
diff --git a/frame.png b/frame.png
new file mode 100644
index 0000000..2b5f67a
--- /dev/null
+++ b/frame.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:5a50b12d0ec7c7facdd7000584712e4337f47b7574a6c60451e76dbfa8626e9f
+size 667
diff --git a/frame.png.import b/frame.png.import
new file mode 100644
index 0000000..92cdb3b
--- /dev/null
+++ b/frame.png.import
@@ -0,0 +1,35 @@
+[remap]
+
+importer="texture"
+type="StreamTexture"
+path="res://.import/frame.png-3ac25dcfa39cb943440d117e36cbfa2f.stex"
+metadata={
+"vram_texture": false
+}
+
+[deps]
+
+source_file="res://frame.png"
+dest_files=[ "res://.import/frame.png-3ac25dcfa39cb943440d117e36cbfa2f.stex" ]
+
+[params]
+
+compress/mode=0
+compress/lossy_quality=0.7
+compress/hdr_mode=0
+compress/bptc_ldr=0
+compress/normal_map=0
+flags/repeat=0
+flags/filter=false
+flags/mipmaps=false
+flags/anisotropic=false
+flags/srgb=2
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/HDR_as_SRGB=false
+process/invert_color=false
+process/normal_map_invert_y=false
+stream=false
+size_limit=0
+detect_3d=false
+svg/scale=1.0