online multiplayer chess game (note server currently down)
| -rw-r--r-- | ClickableSprite.gd | 26 | ||||
| -rw-r--r-- | ClickableSprite.tscn | 26 | ||||
| -rw-r--r-- | Grid.gd | 258 | ||||
| -rw-r--r-- | Square.gd | 6 | ||||
| -rw-r--r-- | Square.tscn | 2 | ||||
| -rw-r--r-- | Utils.gd | 20 | ||||
| -rw-r--r-- | World.tscn | 9 | ||||
| -rw-r--r-- | pieces/King.gd | 8 | ||||
| -rw-r--r-- | pieces/Pawn.gd | 90 | ||||
| -rw-r--r-- | pieces/Piece.gd | 13 | ||||
| -rw-r--r-- | project.godot | 7 | ||||
| -rw-r--r-- | ui/GameUI.tscn | 7 | ||||
| -rw-r--r-- | ui/MovesList.gd | 2 | ||||
| -rw-r--r-- | ui/StartMenu.gd | 16 | ||||
| -rw-r--r-- | ui/Timer.gd | 12 |
15 files changed, 325 insertions, 177 deletions
diff --git a/ClickableSprite.gd b/ClickableSprite.gd new file mode 100644 index 0000000..929c700 --- /dev/null +++ b/ClickableSprite.gd @@ -0,0 +1,26 @@ +extends Node2D + +signal clicked + +var c = 0 + +onready var sprite = $Sprite + + +func _ready(): + $Area2D/CollisionShape2D.shape.extents = Globals.grid.piece_size / 2 + + +func _on_Area2D_input_event(_viewport: Node, _event: InputEvent, _shape_idx: int): + if visible and Input.is_action_just_released("click"): + c += 1 + if c >= 1: + emit_signal("clicked", self) + + +func _on_Area2D_mouse_entered(): + sprite.scale = Vector2(1, 1) + + +func _on_Area2D_mouse_exited(): + sprite.scale = Vector2(1.2, 1.2) diff --git a/ClickableSprite.tscn b/ClickableSprite.tscn new file mode 100644 index 0000000..e2ecdb7 --- /dev/null +++ b/ClickableSprite.tscn @@ -0,0 +1,26 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://ClickableSprite.gd" type="Script" id=1] + +[sub_resource type="RectangleShape2D" id=1] + +[node name="ClickableSprite" type="Node2D"] +visible = false +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +__meta__ = { +"_edit_vertical_guides_": [ ] +} + +[node name="Area2D" type="Area2D" parent="."] +collision_mask = 0 +monitoring = false +monitorable = false + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource( 1 ) + +[connection signal="input_event" from="Area2D" to="." method="_on_Area2D_input_event"] +[connection signal="mouse_entered" from="Area2D" to="." method="_on_Area2D_mouse_entered"] +[connection signal="mouse_exited" from="Area2D" to="." method="_on_Area2D_mouse_exited"] @@ -1,12 +1,13 @@ extends Node2D class_name Grid -onready var PIECE_SET: String = Globals.piece_set - -export(Color) var board_color1 := Color(0.870588, 0.890196, 0.901961) -export(Color) var board_color2 := Color(0.54902, 0.635294, 0.678431) -export(Color) var overlay_color := Color(0.2, 0.345098, 0.188235, 0.592157) - +const topper_header = "┏━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┓" +const middle_header = "┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫" +const middish_heads = "┗━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫" +const bottom_header = "┗━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┛" +const smaller_heads = " ┗━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┛" +const letter_header = " ┃ a ┃ b ┃ c ┃ d ┃ e ┃ f ┃ g ┃ h ┃" +const ender = " ┃ " # for pretty prints const Piece = preload("res://Piece.tscn") const Square = preload("res://Square.tscn") const BottomLeftLabel = preload("res://ui/BottomLeftLabel.tscn") @@ -23,14 +24,20 @@ const default_metadata := { "bcep": [], # black can enpassant } +export(Color) var board_color1 := Color(0.870588, 0.890196, 0.901961) +export(Color) var board_color2 := Color(0.54902, 0.635294, 0.678431) +export(Color) var overlay_color := Color(0.2, 0.345098, 0.188235, 0.592157) + var matrix := [] +var promoting = null var background_matrix := [] var history_matrixes := {} - var last_clicked = null +onready var PIECE_SET: String = Globals.piece_set + onready var background := $Background -onready var ASSETS_PATH := "res://assets/pieces/" + PIECE_SET + "/" +onready var ASSETS_PATH := "res://assets/pieces/%s/" % PIECE_SET onready var foreground := $Foreground onready var pieces := $Pieces onready var status_label := $"../UI/Holder/Back/VBox/Status" @@ -40,16 +47,48 @@ func _ready(): Globals.grid = self # tell the globals that this is the grid init_board() # create the tile squares init_matrix() # create the pieces - init_labels() + init_labels() # add the labels Events.connect("turn_over", self, "_on_turn_over") # listen for turn_over events Events.connect("outoftime", self, "_on_outoftime") # listen for timeout events -func _on_outoftime(who): - if who == "white": - win("black") - else: - win("white") +func _exit_tree(): + Globals.grid = null # reset the globals grid when leaving tree + + +func _input(event): # input + if event.is_action_released("debug"): # if debug + print_matrix_pretty() # print the matrix + if event.is_action_released("kill"): + if last_clicked and OS.is_debug_build(): # last clicked isnt null and were in debug + last_clicked.took() # kill the piece + last_clicked = null + clear_fx() # clear the circles + + +func print_matrix_pretty(mat = matrix): # print the matrix + for j in range(8): # for each row + var r: Array = mat[j] # get the row + if j == 0: + print(topper_header) # print the top border + else: + print(middle_header) # print the middle border + var row = "┃ %s ┃ " % str(8 - j) # init the string + for i in range(8): # for each column + var c = r[i] # get the column + if c: # if there is a piece + row += c.mininame + ender # add the shortname + else: # if there is no piece + row += " " + ender # add 00 + print(row) # print the string + print("%s\n%s\n%s" % [middish_heads, letter_header, smaller_heads]) + + +func reload_sprites(): + for i in range(8): + for j in range(8): + if matrix[i][j]: + matrix[i][j].load_texture() func init_labels(): @@ -97,30 +136,6 @@ func mat2str(mat = matrix): return string -func _on_turn_over(): - var matstr: String = mat2str() - if !history_matrixes.has(matstr): - history_matrixes[matstr] = 1 - else: - history_matrixes[matstr] += 1 - Globals.checking_piece = null # reset checking_piece - Globals.in_check = false # reset in_check - matrix[8] = default_metadata.duplicate() # add the metadata to the matrix - matrix[8].turn = Globals.turn - check_in_check(true) # check if in_check - if !can_move(): - if Globals.in_check: - var winner := "black" if Globals.turn else "white" - status_label.text("%s won the game by checkmate" % winner) - win(winner) - else: - status_label.text("stalemate") - drawed() - elif threefoldrepetition(): - status_label.text("draw by threefold repetition") - drawed() - - func drawed(): Events.emit_signal("game_over") SoundFx.play("Draw") @@ -162,10 +177,6 @@ func can_move(): return false -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 @@ -175,16 +186,14 @@ func init_matrix(): # create the matrix add_pieces() # add the pieces -func make_piece(position: Vector2, script: String, sprite: String, white: bool = true): # make peace +func make_piece(position: Vector2, script: 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 pieces.add_child(piece) # add the piece to the grid - return piece # return the piece + matrix[position.y][position.x] = piece func init_board(): # create the board @@ -194,7 +203,6 @@ func init_board(): # create the board 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 @@ -204,82 +212,52 @@ func init_board(): # create the board func add_pieces(): # add the pieces add_pawns() - add_rooks() - add_knights() - add_bishops() + # add_rooks() + # add_knights() + # add_bishops() add_queens() add_kings() - print_matrix_pretty() func add_pawns(): for i in range(8): - 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) + make_piece(Vector2(i, 1), "res://pieces/Pawn.gd", false) + make_piece(Vector2(i, 6), "res://pieces/Pawn.gd", true) func add_rooks(): - 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) + make_piece(Vector2(0, 0), "res://pieces/Rook.gd", false) + make_piece(Vector2(7, 0), "res://pieces/Rook.gd", false) + make_piece(Vector2(0, 7), "res://pieces/Rook.gd", true) + make_piece(Vector2(7, 7), "res://pieces/Rook.gd", true) func add_knights(): - 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) + make_piece(Vector2(1, 0), "res://pieces/Knight.gd", false) + make_piece(Vector2(6, 0), "res://pieces/Knight.gd", false) + make_piece(Vector2(1, 7), "res://pieces/Knight.gd", true) + make_piece(Vector2(6, 7), "res://pieces/Knight.gd", true) func add_bishops(): - 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) + make_piece(Vector2(2, 0), "res://pieces/Bishop.gd", false) + make_piece(Vector2(5, 0), "res://pieces/Bishop.gd", false) + make_piece(Vector2(2, 7), "res://pieces/Bishop.gd", true) + make_piece(Vector2(5, 7), "res://pieces/Bishop.gd", true) func add_queens(): - 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) + make_piece(Vector2(3, 0), "res://pieces/Queen.gd", false) + make_piece(Vector2(3, 7), "res://pieces/Queen.gd", true) func add_kings(): - 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) + make_piece(Vector2(4, 0), "res://pieces/King.gd", false) + make_piece(Vector2(4, 7), "res://pieces/King.gd", true) Globals.white_king = matrix[7][4] # set the white king Globals.black_king = matrix[0][4] # set the black king -const topper_header = "┏━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┳━━━┓" -const middle_header = "┣━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫" -const middish_heads = "┗━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━╋━━━┫" -const bottom_header = "┗━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┛" -const smaller_heads = " ┗━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┻━━━┛" -const letter_header = " ┃ a ┃ b ┃ c ┃ d ┃ e ┃ f ┃ g ┃ h ┃" -const ender = " ┃ " - - -func print_matrix_pretty(mat = matrix): # print the matrix - for j in range(8): # for each row - var r = mat[j] # get the row - if j == 0: - print(topper_header) # print the top border - else: - print(middle_header) # print the middle border - var row = "┃ " + str(8 - j) + " ┃ " # init the string - for i in range(8): # for each column - var c = r[i] # get the column - if c: # if there is a piece - row += c.mininame + ender # add the shortname - else: # if there is no piece - row += " " + ender # add 00 - print(row) # print the string - print(middish_heads) - print(letter_header) - print(smaller_heads) - - func check_for_circle(position: Vector2): # check for a circle, validating movement return background_matrix[position.x][position.y].circle_on @@ -291,24 +269,34 @@ func check_for_frame(position: Vector2): # check for a frame, validating taking func square_clicked(position: Vector2): # square clicked + if promoting != null: + return 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 + if !is_instance_valid(last_clicked): # last clicked is null, so this is pointless return if check_for_frame(position): # takeable - last_clicked.take(matrix[position.y][position.x]) # eat - turn_over() + handle_take(position) if check_for_circle(position): # see if theres a circle at the position handle_move(position) # move 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 - if last_clicked: # remove the circles + if is_instance_valid(last_clicked): # remove the circles last_clicked.clear_clicked() last_clicked = spot # set it to the new spot spot.clicked() # tell the piece shit happeend +func handle_take(position): + if last_clicked is Pawn: + var pawn = last_clicked + if check_promote(pawn, position, "take"): + return + last_clicked.take(matrix[position.y][position.x]) # eat + turn_over() + + func handle_move(position): if last_clicked is King and last_clicked.can_castle: for i in range(len(last_clicked.can_castle)): @@ -320,19 +308,32 @@ func handle_move(position): castle_data[1].override_moveto = false turn_over() return - if last_clicked is Pawn and last_clicked.enpassant: - for i in range(len(last_clicked.enpassant)): - var en_passant_data = last_clicked.enpassant[i] - if en_passant_data[0] == position: - en_passant_data[1].took() # kill the unfortunate - last_clicked.passant(en_passant_data[0]) - turn_over() - return + if last_clicked is Pawn: + var pawn = last_clicked + if pawn.enpassant: + for i in range(len(pawn.enpassant)): + var en_passant_data = pawn.enpassant[i] + if en_passant_data[0] == position: + en_passant_data[1].took() # kill the unfortunate + pawn.passant(en_passant_data[0]) + turn_over() + return + if check_promote(pawn, position): + return last_clicked.moveto(position) turn_over() +func check_promote(pawn, position, calltype: String = "move"): + if pawn.can_promote(position): + pawn.promote(position, calltype) + promoting = position + return true + return false + + func turn_over(): + promoting = null Events.emit_signal("just_before_turn_over") Globals.add_turn() Globals.turn = not Globals.turn @@ -349,11 +350,32 @@ func clear_fx(): # clear the circles piece.set_frame(false) # clear the frame -func _input(event): # input - if event.is_action_released("debug"): # if debug - print_matrix_pretty() # print the matrix - if event.is_action_released("kill"): - if last_clicked: - last_clicked.took() # kill the piece - last_clicked = null - clear_fx() # clear the circles +func _on_outoftime(who): + if who == "white": + win("black") + else: + win("white") + + +func _on_turn_over(): + var matstr: String = mat2str() + if !history_matrixes.has(matstr): + history_matrixes[matstr] = 1 + else: + history_matrixes[matstr] += 1 + Globals.checking_piece = null # reset checking_piece + Globals.in_check = false # reset in_check + matrix[8] = default_metadata.duplicate() # add the metadata to the matrix + matrix[8].turn = Globals.turn + check_in_check(true) # check if in_check + if !can_move(): + if Globals.in_check: + var winner := "black" if Globals.turn else "white" + status_label.text("%s won the game by checkmate" % winner) + win(winner) + else: + status_label.text("stalemate") + drawed() + elif threefoldrepetition(): + status_label.text("draw by threefold repetition") + drawed() @@ -8,7 +8,7 @@ onready var area := $Squarea onready var areacollisionshape := $Squarea/CollisionShape2D onready var circle := $Circle -signal clicked(real_position) +signal clicked func _ready(): @@ -20,8 +20,8 @@ func _ready(): algebraic_string = Utils.calculate_algebraic_position(real_position) -func _on_Squarea_input_event(_viewport: Node, event: InputEvent, _shape_idx: int): - if event is InputEventMouseButton: +func _on_Squarea_input_event(_viewport: Node, _event: InputEvent, _shape_idx: int): + if Input.is_action_just_pressed("click"): emit_signal("clicked", real_position) diff --git a/Square.tscn b/Square.tscn index 3ee74a3..c4f99b0 100644 --- a/Square.tscn +++ b/Square.tscn @@ -32,7 +32,7 @@ shader_param/color = Color( 0.431373, 0.584314, 0.388235, 0.639216 ) [node name="Square" type="ColorRect"] anchor_right = 1.0 anchor_bottom = 1.0 -margin_right = -780.0 +margin_right = -1180.0 margin_bottom = -780.0 mouse_filter = 2 script = ExtResource( 1 ) @@ -1,3 +1,4 @@ +signal newmove extends Node var turn_moves: PoolStringArray = [] @@ -5,21 +6,11 @@ var turns_moves := [] var counter := 0 -signal newmove - func _ready(): Events.connect("turn_over", self, "_on_turn_over") -func _on_turn_over(): - counter += 1 - if counter >= 2: - counter = 0 - turns_moves.append(turn_moves.join(" ")) - turn_moves.resize(0) - - func is_pawn(inode): return inode is Pawn @@ -77,8 +68,15 @@ func format_seconds(time: float, use_milliseconds: bool = false): if not use_milliseconds: return "%02d:%02d" % [minutes, seconds] - return "%02d:%04.1f" % [minutes, seconds] func round_place(num, places): return round(num * pow(10, places)) / pow(10, places) + + +func _on_turn_over(): + counter += 1 + if counter >= 2: + counter = 0 + turns_moves.append(turn_moves.join(" ")) + turn_moves.resize(0) @@ -14,4 +14,13 @@ script = ExtResource( 1 ) [node name="Foreground" type="CanvasLayer" parent="Grid"] +[node name="Darken" type="ColorRect" parent="Grid"] +visible = false +anchor_right = 1.0 +anchor_bottom = 1.0 +rect_min_size = Vector2( 800, 800 ) +mouse_filter = 2 +mouse_default_cursor_shape = 7 +color = Color( 0, 0, 0, 0.784314 ) + [node name="UI" parent="." instance=ExtResource( 2 )] diff --git a/pieces/King.gd b/pieces/King.gd index 9ab9091..cfc7ade 100644 --- a/pieces/King.gd +++ b/pieces/King.gd @@ -5,6 +5,10 @@ var castle_check := true var can_castle := [] +func _ready(): + Events.connect("just_before_turn_over", self, "just_before_over") + + func get_moves(): var moves := [] for i in all_dirs(): @@ -36,10 +40,6 @@ func just_before_over(): # assign metadata for threefold repetition draw check Globals.grid.matrix[8].bccr = true -func _ready(): - Events.connect("just_before_turn_over", self, "just_before_over") - - func castleing(): var moves := [] var rooks := [pos_around(Vector2.RIGHT * 3), pos_around(Vector2.LEFT * 4)] diff --git a/pieces/Pawn.gd b/pieces/Pawn.gd index 8571e74..42ed456 100644 --- a/pieces/Pawn.gd +++ b/pieces/Pawn.gd @@ -1,12 +1,31 @@ extends Piece class_name Pawn, "res://assets/pieces/california/wP.png" -onready var whiteint := 1 if white else -1 +const promotables := ["Q.png", "N.png", "R.png", "B.png"] var twostepfirstmove := false var just_set := false var enpassant := [] +onready var whiteint := 1 if white else -1 +onready var sprites := [] +onready var darken = get_node("../../Darken") + + +func _ready(): + Events.connect("turn_over", self, "_on_turn_over") + Events.connect("just_before_turn_over", self, "_just_before_turn_over") + sprite.position = Globals.grid.piece_size / 2 + for i in range(0, 4): # add 3 sprites + var newsprite = load("res://ClickableSprite.tscn").instance() + newsprite.position = (sprite.position + Vector2(0, (i * Globals.grid.piece_size.y) * whiteint)) + newsprite.name = "Sprite%s" % str(i) + newsprite.connect("clicked", self, "handle_sprite_input_event") + newsprite.z_index = 5 + newsprite.hide() + add_child(newsprite) + sprites.append(newsprite) + func moveto(position, real = true, take = false): # check if 2 step @@ -20,14 +39,6 @@ func moveto(position, real = true, take = false): .moveto(position, real, take) -func _on_turn_over(): - if just_set: - just_set = false - return - if twostepfirstmove: - twostepfirstmove = false - - func get_moves(): var points := [Vector2.UP, Vector2.UP * 2] var moves := [] @@ -35,7 +46,7 @@ func get_moves(): var point: Vector2 = points[i] point *= whiteint point = pos_around(point) - if at_pos(point) == null: + if is_on_board(point) and at_pos(point) == null: if i == 1 and has_moved or at_pos(pos_around(points[0] * whiteint)) != null: continue if check_spots_check and checkcheck(point): @@ -46,6 +57,12 @@ func get_moves(): return moves +func can_promote(position): + if position.y >= 7 or position.y <= 0: + return true + return false + + func passant(position): enpassant.clear() moveto(position) @@ -72,14 +89,10 @@ func en_passant(turncheck = true): # in passing var passants := [pos_around(Vector2.LEFT), pos_around(Vector2.RIGHT)] var moves := [] for i in passants: - if !is_on_board(i): + if !is_on_board(i) or !at_pos(i): continue var spot = at_pos(i) - if !spot: - continue - if spot.white == white: - continue - if !Utils.is_pawn(spot): + if spot.white == white or !Utils.is_pawn(spot): continue if turncheck and white != Globals.turn: continue @@ -94,6 +107,46 @@ func en_passant(turncheck = true): # in passing return moves +func promote(position, type): + if type == "take": + take(at_pos(position)) + else: + moveto(position) + darken.show() + for i in range(len(promotables)): + sprites[i].sprite.texture = load("%s%s%s" % [Globals.grid.ASSETS_PATH, team.to_lower(), promotables[i]]) + sprites[i].show() + + +func handle_sprite_input_event(node): + darken.hide() + var script = piece(promotables[sprites.find(node)][0]) + Globals.grid.make_piece(real_position, script, white) + Globals.grid.turn_over() + clear_clicked() + queue_free() + + +func piece(string): + match string: + "Q": + return "res://pieces/Queen.gd" + "N": + return "res://pieces/Knight.gd" + "R": + return "res://pieces/Rook.gd" + "B": + return "res://pieces/Bishop.gd" + + +func _on_turn_over(): + if just_set: + just_set = false + return + if twostepfirstmove: + twostepfirstmove = false + + func _just_before_turn_over(): var had_a_enpassant := len(enpassant) > 0 enpassant.clear() @@ -108,8 +161,3 @@ func _just_before_turn_over(): Globals.grid.matrix[8].wcep.append_array(temporary) else: Globals.grid.matrix[8].bcep.append_array(temporary) - - -func _ready(): - Events.connect("turn_over", self, "_on_turn_over") - Events.connect("just_before_turn_over", self, "_just_before_turn_over") diff --git a/pieces/Piece.gd b/pieces/Piece.gd index 8c5565a..7c9d207 100644 --- a/pieces/Piece.gd +++ b/pieces/Piece.gd @@ -6,13 +6,13 @@ var white := true var shortname := "" var mininame := "♙" var has_moved := false -var sprite: Sprite var frameon := false var team := "w" var check_spots_check := true var no_enemys := false var override_moveto := false +onready var sprite := $Sprite onready var tween := $Tween onready var anim := $AnimationPlayer onready var colorrect := $ColorRect @@ -20,6 +20,8 @@ onready var frame := $Frame func _ready(): + team = "w" if white else "b" + sprite.position = Globals.grid.piece_size / 2 var tmp: Array = Utils.get_node_name(self) mininame = tmp[0] shortname = tmp[1] @@ -27,6 +29,11 @@ func _ready(): frame.modulate = Globals.grid.overlay_color colorrect.color = Globals.grid.overlay_color colorrect.rect_size = Globals.grid.piece_size + load_texture() + + +func load_texture(path := "%s%s%s.png" % [Globals.grid.ASSETS_PATH, team.to_lower(), shortname.to_upper()]): + sprite.texture = load(path) func clicked(): @@ -184,8 +191,8 @@ func checkcheck(pos): # moves to position, then checks if your king is in check return false -func is_on_board(vector: Vector2): - if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7: # limit the vector to the board +func is_on_board(vector: Vector2): # limit the vector to the board + if vector.y < 0 or vector.y > 7 or vector.x < 0 or vector.x > 7: return false return true diff --git a/project.godot b/project.godot index d9cac7d..bad9727 100644 --- a/project.godot +++ b/project.godot @@ -50,7 +50,7 @@ _global_script_classes=[ { "path": "res://pieces/Rook.gd" } ] _global_script_class_icons={ -"Bishop": "res://assets/california/wB.png", +"Bishop": "res://assets/pieces/california/wB.png", "Grid": "", "King": "res://assets/pieces/california/wK.png", "Knight": "res://assets/pieces/california/wN.png", @@ -140,6 +140,11 @@ fullscreen={ "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":70,"physical_scancode":0,"unicode":0,"echo":false,"script":null) ] } +click={ +"deadzone": 0.5, +"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null) + ] +} [rendering] diff --git a/ui/GameUI.tscn b/ui/GameUI.tscn index 51cf767..9fc8a58 100644 --- a/ui/GameUI.tscn +++ b/ui/GameUI.tscn @@ -108,3 +108,10 @@ color = Color( 0, 0, 0, 1 ) [node name="Timer" type="Node" parent="Holder/Back/VBox"] script = ExtResource( 5 ) + +[node name="Darken" type="ColorRect" parent="."] +visible = false +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -400.0 +color = Color( 0, 0, 0, 0.784314 ) diff --git a/ui/MovesList.gd b/ui/MovesList.gd index 1e47843..9af2cd9 100644 --- a/ui/MovesList.gd +++ b/ui/MovesList.gd @@ -1,7 +1,7 @@ extends ItemList +var tween: Tween onready var scrollbar = get_v_scroll() -var tween: Tween func _ready(): diff --git a/ui/StartMenu.gd b/ui/StartMenu.gd index 9228cc3..2bbcabe 100644 --- a/ui/StartMenu.gd +++ b/ui/StartMenu.gd @@ -12,10 +12,6 @@ onready var tween := $Tween onready var timer := $Timer -func _on_local_pressed(): - get_tree().change_scene_to(world) - - func _ready(): randomize() colorrect.color = nice_colors[randi() % nice_colors.size()] @@ -23,6 +19,14 @@ func _ready(): _on_Timer_timeout() +func rand(clr): + return clamp(clr + rand_range(0, .1) if randi() % 2 else clr - rand_range(0, .1), 0, 1) + + +func _on_local_pressed(): + get_tree().change_scene_to(world) + + func _on_quit_pressed(): get_tree().quit() @@ -39,7 +43,3 @@ func _on_Timer_timeout(): tween.interpolate_property(colorrect, "color", colorrect.color, clr, timer_length, Tween.TRANS_ELASTIC) tween.start() timer.start(timer_length) - - -func rand(clr): - return clamp(clr + rand_range(0, .1) if randi() % 2 else clr - rand_range(0, .1), 0, 1) diff --git a/ui/Timer.gd b/ui/Timer.gd index b34cc25..de71415 100644 --- a/ui/Timer.gd +++ b/ui/Timer.gd @@ -9,6 +9,12 @@ onready var whitelabel := $"../WhiteTime" onready var blacklabel := $"../BlackTime" +func _ready(): + whitelabel.time = 300 + blacklabel.time = 300 + Events.connect("turn_over", self, "turn_over") + + func _process(delta): if !enabled: return @@ -20,12 +26,6 @@ func _process(delta): enabled = false -func _ready(): - whitelabel.time = 300 - blacklabel.time = 300 - Events.connect("turn_over", self, "turn_over") - - func turn_over(): time_elapsed = 0.0 count += 1 |