115 lines
3.2 KiB
GDScript
115 lines
3.2 KiB
GDScript
@tool
|
|
extends Pawn
|
|
class_name Knight
|
|
|
|
func _ready():
|
|
self.texture = load("res://addons/Chess/Textures/WKnight.svg")
|
|
Points = 3
|
|
|
|
func _process(_delta):
|
|
if Item_Color != Temp_Color:
|
|
Temp_Color = Item_Color
|
|
if Item_Color == 0:
|
|
self.texture = load("res://addons/Chess/Textures/WKnight.svg")
|
|
elif Item_Color == 1:
|
|
self.texture = load("res://addons/Chess/Textures/BKnight.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])
|
|
var game = board_flow.get_parent() as ChessGame
|
|
|
|
# All possible L-shaped moves
|
|
var knight_moves = [
|
|
[-2, -1], [-2, 1],
|
|
[-1, -2], [-1, 2],
|
|
[1, -2], [1, 2],
|
|
[2, -1], [2, 1]
|
|
]
|
|
|
|
for move in knight_moves:
|
|
var target_x = x + move[0]
|
|
var target_y = y + move[1]
|
|
var new_loc = str(target_x) + "-" + str(target_y)
|
|
|
|
if is_valid_cell(board_flow, new_loc):
|
|
# Check tiles in the path
|
|
var path_clear = true
|
|
|
|
# Check horizontally first, then vertically (or vice versa)
|
|
var check_horizontal_first = abs(move[0]) > abs(move[1])
|
|
|
|
if check_horizontal_first:
|
|
# Check horizontal movement
|
|
var step_x = sign(move[0])
|
|
for i in range(1, abs(move[0]) + 1):
|
|
var path_tile_loc = str(x + (i * step_x)) + "-" + str(y)
|
|
var tile = game.tileManager.get_tile(path_tile_loc)
|
|
if tile && !tile.jumpable:
|
|
path_clear = false
|
|
break
|
|
|
|
# Check vertical movement if path still clear
|
|
if path_clear:
|
|
var step_y = sign(move[1])
|
|
var path_tile_loc = str(target_x) + "-" + str(y + step_y)
|
|
var tile = game.tileManager.get_tile(path_tile_loc)
|
|
if tile && !tile.jumpable:
|
|
path_clear = false
|
|
else:
|
|
# Check vertical movement
|
|
var step_y = sign(move[1])
|
|
for i in range(1, abs(move[1]) + 1):
|
|
var path_tile_loc = str(x) + "-" + str(y + (i * step_y))
|
|
var tile = game.tileManager.get_tile(path_tile_loc)
|
|
if tile && !tile.jumpable:
|
|
path_clear = false
|
|
break
|
|
|
|
# Check horizontal movement if path still clear
|
|
if path_clear:
|
|
var step_x = sign(move[0])
|
|
var path_tile_loc = str(x + step_x) + "-" + str(target_y)
|
|
var tile = game.tileManager.get_tile(path_tile_loc)
|
|
if tile && !tile.jumpable:
|
|
path_clear = false
|
|
|
|
# Only add the move if the path is clear and the destination is valid
|
|
if path_clear && (can_move_to_cell(board_flow, new_loc) || can_move_to_cell(board_flow, new_loc, true)):
|
|
moves.regular_moves.append(new_loc)
|
|
|
|
return moves
|
|
|
|
|
|
|
|
# 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])
|
|
|
|
# # All possible L-shaped moves
|
|
# var knight_moves = [
|
|
# [-2, -1], [-2, 1],
|
|
# [-1, -2], [-1, 2],
|
|
# [1, -2], [1, 2],
|
|
# [2, -1], [2, 1]
|
|
# ]
|
|
|
|
# for move in knight_moves:
|
|
# var new_loc = str(x + move[0]) + "-" + str(y + move[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)
|
|
|
|
# return moves
|