online multiplayer chess game (note server currently down)
flip board ( wip )
| -rw-r--r-- | Globals.gd | 4 | ||||
| -rw-r--r-- | Grid.gd | 90 | ||||
| -rw-r--r-- | Square.tscn | 1 | ||||
| -rw-r--r-- | Utils.gd | 4 | ||||
| -rw-r--r-- | assets/ui/flip_board.png | 3 | ||||
| -rw-r--r-- | assets/ui/flip_board.png.import | 35 | ||||
| -rw-r--r-- | assets/ui/svg/flip_board.svg | 3 | ||||
| -rw-r--r-- | pieces/Piece.gd | 11 | ||||
| -rw-r--r-- | project.godot | 6 | ||||
| -rw-r--r-- | ui/BarTextureButton.gd | 42 | ||||
| -rw-r--r-- | ui/BarTextureButton.tscn | 27 | ||||
| -rw-r--r-- | ui/GameUI.tscn | 57 | ||||
| -rw-r--r-- | ui/Lobby.gd | 1 | ||||
| -rw-r--r-- | ui/Settings.tscn | 47 | ||||
| -rw-r--r-- | ui/UI.tscn | 3 | ||||
| -rw-r--r-- | ui/flipbutton.gd | 6 |
16 files changed, 265 insertions, 75 deletions
@@ -10,8 +10,8 @@ var fullmove := 1 var halfmove := 0 var in_check := false var checking_piece: Piece = null -var board_color1: Color -var board_color2: Color +var board_color1: Color = Color(0.870588, 0.890196, 0.901961) +var board_color2: Color = Color(0.54902, 0.635294, 0.678431) var white_king: King var black_king: King var turn := true # true for white, false for black @@ -25,7 +25,6 @@ const default_metadata := { } export(Color) var overlay_color := Color(0.078431, 0.333333, 0.117647, 0.498039) - export(Color) var clockrunning_color := Color(0.219608, 0.278431, 0.133333) export(Color) var clockrunninglow := Color(0.47451, 0.172549, 0.164706) export(Color) var clocklow := Color(0.313726, 0.156863, 0.14902) @@ -35,6 +34,9 @@ var promoting = null var background_matrix := [] var history_matrixes := {} var last_clicked: Piece = null +var flipped = false + +var labels = {"letters": [], "numbers": []} onready var background := $Background onready var ASSETS_PATH: String = "res://assets/pieces/%s/" % Globals.piece_set @@ -89,27 +91,60 @@ func reload_sprites() -> void: matrix[i][j].load_texture() +func rotate_pieces(degree: float) -> void: + for i in range(8): + for j in range(8): + var spot = matrix[i][j] + if spot: + spot.sprite.rotation_degrees = degree + # spot.tween.interpolate_property( + # spot.sprite, "rotation_degrees", spot.sprite.rotation_degrees, degree, .5 + # ) + # spot.tween.start() + + +func flip_labels(to_flip: bool) -> void: + for i in range(8): + var numlabel = labels.numbers[i].get_node("Label") + var letlabel = labels.letters[i].get_node("Label") + var number: int + if to_flip: + number = i + 1 + else: + number = 8 - i + numlabel.text = str(number) + letlabel.text = "hgfedcba"[number - 1] + + +func flip_board(): + if global_position == Vector2(800, 800): + global_position = Vector2(0, 0) + rotation_degrees = 0 + rotate_pieces(0) + flip_labels(false) + else: + global_position = Vector2(800, 800) + rotation_degrees = 180 + rotate_pieces(180) + flip_labels(true) + + func init_labels() -> void: for i in range(8): - var letterslabel := BottomLeftLabel.instance() - letterslabel.rect_position.x = i * piece_size.x - letterslabel.rect_position.y = piece_size.y * 7 - size_label(letterslabel, i) - letterslabel.get_node("Label").text = Utils.to_algebraic(letterslabel.rect_position / piece_size)[0] - foreground.add_child(letterslabel) - var numberslabel := TopRightLabel.instance() - numberslabel.rect_position.y = i * piece_size.x - numberslabel.rect_position.x = piece_size.x * 7 - size_label(numberslabel, i) - numberslabel.get_node("Label").text = str(8 - i) - foreground.add_child(numberslabel) - - -func size_label(label, i) -> void: - label.rect_size = piece_size - label.get_node("Label").add_color_override( - "font_color", Globals.board_color1 if i % 2 == 0 else Globals.board_color2 - ) + labels.letters.append(init_label(BottomLeftLabel, i, Vector2(i, 7), "abcdefgh"[i])) + + labels.numbers.append(init_label(TopRightLabel, i, Vector2(7, i), str(8 - i))) + + +func init_label(labelscene: PackedScene, i: int, position: Vector2, text: String) -> Control: + var labelholder = labelscene.instance() + labelholder.rect_size = piece_size + labelholder.rect_position = position * piece_size + var label = labelholder.get_node("Label") + label.text = text + label.add_color_override("font_color", Globals.board_color1 if i % 2 == 0 else Globals.board_color2) + foreground.add_child(labelholder) + return labelholder func threefoldrepetition() -> bool: @@ -201,7 +236,7 @@ func init_board() -> void: # create the board 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.rect_global_position = Vector2(i, j) * piece_size # set the position square.color = Globals.board_color1 if (i + j) % 2 == 0 else Globals.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 @@ -268,6 +303,7 @@ func check_for_frame(position: Vector2) -> bool: # check for a frame, validatin func square_clicked(position: Vector2) -> void: # square clicked + print(Utils.to_algebraic(position), " clicked") if promoting != null: return if Globals.turn != Globals.team: @@ -290,11 +326,13 @@ func square_clicked(position: Vector2) -> void: # square clicked func handle_take(position) -> void: - if last_clicked is Pawn: + if Utils.is_pawn(last_clicked): # if its a pawn var pawn = last_clicked if check_promote(pawn, position, "take"): return - Globals.network.send_move_packet([last_clicked.real_position, position], Network.MOVEHEADERS.move) # piece taking piece + Globals.network.send_move_packet( + PoolVector2Array([last_clicked.real_position, position]), Network.MOVEHEADERS.move + ) # piece taking piece func handle_move(position) -> void: @@ -322,13 +360,15 @@ func handle_move(position) -> void: if en_passant_data[0] == position: # send some packet Globals.network.send_move_packet( - [pawn.real_position, position, en_passant_data[1].real_position], + PoolVector2Array([pawn.real_position, position, en_passant_data[1].real_position]), Network.MOVEHEADERS.passant ) return if check_promote(pawn, position): return - Globals.network.send_move_packet([last_clicked.real_position, position], Network.MOVEHEADERS.move) # piece moving + Globals.network.send_move_packet( + PoolVector2Array([last_clicked.real_position, position]), Network.MOVEHEADERS.move + ) # piece moving func check_promote(pawn, position, calltype: String = "move") -> bool: diff --git a/Square.tscn b/Square.tscn index c4f99b0..145452c 100644 --- a/Square.tscn +++ b/Square.tscn @@ -4,7 +4,6 @@ [ext_resource path="res://assets/ui/whitespace.png" type="Texture" id=2] [sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 381, 394.5 ) [sub_resource type="Shader" id=2] code = "shader_type canvas_item; @@ -25,6 +25,10 @@ func add_move(move) -> void: emit_signal("newmove", move) +func flip_int(i: int) -> int: + return int(abs(7 - i)) + + func reset_vars() -> void: turn_moves.resize(0) turns_moves.resize(0) diff --git a/assets/ui/flip_board.png b/assets/ui/flip_board.png new file mode 100644 index 0000000..d17889c --- /dev/null +++ b/assets/ui/flip_board.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a83967de91faeb3f46c18622c073820687f3e5d49ba22e3f65b05a83161bd971 +size 1610 diff --git a/assets/ui/flip_board.png.import b/assets/ui/flip_board.png.import new file mode 100644 index 0000000..e7166dc --- /dev/null +++ b/assets/ui/flip_board.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/flip_board.png-a6b1203e00ed82fd76f70be2b116ecd2.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://assets/ui/flip_board.png" +dest_files=[ "res://.import/flip_board.png-a6b1203e00ed82fd76f70be2b116ecd2.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=0 +flags/filter=true +flags/mipmaps=false +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/assets/ui/svg/flip_board.svg b/assets/ui/svg/flip_board.svg new file mode 100644 index 0000000..1019859 --- /dev/null +++ b/assets/ui/svg/flip_board.svg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:062568f21186af897885f31c5557a3ac4eda87ef5cfdd1e7a521421b4e1d587c +size 2087 diff --git a/pieces/Piece.gd b/pieces/Piece.gd index c0a5aa0..f650b50 100644 --- a/pieces/Piece.gd +++ b/pieces/Piece.gd @@ -62,13 +62,7 @@ static func to_algebraic(position) -> String: func move(newpos: Vector2) -> void: # dont use directly; use moveto tween.interpolate_property( - self, - "global_position", - global_position, - newpos * Globals.grid.piece_size, - 0.3, - Tween.TRANS_BACK, - Tween.EASE_IN_OUT + self, "global_position", global_position, newpos * Globals.grid.piece_size, 0.3, Tween.TRANS_BACK ) anim.play("Move") tween.start() @@ -84,7 +78,8 @@ func moveto(position, real := true, take := false, override_moveto = false) -> v else: Utils.add_move(algebraic_take_notation(position)) real_position = position - move(position) + move(real_position) + print("%s moving from %s to %s" % [mininame + shortname, Utils.to_algebraic(global_position / Globals.grid.piece_size), Utils.to_algebraic(real_position)]) SoundFx.play("Move") has_moved = true diff --git a/project.godot b/project.godot index 430715d..f8cec04 100644 --- a/project.godot +++ b/project.godot @@ -9,6 +9,11 @@ config_version=4 _global_script_classes=[ { +"base": "Control", +"class": "BarTextureButton", +"language": "GDScript", +"path": "res://ui/BarTextureButton.gd" +}, { "base": "Piece", "class": "Bishop", "language": "GDScript", @@ -80,6 +85,7 @@ _global_script_classes=[ { "path": "res://saveload.gd" } ] _global_script_class_icons={ +"BarTextureButton": "", "Bishop": "res://assets/pieces/california/wB.png", "ColorPickerBetter": "", "ColorPickerButtonBetter": "", diff --git a/ui/BarTextureButton.gd b/ui/BarTextureButton.gd new file mode 100644 index 0000000..cd3776a --- /dev/null +++ b/ui/BarTextureButton.gd @@ -0,0 +1,42 @@ +extends Control +class_name BarTextureButton + +signal pressed() + +var focused = false + +export(Texture) var texture + +export(Color) var normal_color +export(Color) var highlight_color +export(Color) var pressed_color + +onready var texture_button = $Texture +onready var background = $Background + +func _ready(): + texture_button.texture_normal = texture + texture_button.texture_focused = texture + texture_button.texture_pressed = texture + texture_button.texture_hover = texture + +func _on_Texture_pressed(): + emit_signal("pressed") + +func _process(_delta): + if texture_button.pressed: + background.color = pressed_color + elif focused: + background.color = highlight_color + else: + background.color = normal_color + +func _on_Texture_mouse_entered(): + focused = true + background.color = highlight_color + + +func _on_Texture_mouse_exited(): + focused = false + background.color = normal_color + diff --git a/ui/BarTextureButton.tscn b/ui/BarTextureButton.tscn new file mode 100644 index 0000000..fc40c7a --- /dev/null +++ b/ui/BarTextureButton.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://ui/BarTextureButton.gd" type="Script" id=1] +[ext_resource path="res://assets/ui/whitespace.png" type="Texture" id=2] + +[node name="BarTextureButton" type="Control"] +margin_right = 64.0 +margin_bottom = 64.0 +script = ExtResource( 1 ) +texture = ExtResource( 2 ) +highlight_color = Color( 0.243137, 0.129412, 0.129412, 1 ) +pressed_color = Color( 0.227451, 0.270588, 0.301961, 1 ) + +[node name="Background" type="ColorRect" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 + +[node name="Texture" type="TextureButton" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +focus_mode = 0 +enabled_focus_mode = 0 +expand = true + +[connection signal="mouse_entered" from="Texture" to="." method="_on_Texture_mouse_entered"] +[connection signal="mouse_exited" from="Texture" to="." method="_on_Texture_mouse_exited"] +[connection signal="pressed" from="Texture" to="." method="_on_Texture_pressed"] diff --git a/ui/GameUI.tscn b/ui/GameUI.tscn index f79b3a4..3ed8f61 100644 --- a/ui/GameUI.tscn +++ b/ui/GameUI.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=25 format=2] +[gd_scene load_steps=29 format=2] [ext_resource path="res://ui/main.tres" type="Theme" id=1] [ext_resource path="res://ui/roboto.tres" type="DynamicFont" id=2] @@ -15,6 +15,9 @@ [ext_resource path="res://ui/button.tres" type="StyleBox" id=13] [ext_resource path="res://ui/buttonhover.tres" type="StyleBox" id=14] [ext_resource path="res://ui/FENlabel.gd" type="Script" id=15] +[ext_resource path="res://ui/BarTextureButton.tscn" type="PackedScene" id=16] +[ext_resource path="res://assets/ui/flip_board.png" type="Texture" id=17] +[ext_resource path="res://ui/flipbutton.gd" type="Script" id=18] [sub_resource type="DynamicFont" id=1] size = 25 @@ -85,6 +88,9 @@ PopupMenu/styles/panel = ExtResource( 12 ) VBoxContainer/constants/separation = 15 VScrollBar/styles/scroll = SubResource( 5 ) +[sub_resource type="StyleBoxFlat" id=10] +bg_color = Color( 0, 0, 0, 1 ) + [sub_resource type="DynamicFont" id=8] size = 15 font_data = ExtResource( 3 ) @@ -116,16 +122,15 @@ __meta__ = { [node name="VBox" type="VBoxContainer" parent="Holder/Back"] anchor_right = 1.0 anchor_bottom = 1.0 -custom_constants/separation = 50 alignment = 1 __meta__ = { "_edit_lock_": true } [node name="BlackTime" type="Label" parent="Holder/Back/VBox"] -margin_top = 105.0 +margin_top = 107.0 margin_right = 400.0 -margin_bottom = 188.0 +margin_bottom = 190.0 custom_fonts/font = ExtResource( 2 ) text = "00:00.0" align = 1 @@ -156,9 +161,9 @@ autowrap = true script = ExtResource( 7 ) [node name="MovesList" type="ScrollContainer" parent="Holder/Back/VBox"] -margin_top = 238.0 +margin_top = 205.0 margin_right = 400.0 -margin_bottom = 438.0 +margin_bottom = 405.0 rect_min_size = Vector2( 0, 200 ) theme = SubResource( 6 ) scroll_horizontal_enabled = false @@ -168,10 +173,34 @@ script = ExtResource( 6 ) custom_constants/vseparation = 0 columns = 3 +[node name="buttonbarholder" type="Control" parent="Holder/Back/VBox"] +margin_top = 420.0 +margin_right = 400.0 +margin_bottom = 470.0 +rect_min_size = Vector2( 50, 50 ) + +[node name="Panel" type="Panel" parent="Holder/Back/VBox/buttonbarholder"] +anchor_right = 1.0 +anchor_bottom = 1.0 +custom_styles/panel = SubResource( 10 ) + +[node name="buttonbar" type="HBoxContainer" parent="Holder/Back/VBox/buttonbarholder"] +margin_right = 400.0 +margin_bottom = 50.0 +alignment = 1 + +[node name="FlipBoard" parent="Holder/Back/VBox/buttonbarholder/buttonbar" instance=ExtResource( 16 )] +margin_left = 175.0 +margin_right = 225.0 +margin_bottom = 50.0 +rect_min_size = Vector2( 50, 50 ) +script = ExtResource( 18 ) +texture = ExtResource( 17 ) + [node name="WhiteTime" type="Label" parent="Holder/Back/VBox"] -margin_top = 488.0 +margin_top = 485.0 margin_right = 400.0 -margin_bottom = 571.0 +margin_bottom = 568.0 custom_fonts/font = ExtResource( 2 ) text = "00:00.0" align = 1 @@ -194,10 +223,16 @@ color = Color( 0, 0, 0, 1 ) [node name="Timer" type="Node" parent="Holder/Back/VBox"] script = ExtResource( 5 ) +[node name="Spacer" type="Control" parent="Holder/Back/VBox"] +margin_top = 583.0 +margin_right = 400.0 +margin_bottom = 603.0 +rect_min_size = Vector2( 0, 20 ) + [node name="FENlabel" type="LineEdit" parent="Holder/Back/VBox"] -margin_top = 621.0 +margin_top = 618.0 margin_right = 400.0 -margin_bottom = 695.0 +margin_bottom = 692.0 custom_colors/font_color_uneditable = Color( 1, 1, 1, 1 ) custom_fonts/font = SubResource( 8 ) text = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" @@ -220,3 +255,5 @@ anchor_right = 1.0 anchor_bottom = 1.0 margin_right = -400.0 color = Color( 0, 0, 0, 0.784314 ) + +[connection signal="pressed" from="Holder/Back/VBox/buttonbarholder/buttonbar/FlipBoard" to="Holder/Back/VBox/buttonbarholder/buttonbar/FlipBoard" method="_pressed"] diff --git a/ui/Lobby.gd b/ui/Lobby.gd index b2cbd7f..e8bd0cd 100644 --- a/ui/Lobby.gd +++ b/ui/Lobby.gd @@ -126,6 +126,7 @@ func add_turn() -> void: func _on_data(data: Dictionary) -> void: + print(data) Globals.fullmove = data["fullmove"] Globals.turn = data["turn"] Globals.halfmove = data["halfmove"] diff --git a/ui/Settings.tscn b/ui/Settings.tscn index 1c08478..7b47d2d 100644 --- a/ui/Settings.tscn +++ b/ui/Settings.tscn @@ -21,24 +21,23 @@ color = Color( 0, 0, 0, 0.862745 ) [node name="HBoxContainer" type="HBoxContainer" parent="ColorRect" groups=["control"]] anchor_right = 1.0 anchor_bottom = 1.0 +margin_top = 80.0 alignment = 1 [node name="VBoxContainer" type="VBoxContainer" parent="ColorRect/HBoxContainer" groups=["control"]] margin_left = 54.0 margin_right = 420.0 -margin_bottom = 800.0 -alignment = 1 +margin_bottom = 720.0 [node name="BackButton" type="Button" parent="ColorRect/HBoxContainer/VBoxContainer" groups=["control"]] -margin_top = 121.0 margin_right = 366.0 -margin_bottom = 227.0 +margin_bottom = 106.0 text = "back" [node name="PieceSet" type="OptionButton" parent="ColorRect/HBoxContainer/VBoxContainer" groups=["control"]] -margin_top = 242.0 +margin_top = 121.0 margin_right = 366.0 -margin_bottom = 398.0 +margin_bottom = 277.0 focus_mode = 0 custom_constants/hseparation = 3 enabled_focus_mode = 0 @@ -46,17 +45,17 @@ text = "piece set" icon = ExtResource( 3 ) [node name="PreviewLabel" type="Label" parent="ColorRect/HBoxContainer/VBoxContainer"] -margin_top = 413.0 +margin_top = 292.0 margin_right = 366.0 -margin_bottom = 463.0 +margin_bottom = 342.0 text = "Preview" align = 1 [node name="Preview" type="GridContainer" parent="ColorRect/HBoxContainer/VBoxContainer"] margin_left = 83.0 -margin_top = 478.0 +margin_top = 357.0 margin_right = 283.0 -margin_bottom = 678.0 +margin_bottom = 557.0 size_flags_horizontal = 4 custom_constants/vseparation = 0 custom_constants/hseparation = 0 @@ -102,30 +101,28 @@ rect_min_size = Vector2( 100, 100 ) [node name="VBoxContainer2" type="VBoxContainer" parent="ColorRect/HBoxContainer" groups=["control"]] margin_left = 435.0 margin_right = 804.0 -margin_bottom = 800.0 -alignment = 1 +margin_bottom = 720.0 [node name="VsyncButton" type="CheckBox" parent="ColorRect/HBoxContainer/VBoxContainer2" groups=["control"]] -margin_top = 175.0 margin_right = 369.0 -margin_bottom = 315.0 +margin_bottom = 140.0 focus_mode = 0 pressed = true enabled_focus_mode = 0 text = "vsync" [node name="FullscreenButton" type="CheckBox" parent="ColorRect/HBoxContainer/VBoxContainer2" groups=["control"]] -margin_top = 330.0 +margin_top = 155.0 margin_right = 369.0 -margin_bottom = 470.0 +margin_bottom = 295.0 focus_mode = 0 enabled_focus_mode = 0 text = "fullscreen" [node name="Borderless" type="CheckBox" parent="ColorRect/HBoxContainer/VBoxContainer2" groups=["control"]] -margin_top = 485.0 +margin_top = 310.0 margin_right = 369.0 -margin_bottom = 625.0 +margin_bottom = 450.0 focus_mode = 0 enabled_focus_mode = 0 text = "borders" @@ -133,28 +130,26 @@ text = "borders" [node name="VBoxContainer3" type="VBoxContainer" parent="ColorRect/HBoxContainer"] margin_left = 819.0 margin_right = 1145.0 -margin_bottom = 800.0 -alignment = 1 +margin_bottom = 720.0 [node name="resetbutton" type="Button" parent="ColorRect/HBoxContainer/VBoxContainer3"] -margin_top = 226.0 margin_right = 326.0 -margin_bottom = 332.0 +margin_bottom = 106.0 focus_mode = 0 enabled_focus_mode = 0 text = "reset all" [node name="boardcolor1" parent="ColorRect/HBoxContainer/VBoxContainer3" instance=ExtResource( 5 )] -margin_top = 347.0 +margin_top = 121.0 margin_right = 326.0 -margin_bottom = 453.0 +margin_bottom = 227.0 text = "boardcolor1" script = ExtResource( 6 ) [node name="boardcolor2" parent="ColorRect/HBoxContainer/VBoxContainer3" instance=ExtResource( 5 )] -margin_top = 468.0 +margin_top = 242.0 margin_right = 326.0 -margin_bottom = 574.0 +margin_bottom = 348.0 text = "boardcolor2" [connection signal="pressed" from="ColorRect/HBoxContainer/VBoxContainer/BackButton" to="." method="_on_BackButton_pressed"] diff --git a/ui/UI.tscn b/ui/UI.tscn deleted file mode 100644 index b174e02..0000000 --- a/ui/UI.tscn +++ /dev/null @@ -1,3 +0,0 @@ -[gd_scene format=2] - -[node name="UI" type="CanvasLayer"] diff --git a/ui/flipbutton.gd b/ui/flipbutton.gd new file mode 100644 index 0000000..d995e81 --- /dev/null +++ b/ui/flipbutton.gd @@ -0,0 +1,6 @@ +extends BarTextureButton + + + +func _pressed(): + Globals.grid.flip_board()
\ No newline at end of file |