refactored tile child relationshio

This commit is contained in:
2ManyProjects 2025-02-03 23:36:09 -06:00
parent 2980b3f8ee
commit ff873687f3
12 changed files with 307 additions and 230 deletions

View file

@ -1,100 +1,98 @@
extends Control class_name CardDisplay extends Control
class_name CardDisplay
var cardDisplays = []
var selectedCard = null
const CARD_WIDTH = 150 const CARD_WIDTH = 150
const CARD_HEIGHT = 250 const CARD_HEIGHT = 250
const CARD_MARGIN = 10 const CARD_MARGIN = 10
@onready var container: HBoxContainer
var cardDisplays = []
var selectedCard = null
var container: HBoxContainer
func _ready(): func _ready():
container = HBoxContainer.new() # Create the container first
container.name = "Hand" container = HBoxContainer.new()
container.set_position(Vector2(10, 500)) container.name = "Hand"
add_child(container) container.position = Vector2(10, 500)
add_child(container)
func update_hand(hand: Array): func update_hand(hand: Array):
clear_cards() clear_cards()
for card in hand: for card in hand:
add_card_display(card) add_card_display(card)
func add_card_display(card): func add_card_display(card):
var cardPanel = PanelContainer.new() var cardPanel = PanelContainer.new()
cardPanel.custom_minimum_size = Vector2(CARD_WIDTH, CARD_HEIGHT) cardPanel.custom_minimum_size = Vector2(CARD_WIDTH, CARD_HEIGHT)
var vbox = VBoxContainer.new() var vbox = VBoxContainer.new()
vbox.set_meta("card_id", card.id) vbox.set_meta("card_id", card.id)
cardPanel.add_child(vbox) cardPanel.add_child(vbox)
var nameLabel = Label.new() var nameLabel = Label.new()
nameLabel.text = card.cardName nameLabel.text = card.cardName
nameLabel.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER nameLabel.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
vbox.add_child(nameLabel) vbox.add_child(nameLabel)
var rankLabel = Label.new() var rankLabel = Label.new()
rankLabel.text = "Rank " + str(card.rank) rankLabel.text = "Rank " + str(card.rank)
rankLabel.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER rankLabel.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
vbox.add_child(rankLabel) vbox.add_child(rankLabel)
var descLabel = Label.new() var descLabel = Label.new()
descLabel.text = card.description descLabel.text = card.description
descLabel.autowrap_mode = TextServer.AUTOWRAP_WORD descLabel.autowrap_mode = TextServer.AUTOWRAP_WORD
descLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0) descLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0)
vbox.add_child(descLabel) vbox.add_child(descLabel)
var unitWhitelistLabel = Label.new()
unitWhitelistLabel.text = ", ".join(card.unitWhitelist) var unitWhitelistLabel = Label.new()
unitWhitelistLabel.autowrap_mode = TextServer.AUTOWRAP_WORD unitWhitelistLabel.text = ", ".join(card.unitWhitelist)
unitWhitelistLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0) unitWhitelistLabel.autowrap_mode = TextServer.AUTOWRAP_WORD
vbox.add_child(unitWhitelistLabel) unitWhitelistLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0)
vbox.add_child(unitWhitelistLabel)
var idLabel = Label.new() var idLabel = Label.new()
idLabel.text = card.id.substr(card.id.length() - 8, -1) idLabel.text = card.id.substr(card.id.length() - 8, -1)
idLabel.autowrap_mode = true idLabel.autowrap_mode = true
idLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0) idLabel.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0)
vbox.add_child(idLabel) vbox.add_child(idLabel)
cardPanel.gui_input.connect(func(event): _on_card_clicked(event, card))
cardPanel.gui_input.connect(func(event): _on_card_clicked(event, card))
cardDisplays.append(cardPanel)
cardDisplays.append(cardPanel) container.add_child(cardPanel)
container.add_child(cardPanel)
func _on_card_clicked(event: InputEvent, card: Card): func _on_card_clicked(event: InputEvent, card: Card):
if event is InputEventMouseButton and event.pressed: if event is InputEventMouseButton and event.pressed:
if selectedCard == null || selectedCard.id != card.id: if selectedCard == null || selectedCard.id != card.id:
selectedCard = card selectedCard = card
container.emit_signal("card_pressed", card) container.emit_signal("card_pressed", card)
elif selectedCard.id == card.id: elif selectedCard.id == card.id:
selectedCard = null selectedCard = null
highlight_selectedCard(selectedCard) highlight_selectedCard(selectedCard)
# selectedCard = card
# highlight_selectedCard(card)
# container.emit_signal("card_pressed", card)
func highlight_selectedCard(selected: Card) -> void: func highlight_selectedCard(selected: Card) -> void:
for display in cardDisplays: for display in cardDisplays:
var style = StyleBoxFlat.new() var style = StyleBoxFlat.new()
style.bg_color = Color(0.2, 0.2, 0.2, 1) # Default color style.bg_color = Color(0.2, 0.2, 0.2, 1) # Default color
var vbox = display.get_child(0) var vbox = display.get_child(0)
if selected && vbox.get_meta("card_id") == selected.id: if selected && vbox.get_meta("card_id") == selected.id:
style.bg_color = Color(0.4, 0.6, 0.4, 1) # Selected color style.bg_color = Color(0.4, 0.6, 0.4, 1) # Selected color
display.add_theme_stylebox_override("panel", style) display.add_theme_stylebox_override("panel", style)
func get_card_by_uuid(uuid: String) -> Card: func get_card_by_uuid(uuid: String) -> Card:
for display in cardDisplays: for display in cardDisplays:
var vbox = display.get_child(0) var vbox = display.get_child(0)
if vbox.get_meta("card_id") == uuid: if vbox.get_meta("card_id") == uuid:
return selectedCard return selectedCard
return null return null
func getSelectedCard() -> Card: func getSelectedCard() -> Card:
return selectedCard return selectedCard
func clear_cards(): func clear_cards():
for display in cardDisplays: for display in cardDisplays:
display.queue_free() display.queue_free()
cardDisplays.clear() cardDisplays.clear()
selectedCard = null selectedCard = null

View file

@ -8,7 +8,6 @@ func _init():
effectType = EffectType.SPECIAL_ACTION effectType = EffectType.SPECIAL_ACTION
description = "Force peice to careen towards the enemy side, capturing all pieces in its path" description = "Force peice to careen towards the enemy side, capturing all pieces in its path"
unitWhitelist = ["Rook", "Queen"] unitWhitelist = ["Rook", "Queen"]
func apply_effect(target_piece = null, board_flow = null, game_state = null): func apply_effect(target_piece = null, board_flow = null, game_state = null):
if !super.apply_effect(target_piece, board_flow, game_state): if !super.apply_effect(target_piece, board_flow, game_state):
return false return false
@ -16,43 +15,44 @@ func apply_effect(target_piece = null, board_flow = null, game_state = null):
if !target_piece or !board_flow or !game_state: if !target_piece or !board_flow or !game_state:
return false return false
# Get current container and position
var current_pos = target_piece.get_parent().name.split("-") var current_container = target_piece.get_parent() as PieceContainer
var current_pos = current_container.name.split("-")
var current_x = int(current_pos[0]) var current_x = int(current_pos[0])
var current_y = int(current_pos[1]) var current_y = int(current_pos[1])
# Calculate target position
var target_y = 7 if target_piece.Item_Color == 1 else 0 var target_y = 7 if target_piece.Item_Color == 1 else 0
var y_direction = 1 if target_y > current_y else -1 var y_direction = 1 if target_y > current_y else -1
# Generate tiles to check
var tiles_to_check = [] var tiles_to_check = []
var y = current_y + y_direction var y = current_y + y_direction
while y != target_y + y_direction: while y != target_y + y_direction:
tiles_to_check.append(str(current_x) + "-" + str(y)) tiles_to_check.append(str(current_x) + "-" + str(y))
y += y_direction y += y_direction
# Process pieces in path
for tile_name in tiles_to_check: for tile_name in tiles_to_check:
var tile = board_flow.get_node(tile_name) var container = board_flow.get_node(tile_name) as PieceContainer
if tile.get_child_count() > 0: if container.has_piece():
var piece_to_capture = tile.get_child(0) var piece_to_capture = container.get_piece()
game_state.updatePoints(piece_to_capture) game_state.updatePoints(piece_to_capture)
piece_to_capture.queue_free() container.remove_piece()
# Move piece to final position
var final_container = board_flow.get_node(str(current_x) + "-" + str(target_y)) as PieceContainer
var final_tile = board_flow.get_node(str(current_x) + "-" + str(target_y)) # Important: Need to remove the piece from its current container first
target_piece.reparent(final_tile) # AND keep a reference to it
target_piece.position = Vector2(25, 25) target_piece = current_container.get_piece() # Get reference before removing
current_container.remove_piece(true) # Remove from current container
final_container.set_piece(target_piece) # Set in new container
game_state.currentlyMovingPiece = target_piece game_state.currentlyMovingPiece = target_piece
burned = true burned = true
game_state.resolveMoveEffects(); game_state.resolveMoveEffects()
game_state.stateMachine.transitionToNextState(Constants.POST_MOVE) game_state.stateMachine.transitionToNextState(Constants.POST_MOVE)
return true return true

View file

@ -42,27 +42,29 @@ func apply_effect(target_piece = null, board_flow = null, game_state = null):
tiles_to_check.append(str(target_x) + "-" + str(target_y)) tiles_to_check.append(str(target_x) + "-" + str(target_y))
# Process tiles and add overlay # Process tiles and add overlay
for tile_name in tiles_to_check: for tile_name in tiles_to_check:
var tile = board_flow.get_node(tile_name) var container = board_flow.get_node(tile_name) as PieceContainer
var existing_overlay = tile.get_node_or_null("ExplosiveBootsOverlay") # Handle overlay through container's overlay management
if existing_overlay: container.remove_overlay("ExplosiveBootsOverlay")
existing_overlay.queue_free()
var overlay = ColorRect.new() var overlay = ColorRect.new()
overlay.name = "ExplosiveBootsOverlay" overlay.name = "ExplosiveBootsOverlay"
overlay.color = Color(1, 0, 0, 0.3) overlay.color = Color(1, 0, 0, 0.3)
overlay.size = tile.size overlay.size = container.size
overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
tile.add_child(overlay) container.add_overlay(overlay)
overlay.show() overlay.show()
if tile.get_child_count() > 1: # > 1 because we just added the overlay # Check for pieces to affect
var piece = tile.get_child(0) var piece = container.get_piece()
if target_piece && piece.Item_Color != target_piece.Item_Color: if piece != null and piece.Item_Color != target_piece.Item_Color:
game_state.updatePoints(piece) game_state.updatePoints(piece)
piece.queue_free() container.remove_piece()
# Setup timer to remove overlays # Setup timer to remove overlays
var timer = Timer.new() var timer = Timer.new()

View file

@ -54,25 +54,24 @@ func apply_effect(target_piece = null, board_flow = null, game_state = null):
# Process tiles and add overlay # Process tiles and add overlay
for tile_name in tiles_to_check: for tile_name in tiles_to_check:
var tile = board_flow.get_node(tile_name) var container = board_flow.get_node(tile_name) as PieceContainer
var existing_overlay = tile.get_node_or_null("SupernovaOverlay") # Handle overlay through container's overlay management
if existing_overlay: container.remove_overlay("SupernovaOverlay")
existing_overlay.queue_free()
var overlay = ColorRect.new() var overlay = ColorRect.new()
overlay.name = "SupernovaOverlay" overlay.name = "SupernovaOverlay"
overlay.color = Color(1, 0, 0, 0.3) overlay.color = Color(1, 0, 0, 0.3)
overlay.size = tile.size overlay.size = container.size
overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE
tile.add_child(overlay) container.add_overlay(overlay)
overlay.show() overlay.show()
if tile.get_child_count() > 1: # > 1 because we just added the overlay # Check for pieces to affect
var piece = tile.get_child(0) var piece = container.get_piece()
if piece.Item_Color != target_piece.Item_Color: if piece != null and piece.Item_Color != target_piece.Item_Color:
game_state.updatePoints(piece) game_state.updatePoints(piece)
piece.queue_free() container.remove_piece()
# Setup timer to remove overlays # Setup timer to remove overlays
var timer = Timer.new() var timer = Timer.new()

View file

@ -1,5 +1,6 @@
class_name ChessGame extends Control class_name ChessGame extends Control
const PieceContainer = preload("res://Systems/PieceContainer.gd")
const WHITE = "white" const WHITE = "white"
const BLACK = "black" const BLACK = "black"
signal tile_pressed(location: String) signal tile_pressed(location: String)
@ -131,7 +132,7 @@ func createBoard() -> void:
func createTile(x: int, y: int, isWhite: bool) -> void: func createTile(x: int, y: int, isWhite: bool) -> void:
# print("CreateTile x ", x, " y ", y); # print("CreateTile x ", x, " y ", y);
var tile = Button.new() var tile = PieceContainer.new()
tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize)) tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize))
tile.add_theme_stylebox_override("normal", lightStyle if isWhite else darkStyle) tile.add_theme_stylebox_override("normal", lightStyle if isWhite else darkStyle)
@ -218,8 +219,11 @@ func setupPieces() -> void:
func placePiece(position: String, pieceName: String, color: int) -> void: func placePiece(position: String, pieceName: String, color: int) -> void:
var piece = summonPiece(pieceName, color) var piece = summonPiece(pieceName, color)
boardContainer.get_node(position).add_child(piece) # boardContainer.get_node(position).add_child(piece)
var container = boardContainer.get_node(position)
container.set_piece(piece)
var coords = position.split("-") var coords = position.split("-")
board[int(coords[1])][int(coords[0])] = piece board[int(coords[1])][int(coords[0])] = piece
@ -251,8 +255,8 @@ func summonPiece(pieceName: String, color: int) -> Node:
func clearBoard() -> void: func clearBoard() -> void:
for child in boardContainer.get_children(): for child in boardContainer.get_children():
if child.get_child_count() > 0: if child is PieceContainer:
child.get_child(0).queue_free() child.remove_piece()
# func prepareHand() -> void: # func prepareHand() -> void:
@ -284,8 +288,9 @@ func evaluatePosition() -> Dictionary:
return status return status
func isValidMove(location: String) -> bool: func isValidMove(location: String) -> bool:
var node = get_node("Flow/" + location) var node = get_node("Flow/" + location) as PieceContainer
if node.get_child_count() == 0 || node.get_child(0).Item_Color != Turn: var piece = node.get_piece()
if piece == null || piece.Item_Color != Turn:
for area in areas: for area in areas:
if area == node.name: if area == node.name:
return true return true
@ -315,8 +320,8 @@ func getMovableAreas() -> void:
resetHighlights() resetHighlights()
areas.clear() areas.clear()
specialArea.clear() specialArea.clear()
var container = get_node("Flow/" + selectedNode) as PieceContainer
var piece = get_node("Flow/" + selectedNode).get_child(0) var piece = container.get_piece()
var piece_id = piece.get_instance_id() var piece_id = piece.get_instance_id()
# print("HIGHLIGHTING getMovableAreas 2") # print("HIGHLIGHTING getMovableAreas 2")
if deckManager.attached_cards.has(piece_id): if deckManager.attached_cards.has(piece_id):
@ -348,28 +353,28 @@ func highlightValidMoves() -> void:
func executeMove(targetLocation: String) -> void: func executeMove(targetLocation: String) -> void:
print("executeMove ", targetLocation) print("executeMove ", targetLocation)
var targetNode = get_node("Flow/" + targetLocation) var targetContainer = get_node("Flow/" + targetLocation) as PieceContainer
var piece = get_node("Flow/" + selectedNode).get_child(0) var sourceContainer = get_node("Flow/" + selectedNode) as PieceContainer
var piece = sourceContainer.get_piece()
var old_location = selectedNode var old_location = selectedNode
# print("piece", piece)
# tileManager.process_tile_effect(old_location, piece, false) # Exiting old tile
if targetNode.get_child_count() != 0: # Handle capture if there's a piece in target location
var capturedPiece = targetNode.get_child(0) if targetContainer.has_piece():
var capturedPiece = targetContainer.get_piece()
if Turn == 0: if Turn == 0:
p1Points += capturedPiece.Points p1Points += capturedPiece.Points
p1String.text = str(p1Points) p1String.text = str(p1Points)
else: else:
p2Points += capturedPiece.Points p2Points += capturedPiece.Points
p2String.text = str(p2Points) p2String.text = str(p2Points)
capturedPiece.free() targetContainer.remove_piece() # This handles freeing the captured piece
piece.reparent(targetNode) # Move piece to new location
piece.position = Vector2(25, 25) sourceContainer.remove_piece(true)
# print("piece2", piece) targetContainer.set_piece(piece)
hasMoved = true hasMoved = true
currentlyMovingPiece = piece currentlyMovingPiece = piece
# tileManager.process_tile_effect(targetLocation, piece, true) # Entering new tile
resetHighlights() resetHighlights()
func togglePieceChessEffect() -> void: func togglePieceChessEffect() -> void:

49
Systems/PieceContainer.gd Normal file
View file

@ -0,0 +1,49 @@
class_name PieceContainer extends Button
var piece: Pawn = null
@onready var overlays: Node = $Overlays
@onready var effects: Node = $Effects
func _init() -> void:
# Create containers for different types of children
var overlays_node = Node.new()
overlays_node.name = "Overlays"
add_child(overlays_node)
var effects_node = Node.new()
effects_node.name = "Effects"
add_child(effects_node)
func set_piece(new_piece: Pawn) -> void:
remove_piece() # Clean up any existing piece
piece = new_piece
if new_piece != null:
add_child(new_piece)
new_piece.position = Vector2(25, 25)
func get_piece() -> Pawn:
return piece
func remove_piece(keep_piece: bool = false) -> Pawn:
var old_piece = piece
if piece != null:
remove_child(piece)
if !keep_piece:
piece.queue_free()
old_piece = null
piece = null
return old_piece
func add_overlay(overlay: Node) -> void:
overlays.add_child(overlay)
func remove_overlay(overlay_name: String) -> void:
var overlay = overlays.get_node_or_null(overlay_name)
if overlay:
overlay.queue_free()
func get_overlay(overlay_name: String) -> Node:
return overlays.get_node_or_null(overlay_name)
func has_piece() -> bool:
return piece != null

View file

@ -34,12 +34,12 @@ func exit() -> void:
func handleTilePressed(location: String) -> void: func handleTilePressed(location: String) -> void:
# print("HANDLING Tile PRESSED ", location) # print("HANDLING Tile PRESSED ", location)
var targetNode = game.get_node("Flow/" + location) var container = game.get_node("Flow/" + location) as PieceContainer
if targetNode.get_child_count() == 0: var piece = container.get_piece()
if piece == null:
return return
var piece = targetNode.get_child(0)
var piece_id = piece.get_instance_id() var piece_id = piece.get_instance_id()
var selectedCard = game.cardDisplay.getSelectedCard() var selectedCard = game.cardDisplay.getSelectedCard()
if game.deckManager.attached_cards.has(piece_id): if game.deckManager.attached_cards.has(piece_id):
@ -70,5 +70,3 @@ func complete_attachment_phase() -> void:
if timer.time_left > 0: if timer.time_left > 0:
timer.stop() timer.stop()
_on_timer_timeout() _on_timer_timeout()

View file

@ -33,25 +33,39 @@ func handleMovement(location: String) -> void:
# maybe once u start nmoving a peice global stat var is set # maybe once u start nmoving a peice global stat var is set
# and any moving peice needs ot match that # and any moving peice needs ot match that
print("HANDLING MOVEMENT ", location, " | ", multiMoving, " | ", game.selectedNode) print("HANDLING MOVEMENT ", location, " | ", multiMoving, " | ", game.selectedNode)
var node = game.get_node("Flow/" + location) var node = game.get_node("Flow/" + location) as PieceContainer
# Only try to get piece if we have a selected node
if game.selectedNode == "":
# If no node is selected, we might want to select this one if it has a piece
var piece = node.get_piece()
if piece != null && piece.Item_Color == game.Turn:
game.selectedNode = location
game.getMovableAreas()
return
# Now we know we have a selected node, get its piece
var sourceContainer = game.get_node("Flow/" + game.selectedNode) as PieceContainer
var piece = sourceContainer.get_piece()
if piece == null:
return
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0)
var piece_id = piece.get_instance_id() var piece_id = piece.get_instance_id()
print(piece_id) print(piece_id)
var is_multi_moving = piece_id in moves_remaining and moves_remaining[piece_id] > 1 var is_multi_moving = piece_id in moves_remaining and moves_remaining[piece_id] > 1
if multiMoving.length() > 0: if multiMoving.length() > 0:
if node.get_child_count() > 0 and is_instance_valid(node.get_child(0)): if node.get_piece() != null and is_instance_valid(node.get_piece()):
var attempting_piece = node.get_child(0) var attempting_piece = node.get_piece()
print("Checking Str comp ", str(attempting_piece.get_instance_id()), " ", multiMoving) print("Checking Str comp ", str(attempting_piece.get_instance_id()), " ", multiMoving)
# Only block if it's a different piece of the same color # Only block if it's a different piece of the same color
if str(attempting_piece.get_instance_id()) != multiMoving and attempting_piece.Item_Color == game.Turn: if str(attempting_piece.get_instance_id()) != multiMoving and attempting_piece.Item_Color == game.Turn:
print("early return - can't select different piece of same color during multi-move") print("early return - can't select different piece of same color during multi-move")
return return
if game.selectedNode == "": if game.selectedNode == "":
if node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn: if node.get_piece() != null && node.get_piece().Item_Color == game.Turn:
print("SELECTED NODE ", location) print("SELECTED NODE ", location)
game.selectedNode = location game.selectedNode = location
game.getMovableAreas() game.getMovableAreas()
@ -95,6 +109,7 @@ func handleMovement(location: String) -> void:
else: else:
if game.isValidMove(location): if game.isValidMove(location):
# executeMove(location) # executeMove(location)
handleRegularMove(node, consumeMove) handleRegularMove(node, consumeMove)
if consumeMove: if consumeMove:
multiMoving = "" multiMoving = ""
@ -118,99 +133,113 @@ func handleMovement(location: String) -> void:
func isCastlingMove(node: Node, location: String) -> bool: func isCastlingMove(node: PieceContainer, location: String) -> bool:
return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn && node.get_child(0).name == "Rook" return game.selectedNode != "" && node.get_piece() != null && node.get_piece().Item_Color == game.Turn && node.get_piece().name == "Rook"
func isEnPassantMove(node: Node, location: String) -> bool: func isEnPassantMove(node: PieceContainer, location: String) -> bool:
return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != game.Turn && \ return game.selectedNode != "" && node.get_piece() != null && node.get_piece().Item_Color != game.Turn && \
node.get_child(0).name == "Pawn" && game.specialArea.size() != 0 && game.specialArea[0] == node.name && \ node.get_piece().name == "Pawn" && game.specialArea.size() != 0 && game.specialArea[0] == node.name && \
node.get_child(0).get("En_Passant") == true node.get_piece().get("En_Passant") == true
func isReselectMove(node: Node) -> bool: func isReselectMove(node: PieceContainer) -> bool:
return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn return game.selectedNode != "" && node.get_piece() != null && node.get_piece().Item_Color == game.Turn
func isCaptureMove(node: Node) -> bool: func isCaptureMove(node: PieceContainer) -> bool:
return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != game.Turn return game.selectedNode != "" && node.get_piece() != null && node.get_piece().Item_Color != game.Turn
func isRegularMove(node: Node) -> bool: func isRegularMove(node: PieceContainer) -> bool:
return game.selectedNode != "" && node.get_child_count() == 0 return game.selectedNode != "" && node.get_piece() != null
func handleCastling(node: Node) -> void: func handleCastling(node: PieceContainer) -> void:
print("handleCastling") print("handleCastling")
for i in game.areas: for i in game.areas:
if i == node.name: if i == node.name:
var king = game.get_node("Flow/" + game.selectedNode).get_child(0) var kingContainer = game.get_node("Flow/" + game.selectedNode) as PieceContainer
var rook = node.get_child(0) var rookContainer = node as PieceContainer
king.reparent(game.get_node("Flow/" + game.specialArea[1])) var kingTargetContainer = game.get_node("Flow/" + game.specialArea[1]) as PieceContainer
rook.reparent(game.get_node("Flow/" + game.specialArea[0])) var rookTargetContainer = game.get_node("Flow/" + game.specialArea[0]) as PieceContainer
king.position = Vector2(25, 25)
rook.position = Vector2(25, 25) var king = kingContainer.get_piece()
var rook = rookContainer.get_piece()
kingContainer.remove_piece(true)
rookContainer.remove_piece(true)
kingTargetContainer.set_piece(king)
rookTargetContainer.set_piece(rook)
game.currentlyMovingPiece = king game.currentlyMovingPiece = king
game.resolveMoveEffects() game.resolveMoveEffects()
func handleEnPassant(node: Node) -> void: func handleEnPassant(node: PieceContainer) -> void:
print("handleEnPassant") print("handleEnPassant")
for i in game.specialArea: for i in game.specialArea:
if i == node.name: if i == node.name:
var pawn = game.get_node("Flow/" + game.selectedNode).get_child(0) var targetContainer = game.get_node("Flow/" + game.selectedNode) as PieceContainer
node.get_child(0).free() var pawn = (game.get_node("Flow/" + game.selectedNode) as PieceContainer).get_piece()
pawn.reparent(game.get_node("Flow/" + game.specialArea[1])) node.remove_piece()
pawn.position = Vector2(25, 25) targetContainer.set_piece(pawn)
game.currentlyMovingPiece = pawn game.currentlyMovingPiece = pawn
game.resolveMoveEffects() game.resolveMoveEffects()
func handleCapture(node: Node) -> void: func handleCapture(node: PieceContainer) -> void:
print("handleCapture") print("handleCapture")
for i in game.areas: for i in game.areas:
if i == node.name: if i == node.name:
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0)
var capturedPiece = node.get_child(0)
piece.reparent(node)
piece.position = Vector2(25, 25)
piece.position = Vector2(25, 25)
game.currentlyMovingPiece = piece
game.resolveMoveEffects()
game.updatePoints(capturedPiece)
capturedPiece.free()
func handleRegularMove(node: Node, consume: bool) -> void: var source_container = game.get_node("Flow/" + game.selectedNode) as PieceContainer
print("handleRegularMove", node)
var moving_piece = source_container.get_piece()
var captured_piece = node.get_piece()
if moving_piece && captured_piece:
game.updatePoints(captured_piece)
node.set_piece(moving_piece)
game.currentlyMovingPiece = moving_piece
game.resolveMoveEffects()
func handleRegularMove(node: PieceContainer, consume: bool) -> void:
print("handleRegularMove", node, game.selectedNode)
for i in game.areas: for i in game.areas:
if i == node.name: if i == node.name:
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) print(i)
piece.reparent(node) var sourceContainer = game.get_node("Flow/" + game.selectedNode) as PieceContainer
piece.position = Vector2(25, 25) var piece = sourceContainer.get_piece()
print("Removing Piece 1")
sourceContainer.remove_piece(true)
node.set_piece(piece)
game.currentlyMovingPiece = piece
if consume: if consume:
game.currentlyMovingPiece = piece
game.resolveMoveEffects() game.resolveMoveEffects()
else: else:
game.currentlyMovingPiece = piece game.togglePieceChessEffect()
game.togglePieceChessEffect();
func executeMove(targetLocation: String) -> void: func executeMove(targetLocation: String) -> void:
print("executeMove ", targetLocation) print("executeMove ", targetLocation)
var targetNode = game.get_node("Flow/" + game.targetLocation) var targetContainer = game.get_node("Flow/" + targetLocation) as PieceContainer
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) var sourceContainer = game.get_node("Flow/" + game.selectedNode) as PieceContainer
# print("piece", piece) var piece = sourceContainer.get_piece()
if targetNode.get_child_count() != 0: # Handle capture if there's a piece in the target location
var capturedPiece = targetNode.get_child(0) if targetContainer.has_piece():
var capturedPiece = targetContainer.get_piece()
if game.Turn == 0: if game.Turn == 0:
game.p1Points += capturedPiece.Points game.p1Points += capturedPiece.Points
game.p1String.text = str(game.p1Points) game.p1String.text = str(game.p1Points)
else: else:
game.p2Points += capturedPiece.Points game.p2Points += capturedPiece.Points
game.p2String.text = str(game.p2Points) game.p2String.text = str(game.p2Points)
capturedPiece.free() targetContainer.remove_piece() # This will handle freeing the captured piece
piece.reparent(targetNode) # Move piece to new location
piece.position = Vector2(25, 25) sourceContainer.remove_piece(true)
# print("piece2", piece) targetContainer.set_piece(piece)
game.hasMoved = true game.hasMoved = true
game.currentlyMovingPiece = piece game.currentlyMovingPiece = piece
game.resetHighlights() game.resetHighlights()

View file

@ -61,34 +61,32 @@ func check_kingside_castle(board_flow, x: int, y: int) -> bool:
var check_pos = str(x + i) + "-" + str(y) var check_pos = str(x + i) + "-" + str(y)
if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos): if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos):
return false return false
var container = board_flow.get_node(check_pos) as PieceContainer
if container.has_piece():
return false
# Check if rook is in position and hasn't moved
var rook_pos = str(x + 3) + "-" + str(y) var rook_pos = str(x + 3) + "-" + str(y)
if !is_valid_cell(board_flow, rook_pos): if !is_valid_cell(board_flow, rook_pos):
return false return false
var rook_container = board_flow.get_node(rook_pos) as PieceContainer
var rook_node = board_flow.get_node(rook_pos) var rook = rook_container.get_piece()
if rook_node.get_child_count() != 1:
return false
var rook = rook_node.get_child(0)
return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color
func check_queenside_castle(board_flow, x: int, y: int) -> bool: func check_queenside_castle(board_flow, x: int, y: int) -> bool:
# Check if path is clear # Check if path is clear
for i in range(1, 4): for i in range(1, 4):
var check_pos = str(x - i) + "-" + str(y) var check_pos = str(x - i) + "-" + str(y)
if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos): if !is_valid_cell(board_flow, check_pos) || !can_move_to_cell(board_flow, check_pos):
return false return false
var container = board_flow.get_node(check_pos) as PieceContainer
if container.has_piece():
return false
# Check if rook is in position and hasn't moved # Check if rook is in position and hasn't moved
var rook_pos = str(x - 4) + "-" + str(y) var rook_pos = str(x - 4) + "-" + str(y)
if !is_valid_cell(board_flow, rook_pos): if !is_valid_cell(board_flow, rook_pos):
return false return false
var rook_node = board_flow.get_node(rook_pos) var rook_container = board_flow.get_node(rook_pos) as PieceContainer
if rook_node.get_child_count() != 1: var rook = rook_container.get_piece()
return false
var rook = rook_node.get_child(0)
return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color return rook.name == "Rook" && rook.Castling && rook.Item_Color == self.Item_Color

View file

@ -109,9 +109,9 @@ func getValidMoves(board_flow, current_location: String) -> Dictionary:
# En Passant # En Passant
var adjacent = str(x + dx) + "-" + str(y) var adjacent = str(x + dx) + "-" + str(y)
if is_valid_cell(board_flow, adjacent) && is_valid_cell(board_flow, capture): if is_valid_cell(board_flow, adjacent) && is_valid_cell(board_flow, capture):
var adjacent_cell = board_flow.get_node(adjacent) var adjacent_cell = board_flow.get_node(adjacent) as PieceContainer
if adjacent_cell.get_child_count() == 1: if adjacent_cell.get_piece() != null:
var adjacent_piece = adjacent_cell.get_child(0) var adjacent_piece = adjacent_cell.get_piece()
if adjacent_piece.name == "Pawn" && adjacent_piece.En_Passant && adjacent_piece.Item_Color != self.Item_Color: if adjacent_piece.name == "Pawn" && adjacent_piece.En_Passant && adjacent_piece.Item_Color != self.Item_Color:
moves.special_moves.append([adjacent, capture]) moves.special_moves.append([adjacent, capture])
@ -124,7 +124,8 @@ func is_valid_cell(board_flow, location: String) -> bool:
# Helper for checking if cell is empty or contains enemy # Helper for checking if cell is empty or contains enemy
func can_move_to_cell(board_flow, location: String, is_capture: bool = false) -> bool: func can_move_to_cell(board_flow, location: String, is_capture: bool = false) -> bool:
var node = board_flow.get_node(location) var container = board_flow.get_node(location) as PieceContainer
if is_capture: if is_capture:
return node.get_child_count() == 1 && node.get_child(0).Item_Color != self.Item_Color var piece = container.get_piece()
return node.get_child_count() == 0 return piece != null && piece.Item_Color != self.Item_Color
return !container.has_piece()

View file

@ -51,10 +51,10 @@ func getValidMoves(board_flow, current_location: String) -> Dictionary:
for direction in [-4, 3]: # Check both sides for king for direction in [-4, 3]: # Check both sides for king
var king_pos = str(x + direction) + "-" + str(y) var king_pos = str(x + direction) + "-" + str(y)
if is_valid_cell(board_flow, king_pos): if is_valid_cell(board_flow, king_pos):
var king_node = board_flow.get_node(king_pos) var king_container = board_flow.get_node(king_pos) as PieceContainer
if king_node.get_child_count() == 1: var piece = king_container.get_piece()
var piece = king_node.get_child(0) if piece != null && piece.name == "King" && piece.Castling && piece.Item_Color == self.Item_Color:
if piece.name == "King" && piece.Castling && piece.Item_Color == self.Item_Color: moves.special_moves.append([king_pos, current_location])
moves.special_moves.append([king_pos, current_location])
return moves return moves

View file

@ -76,10 +76,8 @@ layout_mode = 1
anchors_preset = 1 anchors_preset = 1
anchor_left = 1.0 anchor_left = 1.0
anchor_right = 1.0 anchor_right = 1.0
offset_left = -78.0 offset_left = -129.0
offset_top = 1.0 offset_bottom = 39.0
offset_right = 51.0
offset_bottom = 40.0
grow_horizontal = 0 grow_horizontal = 0
[node name="Hand" type="HBoxContainer" parent="."] [node name="Hand" type="HBoxContainer" parent="."]