ChessBuilder/Game.gd
2025-01-24 20:06:23 -06:00

214 lines
7 KiB
GDScript

extends Control
const CardDisplay = preload("res://Systems/CardDisplay.gd")
var Selected_Node = ""
var Turn = 0
var Location_X = ""
var Location_Y = ""
var turn_indicator: ColorRect
var p1String: RichTextLabel
var p2String: RichTextLabel
var p1Points = 0
var p2Points = 0
var pos = Vector2(25, 25)
var Areas: PackedStringArray
# this is seperate the Areas for special circumstances, like castling.
var Special_Area: PackedStringArray
var light_style = StyleBoxFlat.new()
var dark_style = StyleBoxFlat.new()
var highlight_style = StyleBoxFlat.new()
var deck_manager: DeckManager
var card_display: CardDisplay
var has_moved = false # Track if piece has moved this turn
var currently_moving_piece = null
func _ready():
deck_manager = DeckManager.new()
card_display = CardDisplay.new()
add_child(deck_manager)
add_child(card_display)
card_display.update_hand(deck_manager.hand)
deck_manager.connect("hand_updated", func(hand): card_display.update_hand(hand))
light_style.bg_color = Color(0.8, 0.8, 0.8, 1)
dark_style.bg_color = Color(0.2, 0.2, 0.2, 1)
highlight_style.bg_color = Color(0, 0.3, 0, 1)
# Get reference to the turn indicator
turn_indicator = $TurnIndicator
p1String = $Player1Points
p2String = $Player2Points
p1String.text = "0"
p2String.text = "0"
# Set initial color for white's turn
update_turn_indicator()
func update_turn_indicator():
if Turn == 0: # White's turn
turn_indicator.color = Color(1, 1, 1, 1) # White
else: # Black's turn
turn_indicator.color = Color(0, 0, 0, 1) # Black
func reset_highlights():
var Flow = get_node("Flow")
for button in Flow.get_children():
var coord = button.name.split("-")
var isWhiteSquare = (int(coord[0]) + int(coord[1])) % 2 == 0
if isWhiteSquare:
button.add_theme_stylebox_override("normal", light_style)
else:
button.add_theme_stylebox_override("normal", dark_style)
func _on_flow_send_location(location: String):
if Selected_Node != "" && Selected_Node == location:
reset_highlights()
Selected_Node = ""
return
# variables for later
var number = 0
Location_X = ""
var node = get_node("Flow/" + location)
# This is to try and grab the X and Y coordinates from the board
while location.substr(number, 1) != "-":
Location_X += location.substr(number, 1)
number += 1
Location_Y = location.substr(number + 1)
# Now... we need to figure out how to select the pieces. If there is a valid move, do stuff.
# If we re-select, just go to that other piece
if Selected_Node == "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn:
Selected_Node = location
Get_Moveable_Areas()
elif Selected_Node != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn && node.get_child(0).name == "Rook":
# Castling
for i in Areas:
if i == node.name:
var king = get_node("Flow/" + Selected_Node).get_child(0)
var rook = node.get_child(0)
# Using a seperate array because Areas wouldn't be really consistant...
king.reparent(get_node("Flow/" + Special_Area[1]))
rook.reparent(get_node("Flow/" + Special_Area[0]))
king.position = pos
rook.position = pos
# We have to get the parent because it will break lmao.
Update_Game(king.get_parent())
# En Passant
elif Selected_Node != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != Turn && node.get_child(0).name == "Pawn" && Special_Area.size() != 0 && Special_Area[0] == node.name && node.get_child(0).get("En_Passant") == true:
for i in Special_Area:
if i == node.name:
var pawn = get_node("Flow/" + Selected_Node).get_child(0)
node.get_child(0).free()
pawn.reparent(get_node("Flow/" + Special_Area[1]))
pawn.position = pos
Update_Game(pawn.get_parent())
elif Selected_Node != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn:
# Re-select
Selected_Node = location
Get_Moveable_Areas()
elif Selected_Node != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != Turn:
# Taking over a piece
for i in Areas:
if i == node.name:
var Piece = get_node("Flow/" + Selected_Node).get_child(0)
var captured_piece = node.get_child(0)
# Add points to the capturing player
if Turn == 0: # White's turn
p1Points += captured_piece.Points
p1String.text = str(p1Points)
else: # Black's turn
p2Points += captured_piece.Points
p2String.text = str(p2Points)
# Win conditions
if captured_piece.name == "King":
print("Damn, you win!")
node.get_child(0).free()
Piece.reparent(node)
Piece.position = pos
Update_Game(node)
elif Selected_Node != "" && node.get_child_count() == 0:
# Moving a piece
for i in Areas:
if i == node.name:
var Piece = get_node("Flow/" + Selected_Node).get_child(0)
Piece.reparent(node)
Piece.position = pos
Update_Game(node)
func Update_Game(node):
Selected_Node = ""
if Turn == 0:
Turn = 1
else:
Turn = 0
update_turn_indicator()
reset_highlights();
deck_manager.update_card_durations()
has_moved = false
currently_moving_piece = null
var Flow = get_node("Flow")
# get the en-passantable pieces and undo them
var things = Flow.get_children()
for i in things:
if i.get_child_count() != 0 && i.get_child(0).name == "Pawn" && i.get_child(0).Item_Color == Turn && i.get_child(0).En_Passant == true:
i.get_child(0).set("En_Passant", false)
# Remove the abilities once they are either used or not used
if node.get_child(0).name == "Pawn":
if node.get_child(0).Double_Start == true:
node.get_child(0).En_Passant = true
node.get_child(0).Double_Start = false
if node.get_child(0).name == "King":
node.get_child(0).Castling = false
if node.get_child(0).name == "Rook":
node.get_child(0).Castling = false
# Below is the movement that is used for the pieces
func Get_Moveable_Areas():
var Flow = get_node("Flow")
for button in Flow.get_children():
var coord = button.name.split("-")
var isWhiteSquare = (int(coord[0]) + int(coord[1])) % 2 == 0
if isWhiteSquare:
button.add_theme_stylebox_override("normal", light_style)
else:
button.add_theme_stylebox_override("normal", dark_style)
# Clearing the arrays
Areas.clear()
Special_Area.clear()
var Piece = get_node("Flow/" + Selected_Node).get_child(0)
# For the selected piece that we have, we can get the movement that we need here.
var moves = Piece.get_valid_moves(Flow, Selected_Node)
Areas = moves.regular_moves
Special_Area = moves.special_moves
# Highlight valid moves
for move in Areas:
var button = Flow.get_node(move)
var isWhiteSquare = (int(move.split("-")[0]) + int(move.split("-")[1])) % 2 == 0
var baseStyle = dark_style
if isWhiteSquare:
baseStyle = light_style
var combinedStyle = StyleBoxFlat.new()
combinedStyle.bg_color = baseStyle.bg_color + highlight_style.bg_color
button.add_theme_stylebox_override("normal", combinedStyle)
# One function that shortens everything. Its also a pretty good way to see if we went off the board or not.
func Is_Null(Location):
if get_node_or_null("Flow/" + Location) == null:
return true
else:
return false