172 lines
No EOL
5.7 KiB
GDScript
172 lines
No EOL
5.7 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 game: ChessGame = null
|
|
var duration_label: Label
|
|
var Temp_Color = 0
|
|
var Double_Start = true
|
|
var En_Passant = false
|
|
const BASE_SIZE = Vector2(40, 40)
|
|
const CARD_TINT_COLOR = Color(0.3, 0.8, 0.3, 0.5) # Green tint for card effects
|
|
const EFFECT_TINT_COLOR = Color(0.8, 0.2, 0.2, 0.5)
|
|
var id: String = Utils.generate_guid()
|
|
|
|
func _ready():
|
|
modulate = Color.WHITE if Item_Color == 0 else Color.BLACK
|
|
|
|
func _init(chess: ChessGame) -> void:
|
|
game = chess
|
|
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")
|
|
var chess_game = game
|
|
if !is_instance_valid(get_tree()) || !chess_game:
|
|
return
|
|
|
|
if !chess_game.deckManager:
|
|
return
|
|
|
|
var deck_manager = chess_game.deckManager
|
|
var has_card = deck_manager.attached_cards.has(get_instance_id())
|
|
var has_effect = deck_manager.attached_effects.has(get_instance_id())
|
|
var base_color = Color.WHITE if Item_Color == 0 else Color.BLACK
|
|
|
|
if has_effect and deck_manager.attached_effects[get_instance_id()].size() > 0:
|
|
modulate = base_color * ( EFFECT_TINT_COLOR)
|
|
if has_card:
|
|
# Apply tint while keeping the piece color
|
|
modulate = base_color * CARD_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) as PieceContainer
|
|
if adjacent_cell.get_piece() != null:
|
|
var adjacent_piece = adjacent_cell.get_piece()
|
|
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 container = board_flow.get_node(location) as PieceContainer
|
|
var game = board_flow.get_parent() as ChessGame
|
|
var tile = game.tileManager.get_tile(location)
|
|
if tile && !tile.passable:
|
|
return false
|
|
if is_capture:
|
|
var piece = container.get_piece()
|
|
return piece != null && piece.Item_Color != self.Item_Color
|
|
return !container.has_piece()
|
|
|
|
func animate_movement(target_position: Vector2, duration: float = 0.5) -> void:
|
|
# print("--------------STARTING ANIM--------------", position, " ", target_position)
|
|
z_index = 1
|
|
var tween = create_tween()
|
|
# Make sure the tween is configured properly
|
|
tween.set_trans(Tween.TRANS_LINEAR) # or TRANS_CUBIC for smoother movement
|
|
tween.set_ease(Tween.EASE_IN_OUT)
|
|
|
|
var start_pos = position
|
|
tween.tween_property(self, "global_position", target_position, duration)
|
|
#.from(start_pos)
|
|
|
|
# Wait for animation to complete
|
|
await tween.finished
|
|
# print("--------------FINISHED ANIM--------------")
|
|
|
|
func animate_capture(duration: float = 0.5) -> void:
|
|
z_index = 1 # Ensure piece is visible above others during animation
|
|
var tween = create_tween()
|
|
tween.set_trans(Tween.TRANS_LINEAR)
|
|
tween.set_ease(Tween.EASE_IN_OUT)
|
|
|
|
# First turn red
|
|
tween.tween_property(self, "modulate", Color.RED, duration/2)
|
|
# Then shrink to nothing
|
|
tween.tween_property(self, "scale", Vector2.ZERO, duration/2)
|
|
# Finally remove the piece
|
|
tween.tween_callback(queue_free)
|
|
|
|
await tween.finished |