online multiplayer chess game (note server currently down)
board resizing
Co-authored-by: Delano Lourenco <[email protected]>
| -rw-r--r-- | Square.gd | 35 | ||||
| -rw-r--r-- | Square.tscn | 16 | ||||
| -rw-r--r-- | Utils.gd | 25 | ||||
| -rw-r--r-- | networking/PacketHandler.gd | 1 | ||||
| -rw-r--r-- | piece/Piece.gd | 6 | ||||
| -rw-r--r-- | piece/Piece.tscn | 11 | ||||
| m--------- | submodules/gdcli | 0 | ||||
| -rw-r--r-- | ui/board/Arrows.gd | 6 | ||||
| -rw-r--r-- | ui/board/Board.gd | 127 | ||||
| -rw-r--r-- | ui/board/Board.tscn | 44 | ||||
| -rw-r--r-- | ui/board/Game.gd | 14 | ||||
| -rw-r--r-- | ui/board/Game.tscn | 15 | ||||
| -rw-r--r-- | ui/chat/Chat.tscn | 2 | ||||
| -rw-r--r-- | ui/menus/StartMenu.tscn | 2 |
14 files changed, 195 insertions, 109 deletions
@@ -7,28 +7,28 @@ signal right_clicked var move_indicators := [] var square: String -var piece_above := false onready var circle: TextureRect = $CircleHolder/Circle onready var move_indicator: ColorRect = $MoveIndicator func _ready() -> void: - Events.connect("turn_over", self, "check_piece_above") connect("clicked", self, "clicked") - check_piece_above() move_indicator.color = Globals.grid.last_move_indicator_color - circle.rect_min_size = Globals.grid.piece_size / 4 circle.material.set_shader_param("color", Globals.grid.overlay_color) - rect_min_size = Globals.grid.piece_size if Globals.spectating: mouse_default_cursor_shape = CURSOR_FORBIDDEN else: mouse_default_cursor_shape = CURSOR_POINTING_HAND + size() + + +func size(): + circle.rect_min_size = Globals.grid.piece_size / 4 -func check_piece_above(): - piece_above = is_instance_valid(Globals.grid.get_piece(square)) +func check_piece_above() -> bool: + return is_instance_valid(Globals.grid.get_piece(square)) func _gui_input(event: InputEvent): @@ -38,7 +38,7 @@ func _gui_input(event: InputEvent): func _focus_exited(): - if piece_above: + if check_piece_above(): Globals.grid.get_piece(square).background.hide() for m in move_indicators: if is_instance_valid(m): @@ -48,13 +48,12 @@ func _focus_exited(): func clicked(): var b = Globals.grid - var p = b.get_piece(square) - if piece_above and b.chess.turn == Globals.team and not Globals.spectating and p.color == Globals.team: - p.background.show() - var movs = b.chess.__generate_moves({"square": square, "verbose": true}) - for m in movs: - if m.flags & Chess.BITS.CAPTURE: - move_indicators.append(b.board[m.to].frame) - else: - move_indicators.append(b.background_array[m.to].circle) - move_indicators[-1].show() + if check_piece_above() and b.chess.turn == Globals.team and not Globals.spectating: + var p = b.get_piece(square) + if p.color == Globals.team: + p.background.show() + var movs = b.chess.__generate_moves({"square": square, "verbose": true}) + for m in movs: + var i = b.board[m.to].frame if m.flags & Chess.BITS.CAPTURE else b.background_array[m.to].circle + move_indicators.append(i) + i.show() diff --git a/Square.tscn b/Square.tscn index aa172e8..1f4812a 100644 --- a/Square.tscn +++ b/Square.tscn @@ -27,9 +27,13 @@ shader_param/amt = 1.0 shader_param/color = Color( 0.431373, 0.584314, 0.388235, 0.639216 ) [node name="Square" type="ColorRect"] -margin_right = 40.0 -margin_bottom = 40.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -1372.0 +margin_bottom = -750.0 focus_mode = 1 +size_flags_horizontal = 3 +size_flags_vertical = 3 script = ExtResource( 1 ) [node name="MoveIndicator" type="ColorRect" parent="."] @@ -46,10 +50,10 @@ mouse_filter = 2 [node name="Circle" type="TextureRect" parent="CircleHolder"] visible = false material = SubResource( 3 ) -margin_left = 20.0 -margin_top = 20.0 -margin_right = 20.0 -margin_bottom = 20.0 +margin_left = 25.0 +margin_top = 25.0 +margin_right = 25.0 +margin_bottom = 25.0 mouse_filter = 2 texture = ExtResource( 2 ) expand = true @@ -46,6 +46,20 @@ func cli() -> void: ) ) parser.add_argument( + Arg.new({triggers = ["--moves", "-m"], n_args = "*", help = "pgn to start with", arg_names = "pgn"}) + ) + parser.add_argument( + Arg.new( + { + triggers = ["--color", "-c"], + n_args = 1, + default = "white", + help = "color to play as (defaults to white)", + arg_names = "color" + } + ) + ) + parser.add_argument( Arg.new( { triggers = ["--join", "-j"], @@ -79,10 +93,17 @@ func cli() -> void: if args.has("host") and args.host: print("hosting game: %s" % args.host) if PacketHandler.lobby.validate_text(args.host): - var move_list = Pgn.parse(OS.get_environment("MOVES"), false).moves + var s = args.get("moves", PoolStringArray()).join(" ") + var move_list = Pgn.parse(s, false).moves if move_list: print("with moves: %s" % move_list) - PacketHandler.host_game(args.host, true, move_list) + var clr = ( + (true if args.color.to_lower() in ["w", "white"] or str_bool(args.color) else false) + if args.has("color") + else (true) + ) # default white + prints("as", "white" if clr else "black") + PacketHandler.host_game(args.host, clr, move_list) return elif args.has("join") and args.join: print("joining game: %s" % args.join) diff --git a/networking/PacketHandler.gd b/networking/PacketHandler.gd index ddfbf1d..3b87049 100644 --- a/networking/PacketHandler.gd +++ b/networking/PacketHandler.gd @@ -162,6 +162,7 @@ func _start_game() -> void: emit_signal("start_game") lobby.set_buttons(false) if Globals.team == Chess.BLACK: + yield(get_tree(), "idle_frame") board.get_board().flip_board() diff --git a/piece/Piece.gd b/piece/Piece.gd index 468c3b3..7e99386 100644 --- a/piece/Piece.gd +++ b/piece/Piece.gd @@ -19,9 +19,15 @@ signal promotion_decided var promote_to := "" +func size() -> void: # size the control + rect_size = Globals.grid.piece_size + rect_position = Chess.algebraic2vec(position) * Globals.grid.piece_size + + func _ready(): add_child(tween) load_texture() + size() frame.modulate = Globals.grid.overlay_color background.color = Globals.grid.overlay_color diff --git a/piece/Piece.tscn b/piece/Piece.tscn index 1353492..c9daea2 100644 --- a/piece/Piece.tscn +++ b/piece/Piece.tscn @@ -103,24 +103,23 @@ tracks/0/keys = { "values": [ 0.0, 20.0, 0.0 ] } -[node name="Piece" type="Control" groups=["piece"]] +[node name="Piece" type="AspectRatioContainer" groups=["piece"]] margin_right = 80.0 margin_bottom = 80.0 -rect_min_size = Vector2( 80, 80 ) rect_pivot_offset = Vector2( 40, 40 ) mouse_filter = 2 script = ExtResource( 3 ) [node name="ColorRect" type="ColorRect" parent="."] visible = false -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 80.0 +margin_bottom = 80.0 mouse_filter = 2 color = Color( 0.0784314, 0.333333, 0.117647, 0.498039 ) [node name="Sprite" type="TextureRect" parent="."] -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 80.0 +margin_bottom = 80.0 mouse_filter = 2 texture = ExtResource( 1 ) expand = true diff --git a/submodules/gdcli b/submodules/gdcli -Subproject 7f582e8d3a3328023b6d0743597e6e2d09a02a4 +Subproject 6040364dae05d331f8d6c9d3d52186df1d0399c diff --git a/ui/board/Arrows.gd b/ui/board/Arrows.gd index 6b56bf2..b2597d4 100644 --- a/ui/board/Arrows.gd +++ b/ui/board/Arrows.gd @@ -15,7 +15,6 @@ export(Color) var green_overlay func _setup(_b: Grid): b = _b - Events.connect("turn_over", self, "on_turn_over") for k in Chess.SQUARE_MAP: b.background_array[Chess.SQUARE_MAP[k]].connect("right_clicked", self, "right_clicked", [k]) b.background_array[Chess.SQUARE_MAP[k]].connect("clicked", self, "left_clicked", [k]) @@ -98,8 +97,3 @@ func clear_arrows(): arrows.resize(0) circles.resize(0) first = Vector2.ZERO - - -func on_turn_over(): - if b.chess.turn != Globals.team: # i just went; arrows can go away now - clear_arrows() 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) diff --git a/ui/board/Board.tscn b/ui/board/Board.tscn index 965d241..e1c029c 100644 --- a/ui/board/Board.tscn +++ b/ui/board/Board.tscn @@ -3,55 +3,69 @@ [ext_resource path="res://ui/board/Board.gd" type="Script" id=1] [ext_resource path="res://ui/board/Arrows.gd" type="Script" id=2] -[node name="Board" type="Control"] +[node name="Board" type="AspectRatioContainer"] anchor_right = 1.0 anchor_bottom = 1.0 -rect_min_size = Vector2( 640, 640 ) +margin_right = -1122.0 +margin_bottom = -500.0 +rect_min_size = Vector2( 300, 300 ) mouse_filter = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +alignment_horizontal = 0 +alignment_vertical = 0 script = ExtResource( 1 ) __meta__ = { "_edit_group_": true } [node name="Background" type="GridContainer" parent="."] -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 300.0 +margin_bottom = 300.0 mouse_filter = 2 custom_constants/vseparation = 0 custom_constants/hseparation = 0 columns = 8 [node name="Pieces" type="Control" parent="."] -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 300.0 +margin_bottom = 300.0 mouse_filter = 2 -[node name="Foreground" type="CanvasLayer" parent="."] -layer = 0 - [node name="Canvas" type="ViewportContainer" parent="."] modulate = Color( 1, 1, 1, 0.588235 ) -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 300.0 +margin_bottom = 300.0 mouse_filter = 2 stretch = true [node name="Viewport" type="Viewport" parent="Canvas"] -size = Vector2( 1422, 800 ) +size = Vector2( 300, 300 ) transparent_bg = true handle_input_locally = false disable_3d = true usage = 0 +render_target_update_mode = 3 [node name="Arrows" type="Control" parent="Canvas/Viewport"] script = ExtResource( 2 ) red_overlay = Color( 0.729412, 0.254902, 0.254902, 1 ) green_overlay = Color( 0.1272, 0.53, 0.18762, 1 ) +[node name="Foreground" type="GridContainer" parent="."] +margin_right = 300.0 +margin_bottom = 300.0 +mouse_filter = 2 +custom_constants/vseparation = 0 +custom_constants/hseparation = 0 +columns = 8 + [node name="Darken" type="ColorRect" parent="."] visible = false -anchor_right = 1.0 -anchor_bottom = 1.0 +margin_right = 300.0 +margin_bottom = 300.0 mouse_filter = 2 -mouse_default_cursor_shape = 7 +mouse_default_cursor_shape = 8 color = Color( 0, 0, 0, 0.784314 ) + +[connection signal="resized" from="." to="." method="_resized"] diff --git a/ui/board/Game.gd b/ui/board/Game.gd index 834a3da..5eafc8f 100644 --- a/ui/board/Game.gd +++ b/ui/board/Game.gd @@ -1,6 +1,7 @@ extends Control onready var status: StatusLabel = find_node("Status") +onready var chat: Chat = find_node("Chat") onready var sidebar := $Holder/SidebarRight onready var panels := [ sidebar.whitepanel, @@ -21,10 +22,8 @@ func get_board() -> Node: func _spectate_info(info: Dictionary) -> void: - var whitepnl: UserPanel = panels[0] - set_panel(whitepnl, info.white.name, info.white.country) - var blackpnl: UserPanel = panels[1] - set_panel(blackpnl, info.black.name, info.black.country) + set_panel(panels[0], info.white.name, info.white.country) + set_panel(panels[1], info.black.name, info.black.country) func _on_info(info: Dictionary) -> void: @@ -33,6 +32,11 @@ func _on_info(info: Dictionary) -> void: set_panel(panels[abs(enemy_int - 1)], Creds.get("name"), Creds.get("country")) # own panel -func set_panel(pnl, name, country) -> void: +func set_panel(pnl: UserPanel, name: String, country: String) -> void: pnl.set_name(name if name else "Anonymous") pnl.set_flag(country) + + +func _input(event: InputEvent): + if event is InputEventKey and event.pressed and event.scancode == KEY_Z: + chat.visible = !chat.visible diff --git a/ui/board/Game.tscn b/ui/board/Game.tscn index f2c11ce..e27edc1 100644 --- a/ui/board/Game.tscn +++ b/ui/board/Game.tscn @@ -25,7 +25,6 @@ anchor_right = 0.0 anchor_bottom = 0.0 margin_right = 391.0 margin_bottom = 800.0 -size_flags_horizontal = 3 [node name="middle" type="VBoxContainer" parent="Holder"] margin_left = 391.0 @@ -33,22 +32,24 @@ margin_right = 1031.0 margin_bottom = 800.0 rect_min_size = Vector2( 640, 640 ) mouse_filter = 2 -custom_constants/separation = 10 +size_flags_horizontal = 3 +size_flags_vertical = 3 +custom_constants/separation = 0 [node name="Board" parent="Holder/middle" instance=ExtResource( 5 )] anchor_right = 0.0 anchor_bottom = 0.0 margin_right = 640.0 -margin_bottom = 640.0 +margin_bottom = 800.0 sidebar_path = NodePath("../../SidebarRight") ui_path = NodePath("../../..") [node name="BackButton" type="Button" parent="Holder/middle"] visible = false -margin_left = 210.0 -margin_top = 650.0 -margin_right = 430.0 -margin_bottom = 752.0 +margin_left = 245.0 +margin_top = 698.0 +margin_right = 465.0 +margin_bottom = 800.0 rect_min_size = Vector2( 220, 0 ) size_flags_horizontal = 4 size_flags_vertical = 4 diff --git a/ui/chat/Chat.tscn b/ui/chat/Chat.tscn index 59da6c2..ce924d9 100644 --- a/ui/chat/Chat.tscn +++ b/ui/chat/Chat.tscn @@ -9,6 +9,8 @@ anchor_right = 1.0 anchor_bottom = 1.0 rect_min_size = Vector2( 300, 0 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 theme = ExtResource( 1 ) script = ExtResource( 2 ) diff --git a/ui/menus/StartMenu.tscn b/ui/menus/StartMenu.tscn index 7c9e210..b6f3f38 100644 --- a/ui/menus/StartMenu.tscn +++ b/ui/menus/StartMenu.tscn @@ -33,7 +33,7 @@ size_flags_vertical = 0 drag_to_rearrange_enabled = true use_hidden_tabs_for_min_size = true -[node name="οπΏπ¦" parent="tabs" instance=ExtResource( 5 )] +[node name="ο" parent="tabs" instance=ExtResource( 5 )] margin_left = 30.0 margin_top = 86.0 margin_right = -30.0 |