Rook.gd 2.19 KiB
extends Piece
class_name Rook
enum Upgrade {
DIAGONAL_MOVE_ONE,
}
func _init(is_white: bool, position: Vector2):
super._init(is_white, position)
# Testing upgrades
# upgrades.append(Upgrade.DIAGONAL_MOVE_ONE)
func get_valid_moves(board_state: Array, pos: Vector2) -> Array[Vector2]:
var valid_moves: Array[Vector2] = []
var x = pos.x
var y = pos.y
# Check moves to the right
for i in range(x + 1, 8):
if board_state[i][y] == null:
valid_moves.append(Vector2(i, y))
elif board_state[i][y] != null and not board_state[i][y].is_white:
valid_moves.append(Vector2(i, y))
break
else:
break
# Check moves to the left
for i in range(x - 1, -1, -1):
if board_state[i][y] == null:
valid_moves.append(Vector2(i, y))
elif board_state[i][y] != null and not board_state[i][y].is_white:
valid_moves.append(Vector2(i, y))
break
else:
break
# Check moves upwards
for i in range(y - 1, -1, -1):
# Check if the square is empty
if board_state[x][i] == null:
valid_moves.append(Vector2(x, i))
# Check if the square has an enemy piece
elif board_state[x][i] != null and not board_state[x][i].is_white:
valid_moves.append(Vector2(x, i))
break
else:
break
# Check moves downwards
for i in range(y + 1, 8):
# Check if the square is empty
if board_state[x][i] == null:
valid_moves.append(Vector2(x, i))
# Check if the square has an enemy piece
elif board_state[x][i] != null and not board_state[x][i].is_white:
valid_moves.append(Vector2(x, i))
break
else:
break
# Check for DIAGONAL_MOVE_ONE upgrade
if Upgrade.DIAGONAL_MOVE_ONE in upgrades:
if x > 0 and y > 0 and (board_state[x - 1][y - 1] == null or not board_state[x - 1][y - 1].is_white):
valid_moves.append(Vector2(x - 1, y - 1))
if x < 7 and y > 0 and (board_state[x + 1][y - 1] == null or not board_state[x + 1][y - 1].is_white):
valid_moves.append(Vector2(x + 1, y - 1))
if x > 0 and y < 7 and (board_state[x - 1][y + 1] == null or not board_state[x - 1][y + 1].is_white):
valid_moves.append(Vector2(x - 1, y + 1))
if x < 7 and y < 7 and (board_state[x + 1][y + 1] == null or not board_state[x + 1][y + 1].is_white):
valid_moves.append(Vector2(x + 1, y + 1))
return valid_moves