ChessBuilder/addons/Chess/Scripts/Knight.gd

123 lines
No EOL
3.6 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 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.tileXSize)
var cell_delta_y = int(total_delta.y / game.tileYSize)
if abs(cell_delta_x) > abs(cell_delta_y):
# Moving more horizontally ([-2, ±1])
mid_pos = Vector2(
start_pos.x + (cell_delta_x * game.tileXSize),
start_pos.y
)
else:
# Moving more vertically ([±1, -2])
mid_pos = Vector2(
start_pos.x,
start_pos.y + (cell_delta_y * game.tileYSize)
)
# 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