ChessBuilder/addons/Chess/Scripts/Pawn.gd

82 lines
2.6 KiB
GDScript

@tool
extends Sprite2D
class_name Pawn
@export var Current_X_Position = 0
@export var Current_Y_Position = 0
# Item_Color = 0; White
# Item_Color = 1; Black
@export var Item_Color = 0
@export var Points = 1
var Temp_Color = 0
var Double_Start = true
var En_Passant = false
func _ready():
self.texture = load("res://addons/Chess/Textures/WPawn.svg")
func _process(_delta):
if Item_Color != Temp_Color:
Temp_Color = Item_Color
if Item_Color == 0:
self.texture = load("res://addons/Chess/Textures/WPawn.svg")
elif Item_Color == 1:
self.texture = load("res://addons/Chess/Textures/BPawn.svg")
# Movement interface method that all pieces will implement
# In Pawn.gd
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])
# Movement direction based on color
var direction = -1 if Item_Color == 0 else 1
# Forward movement
var forward = str(x) + "-" + str(y + direction)
if is_valid_cell(board_flow, forward) && can_move_to_cell(board_flow, forward):
moves.regular_moves.append(forward)
# Double move on first turn
var double_forward = str(x) + "-" + str(y + (direction * 2))
if Double_Start && is_valid_cell(board_flow, double_forward) && can_move_to_cell(board_flow, double_forward):
moves.regular_moves.append(double_forward)
# Diagonal captures
for dx in [-1, 1]:
var capture = str(x + dx) + "-" + str(y + direction)
if is_valid_cell(board_flow, capture) && can_move_to_cell(board_flow, capture, true):
moves.regular_moves.append(capture)
# En Passant
var adjacent = str(x + dx) + "-" + str(y)
if is_valid_cell(board_flow, adjacent) && is_valid_cell(board_flow, capture):
var adjacent_cell = board_flow.get_node(adjacent)
if adjacent_cell.get_child_count() == 1:
var adjacent_piece = adjacent_cell.get_child(0)
if adjacent_piece.name == "Pawn" && adjacent_piece.En_Passant && adjacent_piece.Item_Color != self.Item_Color:
moves.special_moves.append([adjacent, capture])
return moves
# Helper method for all pieces
func is_valid_cell(board_flow, location: String) -> bool:
var node = board_flow.get_node_or_null(location)
return node != null
# Helper for checking if cell is empty or contains enemy
func can_move_to_cell(board_flow, location: String, is_capture: bool = false) -> bool:
var node = board_flow.get_node(location)
if is_capture:
return node.get_child_count() == 1 && node.get_child(0).Item_Color != self.Item_Color
return node.get_child_count() == 0