online multiplayer chess game (note server currently down)
Diffstat (limited to 'ui/board/Board.gd')
| -rw-r--r-- | ui/board/Board.gd | 127 |
1 files changed, 84 insertions, 43 deletions
diff --git a/ui/board/Board.gd b/ui/board/Board.gd index 2a9b259..7728f14 100644 --- a/ui/board/Board.gd +++ b/ui/board/Board.gd @@ -12,7 +12,7 @@ signal remove_last var move_indicators: PoolIntArray = [] -const piece_size := Vector2(80, 80) +var piece_size: Vector2 export(Color) var overlay_color := Color(0.078431, 0.333333, 0.117647, 0.498039) export(Color) var last_move_indicator_color := Color(0.74902, 0.662745, 0.223529, 0.498039) @@ -56,20 +56,40 @@ func _exit_tree(): Globals.grid = null +func _resized(): + var old_pc = piece_size + piece_size = rect_size / 8 + piece_size.x = clamp(piece_size.x, 0, piece_size.y) + piece_size.y = clamp(piece_size.y, 0, piece_size.x) + rect_pivot_offset = (piece_size * 8) / 2 + if !(board.empty() && background_array.empty()) and piece_size != old_pc: + resize_board() + + func _ready(): + _resized() Events.connect("turn_over", self, "_on_turn_over") PacketHandler.connect("move_data", self, "move") - rect_min_size = piece_size * 8 - rect_pivot_offset = rect_min_size / 2 create_pieces() - init_board() - init_labels() + create_squares() + create_labels() + +func resize_board(): + resize_squares() + resize_pieces() -func init_board() -> void: # create the board + +func resize_squares() -> void: + for i in Chess.SQUARE_MAP.values(): + var square: BackgroundSquare = background_array[i] + square.size() + + +func create_squares() -> void: # create the board background_array.resize(128) for i in Chess.SQUARE_MAP.values(): - var alg = Chess.algebraic(i) + var alg := Chess.algebraic(i) var square := Square.instance() # create a square square.name = alg square.square = alg @@ -81,28 +101,65 @@ func init_board() -> void: # create the board find_node("Arrows")._setup(self) # initialize the arrows -func init_labels() -> void: - foreground.offset = rect_global_position - for i in range(8): - labels.letters.append(init_label(i, Vector2(i, 7), "abcdefgh"[i], Vector2(10, -10), Label.VALIGN_BOTTOM)) - labels.numbers.append(init_label(i, Vector2(7, i), str(8 - i), Vector2(-10, 10), 0, Label.VALIGN_BOTTOM)) - - -func init_label(i: int, position: Vector2, text: String, off := Vector2.ZERO, valign := 0, align := 0) -> Label: +func create_labels() -> void: + var font: DynamicFont = load("res://ui/ubuntu-bold.tres").duplicate() + font.size = 15 + for k in Chess.SQUARE_MAP: + if k == "h1": + var l = init_label(font,k,k[0],VALIGN_BOTTOM,0,false) + var n = init_label(font,k, k[1], 0,VALIGN_BOTTOM,false) + var h = HBoxContainer.new() + h.mouse_filter = MOUSE_FILTER_IGNORE + h.add_child(l) + h.add_child(n) + labels.numbers.append(n) + labels.letters.append(l) + foreground.add_child(h) + elif k[0] == "h": # file h contains numbers + labels.numbers.append(init_label(font, k, k[1], 0, VALIGN_BOTTOM)) + elif k[1] == "1": # rank 1 contains letters + labels.letters.append(init_label(font, k, k[0], VALIGN_BOTTOM)) + else: + var spacer = Control.new() + spacer.mouse_filter = MOUSE_FILTER_IGNORE + spacer.name = k + "_space" + spacer.size_flags_horizontal = SIZE_EXPAND_FILL + spacer.size_flags_vertical = SIZE_EXPAND_FILL + foreground.add_child(spacer) + + +func init_label(font: DynamicFont, alg: String, text: String, valign := 0, align := 0, add:=true) -> Label: var label := Label.new() - label.rect_size = piece_size label.align = align label.valign = valign - label.rect_position = (position * piece_size) + off + label.name = text + label.size_flags_horizontal = SIZE_EXPAND_FILL + label.size_flags_vertical = SIZE_EXPAND_FILL label.text = text - label.add_color_override("font_color", Globals.board_color1 if i % 2 == 0 else Globals.board_color2) - var font: DynamicFont = load("res://ui/ubuntu-bold.tres").duplicate() - font.size = 15 + label.add_color_override( + "font_color", Globals.board_color1 if Chess.square_color(alg) == "dark" else Globals.board_color2 + ) label.add_font_override("font", font) - foreground.add_child(label) + if add: + foreground.add_child(label) return label +func clear_pieces() -> void: + for i in Chess.SQUARE_MAP.values(): + var p: Piece = board[i] + if p: + p.queue_free() + board[i] = null + + +func resize_pieces(): + for i in Chess.SQUARE_MAP.values(): + var p: Piece = board[i] + if p: + p.size() + + func create_pieces(): board.resize(128) for k in Chess.SQUARE_MAP: @@ -113,13 +170,10 @@ func create_pieces(): func make_piece(algebraic: String, piece_type: String, color := "w") -> void: # make peace var piece := PieceScene.instance() # create a piece - var position = Chess.algebraic2vec(algebraic) # get the position piece.name = "%s@%s" % [piece_type, algebraic] piece.position = algebraic piece.type = piece_type - piece.rect_global_position = position * piece_size # set the global position - piece.rect_min_size = piece_size - piece.rect_pivot_offset = piece_size / 2 # rotate around center + piece.size() piece.color = color pieces.add_child(piece) # add the piece to the grid set_piece(algebraic, piece) @@ -143,17 +197,12 @@ func flip_labels() -> void: func flip_board() -> void: + flipped = !flipped + rect_rotation = 0 if rect_rotation == 180 else 180 + foreground.rect_rotation = rect_rotation sidebar.flip_panels() - if flipped: - flipped = false - rect_rotation = 0 - flip_pieces() - flip_labels() - else: - flipped = true - rect_rotation = 180 - flip_pieces() - flip_labels() + flip_pieces() + flip_labels() func square_clicked(clicked_square: String) -> void: @@ -226,14 +275,6 @@ func clear_last_clicked(): darken.hide() -func clear_pieces() -> void: - for i in Chess.SQUARE_MAP.values(): - var p = board[i] - if p: - p.queue_free() - board[i] = null - - func draw(reason := "") -> void: var string = "draw by " + reason ui.set_status(string, 0) |