ChessBuilder/addons/Chess/Scripts/Pawn.gd

130 lines
4.3 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 attached_card_label: Label
var duration_label: Label
var Temp_Color = 0
var Double_Start = true
var En_Passant = false
const BASE_SIZE = Vector2(40, 40)
const EFFECT_TINT_COLOR = Color(0.3, 0.8, 0.3, 0.5) # Green tint for card effects
var id: String = Utils.generate_guid()
func _ready():
modulate = Color.WHITE if Item_Color == 0 else Color.BLACK
func _init() -> void:
self.texture = load("res://addons/Chess/Textures/WPawn.svg")
var background_style = StyleBoxFlat.new()
background_style.bg_color = Color.WHITE
background_style.corner_radius_top_left = 2
background_style.corner_radius_top_right = 2
background_style.corner_radius_bottom_left = 2
background_style.corner_radius_bottom_right = 2
background_style.content_margin_left = 2
background_style.content_margin_right = 2
background_style.content_margin_top = 1
background_style.content_margin_bottom = 1
duration_label = Label.new()
duration_label.add_theme_stylebox_override("normal", background_style)
duration_label.add_theme_color_override("font_color", Color.BLACK)
duration_label.position = Vector2(-20, -20) # Position above the piece
add_child(duration_label)
duration_label.hide()
func update_appearance() -> void:
# print("update_appearance")
if !is_instance_valid(get_tree()) || !get_tree().get_root().has_node("Board"):
return
var chess_game = get_tree().get_root().get_node("Board")
if !chess_game.deckManager:
return
var deck_manager = chess_game.deckManager
var has_card = deck_manager.attached_cards.has(get_instance_id())
if has_card:
# Apply tint while keeping the piece color
var base_color = Color.WHITE if Item_Color == 0 else Color.BLACK
modulate = base_color * EFFECT_TINT_COLOR
# Update duration display
var card = deck_manager.attached_cards[get_instance_id()]
if is_instance_valid(duration_label):
duration_label.text = str(card.remaining_turns)
duration_label.show()
else:
# Reset to normal color
modulate = Color.WHITE if Item_Color == 0 else Color.BLACK
if is_instance_valid(duration_label):
duration_label.hide()
func on_card_effect_changed() -> void:
update_appearance()
# 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