extends "res://Systems/StateMachine/ChessGameState.gd" var moves_remaining = {} var multiMoving = false func _ready() -> void: print("Movement state ready") func enter(_previous: String, _data := {}) -> void: print("ENTERING STATE ", Constants.MOVEMENT) if !game.boardContainer.is_connected("tile_pressed", handleMovement): print("Connecting tile_pressed signal") game.boardContainer.connect("tile_pressed", handleMovement) game.resetHighlights() if game.selectedNode != "": game.getMovableAreas() moves_remaining.clear() multiMoving = false for piece_id in game.deckManager.attached_cards: var card = game.deckManager.attached_cards[piece_id] if card is DoubleTimeCard: var effects = card.modify_moves() moves_remaining[piece_id] = effects.get("extra_moves", 0) + 1 func exit() -> void: if game.boardContainer.is_connected("tile_pressed", handleMovement): game.boardContainer.disconnect("tile_pressed", handleMovement) func handleMovement(location: String) -> void: # print("HANDLING MOVEMENT ", location) var node = game.get_node("Flow/" + location) if game.selectedNode == "": if node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn: game.selectedNode = location game.getMovableAreas() return var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) var piece_id = piece.get_instance_id() var is_multi_moving = piece_id in moves_remaining and moves_remaining[piece_id] > 1 # print(moves_remaining, " | ", piece_id, " | ",is_multi_moving) var consumeMove = true; if is_multi_moving: moves_remaining[piece_id] -= 1 consumeMove = false; else: # No more moves remaining, end turn if piece_id in moves_remaining: moves_remaining.erase(piece_id) # finished.emit(Constants.POST_MOVE) if game.selectedNode == location: if !is_multi_moving and !multiMoving: print("CLEAR SELECTION*************") game.clearSelection() elif isCastlingMove(node, location): handleCastling(node) finished.emit(Constants.POST_MOVE) # En Passant elif isEnPassantMove(node, location): handleEnPassant(node) # Reselect piece elif isReselectMove(node): if !is_multi_moving and !multiMoving: print("RESELECT SELECTION*************") game.selectedNode = location game.getMovableAreas() # Capture piece elif isCaptureMove(node): handleCapture(node) finished.emit(Constants.POST_MOVE) # Regular move # elif isRegularMove(node): # handleRegularMove(node) # finished.emit(Constants.POST_MOVE) else: if game.isValidMove(location): # executeMove(location) handleRegularMove(node, consumeMove) if consumeMove: multiMoving = false game.clearSelection() finished.emit(Constants.POST_MOVE) else: multiMoving = true game.selectedNode = location # Keep the piece selected for another move # game.selectedNode = "" game.hasMoved = false game.currentlyMovingPiece = null game.getMovableAreas() print("ANOTHER MOVE") game.resetHighlights() if game.selectedNode != "": game.getMovableAreas() # finished.emit(Constants.POST_MOVE) func isCastlingMove(node: Node, 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" func isEnPassantMove(node: Node, location: String) -> bool: return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != game.Turn && \ node.get_child(0).name == "Pawn" && game.specialArea.size() != 0 && game.specialArea[0] == node.name && \ node.get_child(0).get("En_Passant") == true func isReselectMove(node: Node) -> bool: return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn func isCaptureMove(node: Node) -> bool: return game.selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != game.Turn func isRegularMove(node: Node) -> bool: return game.selectedNode != "" && node.get_child_count() == 0 func handleCastling(node: Node) -> void: print("handleCastling") for i in game.areas: if i == node.name: var king = game.get_node("Flow/" + game.selectedNode).get_child(0) var rook = node.get_child(0) king.reparent(game.get_node("Flow/" + game.specialArea[1])) rook.reparent(game.get_node("Flow/" + game.specialArea[0])) king.position = Vector2(25, 25) rook.position = Vector2(25, 25) game.currentlyMovingPiece = king game.resolveMoveEffects() func handleEnPassant(node: Node) -> void: print("handleEnPassant") for i in game.specialArea: if i == node.name: var pawn = game.get_node("Flow/" + game.selectedNode).get_child(0) node.get_child(0).free() pawn.reparent(game.get_node("Flow/" + game.specialArea[1])) pawn.position = Vector2(25, 25) game.currentlyMovingPiece = pawn game.resolveMoveEffects() func handleCapture(node: Node) -> void: print("handleCapture") for i in game.areas: if i == node.name: var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) var capturedPiece = node.get_child(0) game.updatePoints(capturedPiece) if capturedPiece.name == "King": print("Game Over!") game.gamecheckMate = true capturedPiece.free() piece.reparent(node) piece.position = Vector2(25, 25) piece.position = Vector2(25, 25) game.currentlyMovingPiece = piece game.resolveMoveEffects() func handleRegularMove(node: Node, consume: bool) -> void: print("handleRegularMove", node) for i in game.areas: if i == node.name: var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) piece.reparent(node) piece.position = Vector2(25, 25) if consume: game.currentlyMovingPiece = piece game.resolveMoveEffects() func executeMove(targetLocation: String) -> void: print("executeMove ", targetLocation) var targetNode = game.get_node("Flow/" + game.targetLocation) var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) # print("piece", piece) if targetNode.get_child_count() != 0: var capturedPiece = targetNode.get_child(0) if game.Turn == 0: game.p1Points += capturedPiece.Points game.p1String.text = str(game.p1Points) else: game.p2Points += capturedPiece.Points game.p2String.text = str(game.p2Points) capturedPiece.free() piece.reparent(targetNode) piece.position = Vector2(25, 25) # print("piece2", piece) game.hasMoved = true game.currentlyMovingPiece = piece game.resetHighlights()