ChessBuilder/addons/Chess/Scripts/Rook.gd

60 lines
1.6 KiB
GDScript

@tool
extends Pawn
class_name Rook
var Castling = true
func _ready():
self.texture = load("res://addons/Chess/Textures/WRook.svg")
Points = 5
func _process(_delta):
if Item_Color != Temp_Color:
Temp_Color = Item_Color
if Item_Color == 0:
self.texture = load("res://addons/Chess/Textures/WRook.svg")
elif Item_Color == 1:
self.texture = load("res://addons/Chess/Textures/BRook.svg")
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])
# Check all four directions
var directions = [[0,1], [0,-1], [1,0], [-1,0]]
for dir in directions:
var curr_x = x
var curr_y = y
while true:
curr_x += dir[0]
curr_y += dir[1]
var new_loc = str(curr_x) + "-" + str(curr_y)
if !is_valid_cell(board_flow, new_loc):
break
if can_move_to_cell(board_flow, new_loc):
moves.regular_moves.append(new_loc)
elif can_move_to_cell(board_flow, new_loc, true):
moves.regular_moves.append(new_loc)
break
else:
break
if Castling:
# Check if there's a king that can castle
for direction in [-4, 3]: # Check both sides for king
var king_pos = str(x + direction) + "-" + str(y)
if is_valid_cell(board_flow, king_pos):
var king_container = board_flow.get_node(king_pos) as PieceContainer
var piece = king_container.get_piece()
if piece != null && piece.name == "King" && piece.Castling && piece.Item_Color == self.Item_Color:
moves.special_moves.append([king_pos, current_location])
return moves