ChessBuilder/addons/Chess/Scripts/King.gd

92 lines
3.2 KiB
GDScript

@tool
extends Pawn
class_name King
var Castling = true
func _ready():
self.texture = load("res://addons/Chess/Textures/WKing.svg")
Points = 10
func _process(_delta):
if Item_Color != Temp_Color:
Temp_Color = Item_Color
if Item_Color == 0:
self.texture = load("res://addons/Chess/Textures/WKing.svg")
elif Item_Color == 1:
self.texture = load("res://addons/Chess/Textures/BKing.svg")
# King.gd
func getValidMoves(board_flow, current_location: String) -> Dictionary:
var moves = {
"regular_moves": [],
"special_moves": []
}
var loc = current_location.split("-")
var x = int(loc[0])
var y = int(loc[1])
# Regular king moves (unchanged)
var king_dirs = [[0,1], [0,-1], [1,0], [-1,0], [1,1], [1,-1], [-1,1], [-1,-1]]
for dir in king_dirs:
var new_loc = str(x + dir[0]) + "-" + str(y + dir[1])
if is_valid_cell(board_flow, new_loc):
if can_move_to_cell(board_flow, new_loc) || can_move_to_cell(board_flow, new_loc, true):
moves.regular_moves.append(new_loc)
# Castling logic
if Castling:
# Kingside castling (right side)
if check_kingside_castle(board_flow, x, y):
# Add rook's current position to regular moves
moves.regular_moves.append(str(x + 3) + "-" + str(y))
# Add king and rook destinations to special moves as individual items
moves.special_moves.append(str(x + 1) + "-" + str(y)) # Rook's destination
moves.special_moves.append(str(x + 2) + "-" + str(y)) # King's destination
# Queenside castling (left side)
if check_queenside_castle(board_flow, x, y):
# Add rook's current position to regular moves
moves.regular_moves.append(str(x - 4) + "-" + str(y))
# Add king and rook destinations to special moves as individual items
moves.special_moves.append(str(x - 1) + "-" + str(y)) # Rook's destination
moves.special_moves.append(str(x - 2) + "-" + str(y)) # King's destination
return moves
func check_kingside_castle(board_flow, x: int, y: int) -> bool:
# Check if path is clear
for i in range(1, 3):
var check_pos = str(x + i) + "-" + str(y)
if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos):
return false
var container = board_flow.get_node(check_pos) as PieceContainer
if container.has_piece():
return false
var rook_pos = str(x + 3) + "-" + str(y)
if !is_valid_cell(board_flow, rook_pos):
return false
var rook_container = board_flow.get_node(rook_pos) as PieceContainer
var rook = rook_container.get_piece()
return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color
func check_queenside_castle(board_flow, x: int, y: int) -> bool:
# Check if path is clear
for i in range(1, 4):
var check_pos = str(x - i) + "-" + str(y)
if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos):
return false
var container = board_flow.get_node(check_pos) as PieceContainer
if container.has_piece():
return false
# Check if rook is in position and hasn't moved
var rook_pos = str(x - 4) + "-" + str(y)
if !is_valid_cell(board_flow, rook_pos):
return false
var rook_container = board_flow.get_node(rook_pos) as PieceContainer
var rook = rook_container.get_piece()
return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color