online multiplayer chess game (note server currently down)
| -rw-r--r-- | Globals.gd | 6 | ||||
| -rw-r--r-- | Grid.gd | 261 | ||||
| -rw-r--r-- | Piece.gd | 213 | ||||
| -rw-r--r-- | Piece.tscn | 56 | ||||
| -rw-r--r-- | Square.gd | 1 | ||||
| -rw-r--r-- | UI.tscn | 3 | ||||
| -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 |
14 files changed, 552 insertions, 335 deletions
@@ -2,7 +2,9 @@ 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 white_king: King +var black_king: King var turn := true # true for white, false for black # true cuz white goes first @@ -20,147 +20,159 @@ var matrix = [] var background_matrix = [] var last_clicked +onready var piece_sets = walk_dir() + func _ready(): - print(PIECE_SET) - Globals.grid = self - init_board() - init_matrix() - Events.connect("turn_over", self, "_on_turn_over") + Globals.grid = self # tell the globals that this is the grid + init_board() # create the tile squares + init_matrix() # create the pieces + Events.connect("turn_over", self, "_on_turn_over") # listen for turn_over events func _on_turn_over(): - 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.checking_piece = null # reset checking_piece + Globals.in_check = false # reset in_check + check_in_check() # check if in_check -func _exit_tree(): - Globals.grid = null +func check_in_check(): # check if in_check + for i in range(0, 8): # for each row + 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 + 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 by " + spot.shortname) # print the check + return true + return false # not in check -func init_matrix(): - for i in range(8): - matrix.append([]) - for _j in range(8): - matrix[i].append(null) - add_pieces() - - -func instance_piece_at_position(position: Vector2, name: String, sprite: String, white: bool = true): - var piece = Piece.instance() - piece.sprite = piece.get_node("Sprite") - piece.sprite.texture = load(sprite) - piece.real_position = position - position *= piece_size - piece.global_position = position - piece.realname = name - piece.name = name - piece.white = white - add_child(piece) - return piece - - -func init_board(): - for i in range(8): - background_matrix.append([]) - for j in range(8): - # var square = ColorRect.new() - var square = Square.instance() - square.rect_size = piece_size - square.rect_position = Vector2(i, j) * piece_size - square.realname = "square_" + str(i) + "_" + str(j) - square.color = board_color1 if (i + j) % 2 == 0 else board_color2 - square.real_position = Vector2(i, j) - background.add_child(square) - square.connect("clicked", self, "square_clicked") - background_matrix[i].append(square) - - -func add_pieces(): +func _exit_tree(): + Globals.grid = null # reset the globals grid when leaving tree + + +func init_matrix(): # create the matrix + for i in range(8): # for each row + matrix.append([]) # add a row + for _j in range(8): # for each column + matrix[i].append(null) # add a square + add_pieces() # add the pieces + + +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.white = white # set its team + add_child(piece) # add the piece to the grid + return piece # return the piece + + +func init_board(): # create the board + for i in range(8): # for each row + background_matrix.append([]) # add a row + for j in range(8): # for each column + 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.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 + square.connect("clicked", self, "square_clicked") # connect the clicked event + background_matrix[i].append(square) # add the square to the background matrix + + +func add_pieces(): # add the pieces add_pawns() add_rooks() add_knights() add_bishops() add_queens() add_kings() - print_matrix_pretty() + print_matrix_pretty() # print the matrix func add_pawns(): for i in range(8): - matrix[1][i] = instance_piece_at_position(Vector2(i, 1), "pawn", ASSETS_PATH + "bP.png", false) - matrix[6][i] = instance_piece_at_position(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] = instance_piece_at_position(Vector2(0, 0), "rook", ASSETS_PATH + "bR.png", false) - matrix[0][7] = instance_piece_at_position(Vector2(7, 0), "rook", ASSETS_PATH + "bR.png", false) - matrix[7][0] = instance_piece_at_position(Vector2(0, 7), "rook", ASSETS_PATH + "wR.png", true) - matrix[7][7] = instance_piece_at_position(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] = instance_piece_at_position(Vector2(1, 0), "knight", ASSETS_PATH + "bN.png", false) - matrix[0][6] = instance_piece_at_position(Vector2(6, 0), "knight", ASSETS_PATH + "bN.png", false) - matrix[7][1] = instance_piece_at_position(Vector2(1, 7), "knight", ASSETS_PATH + "wN.png", true) - matrix[7][6] = instance_piece_at_position(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] = instance_piece_at_position(Vector2(2, 0), "bishop", ASSETS_PATH + "bB.png", false) - matrix[0][5] = instance_piece_at_position(Vector2(5, 0), "bishop", ASSETS_PATH + "bB.png", false) - matrix[7][2] = instance_piece_at_position(Vector2(2, 7), "bishop", ASSETS_PATH + "wB.png", true) - matrix[7][5] = instance_piece_at_position(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] = instance_piece_at_position(Vector2(3, 0), "queen", ASSETS_PATH + "bQ.png", false) - matrix[7][3] = instance_piece_at_position(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] = instance_piece_at_position(Vector2(4, 0), "king", ASSETS_PATH + "bK.png", false) - matrix[7][4] = instance_piece_at_position(Vector2(4, 7), "king", ASSETS_PATH + "wK.png", true) - - -func print_matrix_pretty(mat = matrix): - print("[") - for r in mat: - var row = " [" - for i in range(8): - var c = r[i] - var ender = ", " if i < 7 else "" - if c: - row += c.realname + ender - else: - row += "null" + ender - print(row + "],") - print("]") - - -func check_for_circle(position: Vector2): + 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 + for j in range(len(mat)): # for each row + var r = mat[j] # get the row + var row = str(8 - j) + " " # init the string + for i in range(8): # for each column + var c = r[i] # get the column + var ender = " " if i < 7 else "" # set the end string + if c: # if there is a piece + row += c.shortname + ender # add the shortname + else: # if there is no piece + row += "00" + ender # add 00 + print(row) # print the string + print(" a b c d e f g h") # print the column names + + +func check_for_circle(position: Vector2): # check for a circle, validating movement return background_matrix[position.x][position.y].circle_on -func check_for_frame(position: Vector2): - if !matrix[position.y][position.x]: - return false - return matrix[position.y][position.x].frameon +func check_for_frame(position: Vector2): # check for a frame, validating taking + if !matrix[position.y][position.x]: # if there is no piece + return false # return false + return matrix[position.y][position.x].frameon # return if the frame is on -func square_clicked(position: Vector2): - var spot = matrix[position.y][position.x] +func square_clicked(position: Vector2): # square clicked + var spot = matrix[position.y][position.x] # get the spot 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 @@ -170,21 +182,40 @@ func square_clicked(position: Vector2): spot.clicked() # tell the piece shit happeend -func clear_circles(): - for i in range(8): - 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) - - -func _input(event): - if event.is_action("debug"): - print_matrix_pretty() +func turn_over(): + Globals.turn = not Globals.turn + Globals.turns += 1 + Events.emit_signal("turn_over") + + +func clear_fx(): # 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 + 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 + if event.is_action("debug"): # if debug + print_matrix_pretty() # print the matrix + + +func walk_dir(path = "res://assets"): # walk the directory, finding the asset packs + var folders = [] # init the folders + var dir = Directory.new() # init the directory + if dir.open(path) == OK: # open the directory + dir.list_dir_begin() # list the directory + var file_name = dir.get_next() # get the next file + while file_name != "": # while there is a file + if dir.current_is_dir(): # if the current is a directory + if file_name == "." or file_name == "..": # if it is a dot or dot dot + file_name = dir.get_next() # get the next file + continue + folders.append(file_name) # add the folder + file_name = dir.get_next() # get the next file + else: + printerr("An error occurred when trying to access the path " + path) # print the error + return folders # return the folders diff --git a/Piece.gd b/Piece.gd deleted file mode 100644 index 369f508..0000000 --- a/Piece.gd +++ /dev/null @@ -1,213 +0,0 @@ -extends Node2D -class_name Piece, "res://assets/california/wP.png" - -var real_position = Vector2.ZERO -var white := true -var realname = "pawn" -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 - - -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): - 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") - - -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(carry, real): - if real: - set_circle(carry) - set_circle(carry, "take") - else: - var result = set_circle(carry, "take", false) - return result # checking if king is takeable - - -func create_circles(real = true): - # for motion - match realname: - "pawn": - var carry = ( - [pos_around(Vector2.UP)] - if has_moved - else [pos_around(Vector2.UP), pos_around(Vector2.UP * 2)] - ) - if !white: - carry = ( - [pos_around(Vector2.DOWN)] - if has_moved - else [pos_around(Vector2.DOWN), pos_around(Vector2.DOWN * 2)] - ) - if real: - set_circle(carry) - # deal with the take logic - carry = [] - 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 - carry.append(i) - if real: - set_circle(carry, "take") - else: - return set_circle(carry, "take", false) - "king": - 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)) - ] - return reality(carry, real) - "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)) - ] - return reality(carry, real) - "rook": - var carry = traverse(all_dirs().slice(0, 4)) - return reality(carry, real) - "bishop": - var carry = traverse(all_dirs().slice(4, 8)) - return reality(carry, real) - "queen": - var carry = traverse(all_dirs()) - return reality(carry, real) - - -func traverse(arr = [Vector2.UP, Vector2.DOWN, Vector2.LEFT, Vector2.RIGHT]): - var carry = [] - for i in arr: - var pos = real_position - while true: - pos = pos + i - if not is_on_board(pos): - break - if at_pos(pos) != null: - carry.append(pos) - break - carry.append(pos) - return carry - - -func at_pos(vector): - return Globals.grid.matrix[vector.y][vector.x] - - -func set_circle(positions: Array, type := "move", real = true): - for i in range(len(positions)): - var pos = positions[i] - if not is_on_board(pos): - continue - var spot = at_pos(pos) - if type == "move": - if spot: - continue - 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: - spot.set_frame(true) - if spot.realname == "king": - if real: - printerr("shit") - else: - print("chec") - return true - return false - - -func pd(string, toprint): - if toprint: - print(string) - - -func is_on_board(vector: Vector2) -> bool: - 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 @@ -0,0 +1,3 @@ +[gd_scene format=2] + +[node name="UI" type="CanvasLayer"] 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] |