online multiplayer chess game (note server currently down)
| -rw-r--r-- | Grid.gd | 28 | ||||
| -rw-r--r-- | Piece.gd | 133 | ||||
| -rw-r--r-- | project.godot | 6 |
3 files changed, 127 insertions, 40 deletions
@@ -1,4 +1,5 @@ extends Node2D +class_name Grid export var PIECE_SET = "california" @@ -146,27 +147,29 @@ func print_matrix_pretty(mat = matrix): func check_for_circle(position: Vector2): return background_matrix[position.x][position.y].circle.visible -func check_for_frame(position:Vector2): + +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: # spot is not a tile or spot is not white + 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): # 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 + 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 # set it to the new spot - spot.clicked() # tell the piece shit happeend + last_clicked = spot # set it to the new spot + spot.clicked() # tell the piece shit happeend func clear_circles(): @@ -175,6 +178,7 @@ func clear_circles(): var square = background_matrix[i][j] square.set_circle(false) + func clear_frames(): for i in range(8): for j in range(8): @@ -7,6 +7,8 @@ var realname = "pawn" var has_moved = false var sprite +var black_holder + onready var colorrect = $ColorRect onready var frame = $Frame @@ -18,23 +20,23 @@ func _ready(): colorrect.rect_size = Globals.grid.piece_size - - func clicked(): colorrect.show() create_circles() print(realname, " was clicked") - -func clear_clicked(): # TODO: fix this shit + +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 + + +func move(newpos: Vector2): # dont use directly; use moveto has_moved = true global_position = newpos * Globals.grid.piece_size + func moveto(position): Globals.grid.matrix[real_position.y][real_position.x] = null Globals.grid.matrix[position.y][position.x] = self @@ -45,13 +47,29 @@ func moveto(position): func pos_around(around_vector): return real_position + around_vector + +func all_dirs(): + return [ + Vector2.UP, + Vector2.DOWN, + Vector2.LEFT, + Vector2.RIGHT, + Vector2(1, 1), + Vector2(1, -1), + Vector2(-1, 1), + Vector2(-1, -1) + ] + + func create_circles(): # for motion match realname: "pawn": - var carry = [pos_around(Vector2.UP)] - if !has_moved: - carry.append(pos_around(Vector2(0, -2))) + var carry = ( + [pos_around(Vector2.UP)] + if has_moved + else [pos_around(Vector2.UP), pos_around(Vector2.UP * 2)] + ) set_circle(carry) # deal with the take logic carry = [] @@ -60,48 +78,107 @@ func create_circles(): 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) + carry.append(i) 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))) + var carry = [ + pos_around(Vector2.UP), + pos_around(Vector2.DOWN), + pos_around(Vector2.LEFT), + pos_around(Vector2.RIGHT), + pos_around(Vector2(1, 1)), + pos_around(Vector2(1, -1)), + pos_around(Vector2(-1, 1)), + pos_around(Vector2(-1, -1)) + ] + set_circle(carry) + set_circle(carry, "take") # king ez + "knight": + var carry = [ + pos_around(Vector2(-2, -1)), + pos_around(Vector2(-2, 1)), + pos_around(Vector2(2, -1)), + pos_around(Vector2(2, 1)), + pos_around(Vector2(-1, -2)), + pos_around(Vector2(1, -2)), + pos_around(Vector2(-1, 2)), + pos_around(Vector2(1, 2)) + ] + set_circle(carry) + set_circle(carry, "take") + "rook": + var carry = traverse(all_dirs().slice(0, 4)) + set_circle(carry) + set_circle(carry, "take") + "bishop": + var carry = traverse(all_dirs().slice(4, 8)) + set_circle(carry) + set_circle(carry, "take") + "queen": + var carry = traverse(all_dirs()) set_circle(carry) - set_circle(carry, "take") # king ez + set_circle(carry, "take") + + +func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]): + var carry = [] + for i in arr: + black_holder = false + var pos = real_position + while true: + pos = pos + i + pos = clamp_vector(pos) + if !traverse_helper(pos): + break + carry.append(pos) + black_holder = false + return carry + + +func traverse_helper(pos): + if pos == null: + return null + pos = at_pos(pos) + if pos: + if !pos.white and !black_holder: + black_holder = true + return true + return null + return true -func set_circle(positions: Array, type: = "move"): + +func at_pos(vector): + return Globals.grid.matrix[vector.y][vector.x] + + +func set_circle(positions: Array, type := "move"): for i in range(len(positions)): 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 + var spot = at_pos(pos) if type == "move": - # print("creating move circle at", pos) + if spot: + continue 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): + +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): + +func take(piece: Piece): var piecepos = piece.real_position piece.queue_free() moveto(piecepos) diff --git a/project.godot b/project.godot index 28ac956..03c9fa1 100644 --- a/project.godot +++ b/project.godot @@ -10,11 +10,17 @@ config_version=4 _global_script_classes=[ { "base": "Node2D", +"class": "Grid", +"language": "GDScript", +"path": "res://Grid.gd" +}, { +"base": "Node2D", "class": "Piece", "language": "GDScript", "path": "res://Piece.gd" } ] _global_script_class_icons={ +"Grid": "", "Piece": "res://assets/california/wP.png" } |