online multiplayer chess game (note server currently down)
mostly working
| -rw-r--r-- | Globals.gd | 2 | ||||
| -rw-r--r-- | Grid.gd | 75 | ||||
| -rw-r--r-- | Piece.gd | 267 | ||||
| -rw-r--r-- | Piece.tscn | 56 | ||||
| -rw-r--r-- | Square.gd | 1 | ||||
| -rw-r--r-- | pieces/Bishop.gd | 11 | ||||
| -rw-r--r-- | pieces/King.gd | 25 | ||||
| -rw-r--r-- | pieces/Knight.gd | 28 | ||||
| -rw-r--r-- | pieces/Pawn.gd | 41 | ||||
| -rw-r--r-- | pieces/Piece.gd | 180 | ||||
| -rw-r--r-- | pieces/Queen.gd | 11 | ||||
| -rw-r--r-- | pieces/Rook.gd | 11 | ||||
| -rw-r--r-- | project.godot | 40 |
13 files changed, 440 insertions, 308 deletions
@@ -4,5 +4,7 @@ var grid: Grid = null var turns := 0 var in_check = false var checking_piece: Piece = null +var white_king: King +var black_king: King var turn := true # true for white, false for black # true cuz white goes first @@ -41,11 +41,10 @@ func check_in_check(): # check if in_check for j in range(0, 8): # for each column var spot = matrix[i][j] # get the square if spot and spot.white != Globals.turn: # enemie - print("checking", spot.realname) # print the piece - if matrix[i][j].create_circles(false): # if it can take the king + if matrix[i][j].can_check_king(Globals.white_king if Globals.turn else Globals.black_king): # if it can take the king Globals.in_check = true # set in_check Globals.checking_piece = matrix[i][j] # set checking_piece - print("check") # print the check + print("check by " + spot.shortname) # print the check return true return false # not in check @@ -62,14 +61,13 @@ func init_matrix(): # create the matrix add_pieces() # add the pieces -func make_piece(position: Vector2, name: String, sprite: String, white: bool = true): # make peace +func make_piece(position: Vector2, script: String, sprite: String, white: bool = true): # make peace var piece = Piece.instance() # create a piece + piece.script = load(script) # set the script piece.sprite = piece.get_node("Sprite") # get the sprite piece.sprite.texture = load(sprite) # set the sprite piece.real_position = position # set the real position piece.global_position = position * piece_size # set the global position - piece.realname = name # set the real name - piece.name = name # set the name piece.white = white # set its team add_child(piece) # add the piece to the grid return piece # return the piece @@ -82,7 +80,7 @@ func init_board(): # create the board var square = Square.instance() # create a square square.rect_size = piece_size # set the size square.rect_position = Vector2(i, j) * piece_size # set the position - square.realname = "square_" + str(i) + "_" + str(j) # set the real name + square.name = "square_" + str(i) + "_" + str(j) # set the real name square.color = board_color1 if (i + j) % 2 == 0 else board_color2 # set the color square.real_position = Vector2(i, j) # set the real position background.add_child(square) # add the square to the background @@ -102,39 +100,41 @@ func add_pieces(): # add the pieces func add_pawns(): for i in range(8): - matrix[1][i] = make_piece(Vector2(i, 1), "pawn", ASSETS_PATH + "bP.png", false) - matrix[6][i] = make_piece(Vector2(i, 6), "pawn", ASSETS_PATH + "wP.png", true) + matrix[1][i] = make_piece(Vector2(i, 1), "res://pieces/Pawn.gd", ASSETS_PATH + "bP.png", false) + matrix[6][i] = make_piece(Vector2(i, 6), "res://pieces/Pawn.gd", ASSETS_PATH + "wP.png", true) func add_rooks(): - matrix[0][0] = make_piece(Vector2(0, 0), "rook", ASSETS_PATH + "bR.png", false) - matrix[0][7] = make_piece(Vector2(7, 0), "rook", ASSETS_PATH + "bR.png", false) - matrix[7][0] = make_piece(Vector2(0, 7), "rook", ASSETS_PATH + "wR.png", true) - matrix[7][7] = make_piece(Vector2(7, 7), "rook", ASSETS_PATH + "wR.png", true) + matrix[0][0] = make_piece(Vector2(0, 0), "res://pieces/Rook.gd", ASSETS_PATH + "bR.png", false) + matrix[0][7] = make_piece(Vector2(7, 0), "res://pieces/Rook.gd", ASSETS_PATH + "bR.png", false) + matrix[7][0] = make_piece(Vector2(0, 7), "res://pieces/Rook.gd", ASSETS_PATH + "wR.png", true) + matrix[7][7] = make_piece(Vector2(7, 7), "res://pieces/Rook.gd", ASSETS_PATH + "wR.png", true) func add_knights(): - matrix[0][1] = make_piece(Vector2(1, 0), "knight", ASSETS_PATH + "bN.png", false) - matrix[0][6] = make_piece(Vector2(6, 0), "knight", ASSETS_PATH + "bN.png", false) - matrix[7][1] = make_piece(Vector2(1, 7), "knight", ASSETS_PATH + "wN.png", true) - matrix[7][6] = make_piece(Vector2(6, 7), "knight", ASSETS_PATH + "wN.png", true) + matrix[0][1] = make_piece(Vector2(1, 0), "res://pieces/Knight.gd", ASSETS_PATH + "bN.png", false) + matrix[0][6] = make_piece(Vector2(6, 0), "res://pieces/Knight.gd", ASSETS_PATH + "bN.png", false) + matrix[7][1] = make_piece(Vector2(1, 7), "res://pieces/Knight.gd", ASSETS_PATH + "wN.png", true) + matrix[7][6] = make_piece(Vector2(6, 7), "res://pieces/Knight.gd", ASSETS_PATH + "wN.png", true) func add_bishops(): - matrix[0][2] = make_piece(Vector2(2, 0), "bishop", ASSETS_PATH + "bB.png", false) - matrix[0][5] = make_piece(Vector2(5, 0), "bishop", ASSETS_PATH + "bB.png", false) - matrix[7][2] = make_piece(Vector2(2, 7), "bishop", ASSETS_PATH + "wB.png", true) - matrix[7][5] = make_piece(Vector2(5, 7), "bishop", ASSETS_PATH + "wB.png", true) + matrix[0][2] = make_piece(Vector2(2, 0), "res://pieces/Bishop.gd", ASSETS_PATH + "bB.png", false) + matrix[0][5] = make_piece(Vector2(5, 0), "res://pieces/Bishop.gd", ASSETS_PATH + "bB.png", false) + matrix[7][2] = make_piece(Vector2(2, 7), "res://pieces/Bishop.gd", ASSETS_PATH + "wB.png", true) + matrix[7][5] = make_piece(Vector2(5, 7), "res://pieces/Bishop.gd", ASSETS_PATH + "wB.png", true) func add_queens(): - matrix[0][3] = make_piece(Vector2(3, 0), "queen", ASSETS_PATH + "bQ.png", false) - matrix[7][3] = make_piece(Vector2(3, 7), "queen", ASSETS_PATH + "wQ.png", true) + matrix[0][3] = make_piece(Vector2(3, 0), "res://pieces/Queen.gd", ASSETS_PATH + "bQ.png", false) + matrix[7][3] = make_piece(Vector2(3, 7), "res://pieces/Queen.gd", ASSETS_PATH + "wQ.png", true) func add_kings(): - matrix[0][4] = make_piece(Vector2(4, 0), "king", ASSETS_PATH + "bK.png", false) - matrix[7][4] = make_piece(Vector2(4, 7), "king", ASSETS_PATH + "wK.png", true) + matrix[0][4] = make_piece(Vector2(4, 0), "res://pieces/King.gd", ASSETS_PATH + "bK.png", false) + matrix[7][4] = make_piece(Vector2(4, 7), "res://pieces/King.gd", ASSETS_PATH + "wK.png", true) + Globals.white_king = matrix[7][4] # set the white king + Globals.black_king = matrix[0][4] # set the black king func print_matrix_pretty(mat = matrix): # print the matrix @@ -167,10 +167,12 @@ func square_clicked(position: Vector2): # square clicked if !spot or spot.white != Globals.turn: # spot is not a tile or spot is not turn color 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 + turn_over() + if check_for_circle(position): # see if theres a circle at the position + last_clicked.moveto(position) # if there is, move there + turn_over() 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 @@ -180,19 +182,20 @@ func square_clicked(position: Vector2): # square clicked spot.clicked() # tell the piece shit happeend -func clear_circles(): # clear the circles - for i in range(8): # for each row - for j in range(8): # for each column - var square = background_matrix[i][j] # get the square - square.set_circle(false) # set the circle to false +func turn_over(): + Globals.turn = not Globals.turn + Globals.turns += 1 + Events.emit_signal("turn_over") -func clear_frames(): # clear the frames +func clear_fx(): # clear the circles for i in range(8): # for each row for j in range(8): # for each column - var square = matrix[i][j] # get the square - if square: # if there is a piece - square.set_frame(false) # set the frame to false + var square = background_matrix[i][j] # get the square + square.set_circle(false) # set the circle to false + var piece = matrix[i][j] # get the piece + if piece: # if there is a piece + piece.set_frame(false) # clear the frame func _input(event): # input diff --git a/Piece.gd b/Piece.gd deleted file mode 100644 index b4de113..0000000 --- a/Piece.gd +++ /dev/null @@ -1,267 +0,0 @@ -extends Node2D -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 - - -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 - 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(): - colorrect.show() - create_circles() - print(realname, " was 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 - tween.interpolate_property( - self, - "global_position", - global_position, - newpos * Globals.grid.piece_size, - 0.5, - Tween.TRANS_BACK, - Tween.EASE_IN_OUT - ) - tween.start() - # global_position = newpos * Globals.grid.piece_size - - -func moveto(position, real = true): - Globals.grid.matrix[real_position.y][real_position.x] = null - Globals.grid.matrix[position.y][position.x] = self - 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): - 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 reality(circle_array, real): - if real: - set_circle(circle_array) - set_circle(circle_array, "take") - else: - var result = set_circle(circle_array, "take", false) - return result # checking if king is takeable - - -func create_circles(real = true): - # for motion - match realname: - "pawn": - var circle_array = ( - [pos_around(Vector2.UP)] - if has_moved - else [pos_around(Vector2.UP), pos_around(Vector2.UP * 2)] - ) - if !white: - circle_array = ( - [pos_around(Vector2.DOWN)] - if has_moved - else [pos_around(Vector2.DOWN), pos_around(Vector2.DOWN * 2)] - ) - if real: - set_circle(circle_array) - # deal with the take logic - circle_array = [] - var takes = [pos_around(Vector2(-1, -1)), pos_around(Vector2(1, -1))] - if !white: - takes = [pos_around(Vector2(-1, 1)), pos_around(Vector2(1, 1))] - for i in takes: - if not is_on_board(i): - continue - circle_array.append(i) - if real: - set_circle(circle_array, "take") - else: - return set_circle(circle_array, "take", false) - "king": - var circle_array = [ - 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)) - ] - return reality(circle_array, real) - "knight": - var circle_array = [ - 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)) - ] - return reality(circle_array, real) - "rook": - var circle_array = traverse(all_dirs().slice(0, 4)) - return reality(circle_array, real) - "bishop": - var circle_array = traverse(all_dirs().slice(4, 8)) - return reality(circle_array, real) - "queen": - # debug with queen - print("queen here!") - print("my real position is") - print(real_position) - var circle_array = traverse(all_dirs()) - var check_king = reality(circle_array, real) - print("yes" if check_king else "no") - return check_king - - -func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]): - var circle_array = [] - for i in arr: - var pos = real_position - while true: - pos += i - if not is_on_board(pos): - break - if at_pos(pos) != null: - circle_array.append(pos) - break - circle_array.append(pos) - return circle_array - - -func at_pos(vector): - return Globals.grid.matrix[vector.y][vector.x] - - -func create_move_circles(spot, pos): - if spot: # if there is a piece at the spot - return false # we cant move onto a piece - if checkcheck(pos): # if moving to position fixes the check or were not in check - Globals.grid.background_matrix[pos.x][pos.y].set_circle(true) # make the move circle - - -func create_take_circles(spot, real): # create take circles - var team = Globals.turn if real else !Globals.turn # is your team, or, if testing with !real, opponents team - if spot and spot.white != team: # if spot is not null and is not your team - if Globals.in_check and spot != Globals.checking_piece: # if you are in check and the spot is not the piece that is checking you - return # it isnt going to fix the check, so return - spot.set_frame(real) # turn on the little take frame on the piece, to show its takeable - if spot.realname == "king": # if the piece is a king - if real: # and its not a test - printerr("shit") # we fucked up - else: - return true # it is a test for if in check, so return true - return false # nothing happened, so return false - - -func set_circle(positions: Array, type := "move", real = true): - for pos in positions: - if not is_on_board(pos): # check if the position is on the boards - continue # if it isnt, continue - - var spot = at_pos(pos) # get the piece at the position - if type == "move": # if the type is move - if !create_move_circles(spot, pos): # create the move circles - continue # if the spot is not null, continue instead of creating circles - elif type == "take": # if the type is take - if create_take_circles(spot, real) == true and !real: # if the king is in check, return true - return true - return false # if nothing happened, return false - - -func checkcheck(pos): - if Globals.in_check: # if you are in check - var mat = Globals.grid.matrix.duplicate(true) # make a copy of the matrix - moveto(pos, false) # move to the position - print("moved " + realname + " to " + str(pos)) - var retu = true # return true by default - if !Globals.grid.check_in_check(false): # if you are still in check - print("did not fix check") # sadge - retu = false # return false, but fix the matrix first - Globals.grid.print_matrix_pretty(mat) # print the matrix - Globals.grid.matrix = mat # revert changes on the matrix - return retu - return true - - -func pd(string, toprint): - if toprint: - print(string) - - -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 - - -func take(piece: Piece): - var piecepos = piece.real_position - piece.queue_free() - moveto(piecepos) - - -func set_frame(value, real = true): - frameon = value - if real: - frame.visible = value @@ -1,9 +1,57 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] -[ext_resource path="res://Piece.gd" type="Script" id=1] +[ext_resource path="res://pieces/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] +[sub_resource type="Animation" id=1] +resource_name = "Move" +length = 0.5 +step = 0.05 +tracks/0/type = "value" +tracks/0/path = NodePath("Sprite:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 0.25, 0.5 ), +"transitions": PoolRealArray( 2, 1, 0.5 ), +"update": 0, +"values": [ Vector2( 1, 1 ), Vector2( 1.15, 1.15 ), Vector2( 1, 1 ) ] +} + +[sub_resource type="Animation" id=2] +resource_name = "Take" +length = 0.5 +step = 0.05 +tracks/0/type = "value" +tracks/0/path = NodePath("Sprite:scale") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 0.1, 0.15, 0.35, 0.5 ), +"transitions": PoolRealArray( 2, 1, 1, 1, 0.5 ), +"update": 0, +"values": [ Vector2( 1, 1 ), Vector2( 1.25, 1.25 ), Vector2( 1.35, 1.35 ), Vector2( 0.25, 0.25 ), Vector2( 0, 0 ) ] +} +tracks/1/type = "method" +tracks/1/path = NodePath(".") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/keys = { +"times": PoolRealArray( 0.5 ), +"transitions": PoolRealArray( 1 ), +"values": [ { +"args": [ ], +"method": "queue_free" +} ] +} + [node name="Piece" type="Node2D"] script = ExtResource( 1 ) @@ -23,3 +71,7 @@ position = Vector2( 50, 50 ) texture = ExtResource( 3 ) [node name="Tween" type="Tween" parent="."] + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +anims/Move = SubResource( 1 ) +anims/Take = SubResource( 2 ) @@ -1,6 +1,5 @@ extends ColorRect -var realname = "Square" var real_position = Vector2() var circle_on = false diff --git a/pieces/Bishop.gd b/pieces/Bishop.gd new file mode 100644 index 0000000..e542ebf --- /dev/null +++ b/pieces/Bishop.gd @@ -0,0 +1,11 @@ +extends Piece +class_name Bishop + + +func get_moves(): + return .traverse(.all_dirs().slice(4, 8)) + + +func _ready(): + ._ready() + shortname = "b" + team diff --git a/pieces/King.gd b/pieces/King.gd new file mode 100644 index 0000000..4c2c9d1 --- /dev/null +++ b/pieces/King.gd @@ -0,0 +1,25 @@ +extends Piece +class_name King + + +func get_moves(): + var moves = [ + .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)) + ] + var final = [] + for i in moves: + if .is_on_board(i): + final.append(i) + return final + + +func _ready(): + ._ready() + shortname = "k" + team diff --git a/pieces/Knight.gd b/pieces/Knight.gd new file mode 100644 index 0000000..2ba7277 --- /dev/null +++ b/pieces/Knight.gd @@ -0,0 +1,28 @@ +class_name Knight +extends Piece + + +func get_moves(): + var moves = [ + 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)) + ] + var final = [] + for i in moves: + if is_on_board(i): + if check_spots_check: + if !checkcheck(i): + continue + final.append(i) + return final + + +func _ready(): + ._ready() + shortname = "k" + team diff --git a/pieces/Pawn.gd b/pieces/Pawn.gd new file mode 100644 index 0000000..55892bc --- /dev/null +++ b/pieces/Pawn.gd @@ -0,0 +1,41 @@ +extends Piece +class_name Pawn + + +func get_moves(): + var points = [Vector2.UP, Vector2.UP * 2] + var whiteint = 1 if white else -1 + var moves = [] + for i in range(len(points)): + var point = points[i] + point *= whiteint + point = pos_around(point) + if at_pos(point) == null: + if i == 1 and has_moved or at_pos(pos_around(points[0] * whiteint)) != null: + continue + if check_spots_check: + if !checkcheck(point): + continue + if is_on_board(point): + moves.append(point) + return moves + + +func get_attacks(): + var points = [Vector2.UP + Vector2.RIGHT, Vector2.UP + Vector2.LEFT] + var whiteint = int(white) + var moves = [] + for i in range(len(points)): + var point = points[i] + point *= whiteint + point = pos_around(point) + if !is_on_board(point): + continue + if at_pos(point) != null and at_pos(point).white != white: + moves.append(point) + return moves + + +func _ready(): + ._ready() + shortname = "p" + team diff --git a/pieces/Piece.gd b/pieces/Piece.gd new file mode 100644 index 0000000..676c37f --- /dev/null +++ b/pieces/Piece.gd @@ -0,0 +1,180 @@ +extends Node2D +class_name Piece, "res://assets/california/wP.png" + +var real_position = Vector2.ZERO +var white := true +var shortname = "" +var has_moved = false +var sprite +var frameon +var team = "w" +var check_spots_check = true + +onready var tween = $Tween +onready var anim = $AnimationPlayer +onready var colorrect = $ColorRect +onready var frame = $Frame + + +func _ready(): + team = "W" if white else "B" + 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 clicked(): + colorrect.show() + set_circle(get_moves()) + set_circle(get_attacks(), "take") + print(shortname, " was clicked") + + +func clear_clicked(): + colorrect.hide() + Globals.grid.clear_fx() + + +func move(newpos: Vector2): # dont use directly; use moveto + has_moved = true + tween.interpolate_property( + self, + "global_position", + global_position, + newpos * Globals.grid.piece_size, + 0.5, + Tween.TRANS_BACK, + Tween.EASE_IN_OUT + ) + anim.play("Move") + tween.start() + # global_position = newpos * Globals.grid.piece_size + + +func moveto(position, real = true): + Globals.grid.matrix[real_position.y][real_position.x] = null + Globals.grid.matrix[position.y][position.x] = self + if real: + real_position = position + move(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 traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]): + var circle_array = [] + for i in arr: + var pos = real_position + while true: + pos += i + if !is_on_board(pos): + break + + if at_pos(pos) != null: # only one black + if check_spots_check: + if checkcheck(pos): + circle_array.append(pos) + break + break + break + if check_spots_check: + if !checkcheck(pos): + continue + circle_array.append(pos) + return circle_array + + +func at_pos(vector): + return Globals.grid.matrix[vector.y][vector.x] + + +func get_moves(): # @Override + pass + + +func get_attacks(): # @Override + var moves = get_moves() # assumes the attacks are same as moves + var final = [] + for i in moves: + if at_pos(i) != null and at_pos(i).white != white: + final.append(i) + return final + + +func can_check_king(king): + check_spots_check = false + for attackable in get_attacks(): + if at_pos(attackable) == king: + check_spots_check = true + return true + check_spots_check = true + return false + + +func create_move_circles(pos): + Globals.grid.background_matrix[pos.x][pos.y].set_circle(true) # make the move circle + + +func create_take_circles(spot): # create take circles + spot.set_frame(true) # turn on the little take frame on the piece, to show its takeable + + +func set_circle(positions: Array, type := "move"): + for pos in positions: + var spot = at_pos(pos) # get the piece at the position + if type == "move": # if the type is move + create_move_circles(pos) # create the move circle + elif type == "take": # if the type is take + create_take_circles(spot) # if the king is in check, return true + + +func checkcheck(pos): # moves to position, then checks if your king is in check + if Globals.in_check: # if you are in check + var mat = Globals.grid.matrix.duplicate(true) # make a copy of the matrix + moveto(pos, false) # move to the position + print("moved " + shortname + " to " + str(pos)) + var retu = true # return true by default + if !Globals.grid.check_in_check(): # if you are still in check + print("did not fix check") # sadge + retu = false # return false, but fix the matrix first + # Globals.grid.print_matrix_pretty(mat) # print the matrix + Globals.grid.matrix = mat # revert changes on the matrix + return retu + return true + + +func is_on_board(vector: Vector2): + if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7: + return false + return true + + +func take(piece: Piece): + clear_clicked() + piece.took() + moveto(piece.real_position) + + +func took(): # called when piece is taken + anim.play("Take") + + +func set_frame(value): + frameon = value + frame.visible = value diff --git a/pieces/Queen.gd b/pieces/Queen.gd new file mode 100644 index 0000000..8fdb900 --- /dev/null +++ b/pieces/Queen.gd @@ -0,0 +1,11 @@ +extends Piece +class_name Queen + + +func get_moves(): + return .traverse(.all_dirs()) + + +func _ready(): + ._ready() + shortname = "q" + team diff --git a/pieces/Rook.gd b/pieces/Rook.gd new file mode 100644 index 0000000..85eb97b --- /dev/null +++ b/pieces/Rook.gd @@ -0,0 +1,11 @@ +class_name Rook +extends Piece + + +func get_moves(): + return .traverse() + + +func _ready(): + ._ready() + shortname = "r" + team diff --git a/project.godot b/project.godot index 532f2e0..223672b 100644 --- a/project.godot +++ b/project.godot @@ -9,19 +9,55 @@ config_version=4 _global_script_classes=[ { +"base": "Piece", +"class": "Bishop", +"language": "GDScript", +"path": "res://pieces/Bishop.gd" +}, { "base": "Node2D", "class": "Grid", "language": "GDScript", "path": "res://Grid.gd" }, { +"base": "Piece", +"class": "King", +"language": "GDScript", +"path": "res://pieces/King.gd" +}, { +"base": "Piece", +"class": "Knight", +"language": "GDScript", +"path": "res://pieces/Knight.gd" +}, { +"base": "Piece", +"class": "Pawn", +"language": "GDScript", +"path": "res://pieces/Pawn.gd" +}, { "base": "Node2D", "class": "Piece", "language": "GDScript", -"path": "res://Piece.gd" +"path": "res://pieces/Piece.gd" +}, { +"base": "Piece", +"class": "Queen", +"language": "GDScript", +"path": "res://pieces/Queen.gd" +}, { +"base": "Piece", +"class": "Rook", +"language": "GDScript", +"path": "res://pieces/Rook.gd" } ] _global_script_class_icons={ +"Bishop": "", "Grid": "", -"Piece": "res://assets/california/wP.png" +"King": "", +"Knight": "", +"Pawn": "", +"Piece": "res://assets/california/wP.png", +"Queen": "", +"Rook": "" } [application] |