ChessBuilder/Systems/StateMachine/GameStates/Movement.gd

218 lines
8.4 KiB
GDScript

extends "res://Systems/StateMachine/ChessGameState.gd"
var moves_remaining = {}
var multiMoving = ""
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 = ""
for piece_id in game.deckManager.attached_cards:
var card = game.deckManager.attached_cards[piece_id]
if card.effectType == Card.EffectType.MOVEMENT_MODIFIER:
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:
# we need to prevent swapping of focus between peices after the double move process has started
# maybe once u start nmoving a peice global stat var is set
# and any moving peice needs ot match that
print("HANDLING MOVEMENT ", location, " | ", multiMoving, " | ", game.selectedNode)
var node = game.get_node("Flow/" + location)
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0)
var piece_id = piece.get_instance_id()
print(piece_id)
var is_multi_moving = piece_id in moves_remaining and moves_remaining[piece_id] > 1
if multiMoving.length() > 0:
if node.get_child_count() > 0 and is_instance_valid(node.get_child(0)):
var attempting_piece = node.get_child(0)
print("Checking Str comp ", str(attempting_piece.get_instance_id()), " ", multiMoving)
# 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:
print("early return - can't select different piece of same color during multi-move")
return
if game.selectedNode == "":
if node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn:
print("SELECTED NODE ", location)
game.selectedNode = location
game.getMovableAreas()
return
# 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 = ""
game.clearSelection()
print("Consuming move")
finished.emit(Constants.POST_MOVE)
elif is_multi_moving:
game.selectedNode = location # Keep the piece selected for another move
multiMoving = str(piece_id)
# game.selectedNode = ""
game.hasMoved = false
game.currentlyMovingPiece = null
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()
else:
game.currentlyMovingPiece = piece
game.togglePieceChessEffect();
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()