@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]) 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 both possible paths to the target var horizontal_first_clear = true var vertical_first_clear = true # Path 1: Check horizontal first, then vertical # 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: horizontal_first_clear = false break # Check vertical movement if horizontal was clear if horizontal_first_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: horizontal_first_clear = false # Path 2: Check vertical first, then horizontal # 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: vertical_first_clear = false break # Check horizontal movement if vertical was clear if vertical_first_clear: var path_tile_loc = str(x + step_x) + "-" + str(target_y) var tile = game.tileManager.get_tile(path_tile_loc) if tile && !tile.jumpable: vertical_first_clear = false # Move is valid if EITHER path is clear if (horizontal_first_clear || vertical_first_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 animate_movement(target_position: Vector2, duration: float = 0.5) -> void: # z_index = 1 # var tween = create_tween() # tween.set_trans(Tween.TRANS_LINEAR) # tween.set_ease(Tween.EASE_IN_OUT) # var start_pos = global_position # var total_delta = target_position - start_pos # var mid_pos: Vector2 # var game = get_tree().get_first_node_in_group("ChessGame") as ChessGame # var cell_delta_x = int(total_delta.x / game.boardXSize) # var cell_delta_y = int(total_delta.y / game.boardYSize) # if abs(cell_delta_x) > abs(cell_delta_y): # # Moving more horizontally ([-2, ±1]) # mid_pos = Vector2( # start_pos.x + (cell_delta_x * game.boardXSize), # start_pos.y # ) # else: # # Moving more vertically ([±1, -2]) # mid_pos = Vector2( # start_pos.x, # start_pos.y + (cell_delta_y * game.boardYSize) # ) # # First move (longer distance) # tween.tween_property(self, "global_position", mid_pos, duration / 3 * 2) # # Second move (shorter distance) # tween.tween_property(self, "global_position", target_position, duration / 3 * 1) # await tween.finished # z_index = 0 func animate_movement(target_position: Vector2, duration: float = 0.5) -> void: z_index = 1 var tween = create_tween() tween.set_trans(Tween.TRANS_LINEAR) tween.set_ease(Tween.EASE_IN_OUT) var start_pos = global_position var total_delta = target_position - start_pos var mid_pos: Vector2 var cell_delta_x = int(total_delta.x / game.boardXSize) var cell_delta_y = int(total_delta.y / game.boardYSize) # Convert current and target positions to board coordinates var current_x = int(start_pos.x / game.boardXSize) var current_y = int(start_pos.y / game.boardYSize) # Check both possible paths var horizontal_first_clear = true var vertical_first_clear = true # Check horizontal-first path for i in range(1, abs(cell_delta_x) + 1): var step_x = sign(cell_delta_x) var path_tile_loc = str(current_x + (i * step_x)) + "-" + str(current_y) var tile = game.tileManager.get_tile(path_tile_loc) if tile && !tile.jumpable: horizontal_first_clear = false break if horizontal_first_clear: var step_y = sign(cell_delta_y) var path_tile_loc = str(current_x + cell_delta_x) + "-" + str(current_y + step_y) var tile = game.tileManager.get_tile(path_tile_loc) if tile && !tile.jumpable: horizontal_first_clear = false # Check vertical-first path for i in range(1, abs(cell_delta_y) + 1): var step_y = sign(cell_delta_y) var path_tile_loc = str(current_x) + "-" + str(current_y + (i * step_y)) var tile = game.tileManager.get_tile(path_tile_loc) if tile && !tile.jumpable: vertical_first_clear = false break if vertical_first_clear: var step_x = sign(cell_delta_x) var path_tile_loc = str(current_x + step_x) + "-" + str(current_y + cell_delta_y) var tile = game.tileManager.get_tile(path_tile_loc) if tile && !tile.jumpable: vertical_first_clear = false # Choose the valid path for animation var use_horizontal_first = true if !horizontal_first_clear && vertical_first_clear: use_horizontal_first = false elif horizontal_first_clear && vertical_first_clear: # If both paths are valid, use the traditional logic use_horizontal_first = abs(cell_delta_x) > abs(cell_delta_y) elif !horizontal_first_clear && !vertical_first_clear: push_error("Attempting to animate an invalid move!") return if use_horizontal_first: mid_pos = Vector2( start_pos.x + (cell_delta_x * game.boardXSize), start_pos.y ) else: mid_pos = Vector2( start_pos.x, start_pos.y + (cell_delta_y * game.boardYSize) ) tween.tween_property(self, "global_position", mid_pos, duration / 3 * 2) tween.tween_property(self, "global_position", target_position, duration / 3 * 1) await tween.finished z_index = 0