94 lines
3.1 KiB
GDScript
94 lines
3.1 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
|
|
|
|
# Check if rook is in position and hasn't moved
|
|
var rook_pos = str(x + 3) + "-" + str(y)
|
|
if !is_valid_cell(board_flow, rook_pos):
|
|
return false
|
|
|
|
var rook_node = board_flow.get_node(rook_pos)
|
|
if rook_node.get_child_count() != 1:
|
|
return false
|
|
|
|
var rook = rook_node.get_child(0)
|
|
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
|
|
|
|
# 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_node = board_flow.get_node(rook_pos)
|
|
if rook_node.get_child_count() != 1:
|
|
return false
|
|
|
|
var rook = rook_node.get_child(0)
|
|
return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color
|