online multiplayer chess game (note server currently down)
Diffstat (limited to 'Grid.gd')
| -rw-r--r-- | Grid.gd | 114 |
1 files changed, 55 insertions, 59 deletions
@@ -4,6 +4,7 @@ export var PIECE_SET = "california" export(Color) var board_color1 = Color.white export(Color) var board_color2 = Color.black +export(Color) var overlay_color = Color(0.2, 0.345098, 0.188235, 0.592157) onready var background = $Background @@ -16,6 +17,18 @@ const piece_size = Vector2(100, 100) var matrix = [] var background_matrix = [] +var last_clicked + + +func _ready(): + Globals.grid = self + init_board() + init_matrix() + + +func _exit_tree(): + Globals.grid = null + func init_matrix(): for i in range(8): @@ -29,6 +42,8 @@ func instance_piece_at_position(position: Vector2, name: String, sprite: String, var piece = Piece.instance() piece.sprite = piece.get_node("Sprite") piece.sprite.texture = load(sprite) + piece.real_position = position + position *= piece_size # piece.sprite.flip_v = not white # this is not shogi you eejit if white: position -= Vector2(0, piece_size.y) # boost it up @@ -48,12 +63,14 @@ func init_board(): var square = Square.instance() square.rect_size = piece_size square.rect_position = Vector2(i, j) * piece_size - square.name = "square_" + str(i) + "_" + str(j) + 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) + print_matrix_pretty(background_matrix) + func add_pieces(): add_pawns() @@ -68,74 +85,50 @@ func add_pieces(): func add_pawns(): for i in range(8): matrix[1][i] = instance_piece_at_position( - Vector2(i, 1) * piece_size, "pawn", ASSETS_PATH + "bP.png", false + Vector2(i, 1), "pawn", ASSETS_PATH + "bP.png", false ) matrix[6][i] = instance_piece_at_position( - Vector2(i, 7) * piece_size, "pawn", ASSETS_PATH + "wP.png", true + Vector2(i, 7), "pawn", ASSETS_PATH + "wP.png", true ) func add_rooks(): - matrix[0][0] = instance_piece_at_position( - Vector2(0, 0) * piece_size, "rook", ASSETS_PATH + "bR.png", false - ) - matrix[0][7] = instance_piece_at_position( - Vector2(7, 0) * piece_size, "rook", ASSETS_PATH + "bR.png", false - ) - matrix[7][0] = instance_piece_at_position( - Vector2(0, 8) * piece_size, "rook", ASSETS_PATH + "wR.png", true - ) - matrix[7][7] = instance_piece_at_position( - Vector2(7, 8) * piece_size, "rook", ASSETS_PATH + "wR.png", true - ) + 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, 8), "rook", ASSETS_PATH + "wR.png", true) + matrix[7][7] = instance_piece_at_position(Vector2(7, 8), "rook", ASSETS_PATH + "wR.png", true) func add_knights(): matrix[0][1] = instance_piece_at_position( - Vector2(1, 0) * piece_size, "knight", ASSETS_PATH + "bN.png", false + Vector2(1, 0), "knight", ASSETS_PATH + "bN.png", false ) matrix[0][6] = instance_piece_at_position( - Vector2(6, 0) * piece_size, "knight", ASSETS_PATH + "bN.png", false - ) - matrix[7][1] = instance_piece_at_position( - Vector2(1, 8) * piece_size, "knight", ASSETS_PATH + "wN.png", true - ) - matrix[7][6] = instance_piece_at_position( - Vector2(6, 8) * piece_size, "knight", ASSETS_PATH + "wN.png", true + Vector2(6, 0), "knight", ASSETS_PATH + "bN.png", false ) + matrix[7][1] = instance_piece_at_position(Vector2(1, 8), "knight", ASSETS_PATH + "wN.png", true) + matrix[7][6] = instance_piece_at_position(Vector2(6, 8), "knight", ASSETS_PATH + "wN.png", true) func add_bishops(): matrix[0][2] = instance_piece_at_position( - Vector2(2, 0) * piece_size, "bishop", ASSETS_PATH + "bB.png", false + Vector2(2, 0), "bishop", ASSETS_PATH + "bB.png", false ) matrix[0][5] = instance_piece_at_position( - Vector2(5, 0) * piece_size, "bishop", ASSETS_PATH + "bB.png", false - ) - matrix[7][2] = instance_piece_at_position( - Vector2(2, 8) * piece_size, "bishop", ASSETS_PATH + "wB.png", true - ) - matrix[7][5] = instance_piece_at_position( - Vector2(5, 8) * piece_size, "bishop", ASSETS_PATH + "wB.png", true + Vector2(5, 0), "bishop", ASSETS_PATH + "bB.png", false ) + matrix[7][2] = instance_piece_at_position(Vector2(2, 8), "bishop", ASSETS_PATH + "wB.png", true) + matrix[7][5] = instance_piece_at_position(Vector2(5, 8), "bishop", ASSETS_PATH + "wB.png", true) func add_queens(): - matrix[0][3] = instance_piece_at_position( - Vector2(3, 0) * piece_size, "queen", ASSETS_PATH + "bQ.png", false - ) - matrix[7][3] = instance_piece_at_position( - Vector2(3, 8) * piece_size, "queen", ASSETS_PATH + "wQ.png", true - ) + 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, 8), "queen", ASSETS_PATH + "wQ.png", true) func add_kings(): - matrix[0][4] = instance_piece_at_position( - Vector2(4, 0) * piece_size, "king", ASSETS_PATH + "bK.png", false - ) - matrix[7][4] = instance_piece_at_position( - Vector2(4, 8) * piece_size, "king", ASSETS_PATH + "wK.png", true - ) + 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, 8), "king", ASSETS_PATH + "wK.png", true) func print_matrix_pretty(mat = matrix): @@ -153,26 +146,29 @@ func print_matrix_pretty(mat = matrix): print("]") -func _ready(): - Globals.piece_size = piece_size - init_board() - init_matrix() +func check_for_circle(position: Vector2): + var circle = background_matrix[position.x][position.y].circle.visible + return circle func square_clicked(position: Vector2): var spot = matrix[position.y][position.x] - if !spot: - print("spot isnt null") - if !Globals.last_clicked: # its null - print("last clicked is null") + if !spot or !spot.white: + if !last_clicked: # its null return - print("last clicked wasnt null") - Globals.last_clicked.spot(position) - Globals.last_clicked = null - elif Globals.last_clicked != spot: - print("last clicked is different") - if Globals.last_clicked: - Globals.last_clicked.spot(null) - Globals.last_clicked = spot + if check_for_circle(position): + last_clicked.moveto(position) + last_clicked.clear_clicked() + last_clicked = null + elif last_clicked != spot: + if last_clicked: + last_clicked.clear_clicked() + last_clicked = spot spot.clicked() + +func clear_circles(): + for i in range(8): + for j in range(8): + var square = background_matrix[i][j] + square.set_circle(false) |