ChessBuilder/Systems/StateMachine/GameStates/Movement.gd

149 lines
5.5 KiB
GDScript

extends "res://Systems/StateMachine/ChessGameState.gd"
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()
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()
elif game.selectedNode == location:
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):
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)
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) -> 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)
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()