Full StateMachine Implementation

This commit is contained in:
2ManyProjects 2025-01-26 20:50:55 -06:00
parent 08e27e9268
commit 4f00d01f82
17 changed files with 316 additions and 188 deletions

View file

@ -2,6 +2,7 @@ extends Control
class_name CardDisplay class_name CardDisplay
var card_displays = [] var card_displays = []
var selected_card = null
const CARD_WIDTH = 100 const CARD_WIDTH = 100
const CARD_HEIGHT = 150 const CARD_HEIGHT = 150
const CARD_MARGIN = 10 const CARD_MARGIN = 10
@ -44,7 +45,24 @@ func add_card_display(card: Card):
card_displays.append(card_panel) card_displays.append(card_panel)
container.add_child(card_panel) container.add_child(card_panel)
func _on_card_clicked(event: InputEvent, card: Card):
if event is InputEventMouseButton and event.pressed:
selected_card = card
highlight_selected_card(card)
func highlight_selected_card(selected: Card):
for display in card_displays:
var style = StyleBoxFlat.new()
style.bg_color = Color(0.2, 0.2, 0.2, 1)
if display.get_child(0).get_child(0).text == selected.card_name:
style.bg_color = Color(0.4, 0.6, 0.4, 1)
display.add_theme_stylebox_override("panel", style)
func get_selected_card() -> Card:
return selected_card
func clear_cards(): func clear_cards():
for display in card_displays: for display in card_displays:
display.queue_free() display.queue_free()
card_displays.clear() card_displays.clear()
selected_card = null

View file

@ -2,7 +2,8 @@ class_name ChessGame extends Control
const WHITE = "white" const WHITE = "white"
const BLACK = "black" const BLACK = "black"
signal tile_pressed(location: String)
signal send_location(location: String)
var currentPlayer: String = WHITE var currentPlayer: String = WHITE
var board: Array var board: Array
var activeEffects: Array var activeEffects: Array
@ -12,6 +13,8 @@ var locationX: String = ""
var locationY: String = "" var locationY: String = ""
var areas: PackedStringArray var areas: PackedStringArray
var specialArea: PackedStringArray var specialArea: PackedStringArray
var gamecheckMate: bool = false
var gamedraw: bool = false
var hasMoved: bool = false var hasMoved: bool = false
var currentlyMovingPiece = null var currentlyMovingPiece = null
var p1Points: int = 0 var p1Points: int = 0
@ -38,6 +41,8 @@ var highlightStyle = null
func _ready() -> void: func _ready() -> void:
initializeGame() initializeGame()
stateMachine.transitionToNextState(Constants.WHITE_TURN)
func _unhandled_input(event: InputEvent) -> void: func _unhandled_input(event: InputEvent) -> void:
stateMachine.unhandledInput(event) stateMachine.unhandledInput(event)
@ -50,58 +55,22 @@ func initializeGame() -> void:
initializeBoard() initializeBoard()
setupUI() setupUI()
initializeDeckSystem() initializeDeckSystem()
# func initializeBoard() -> void:
# setupStyles()
# if boardXSize < 0 || boardYSize < 0:
# return
# var numberX = 0
# var numberY = 0
# var isWhite = true
# board = []
# for i in range(8):
# var row = []
# for j in range(8):
# row.append(null)
# board.append(row)
# while numberY != boardYSize:
# boardContainer.size.y += tileYSize + 5
# boardContainer.size.x += tileXSize + 5
# while numberX != boardXSize:
# var tile = Button.new()
# tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize))
# if isWhite:
# tile.add_theme_stylebox_override("normal", lightStyle)
# else:
# tile.add_theme_stylebox_override("normal", darkStyle)
# isWhite = !isWhite
# tile.pressed.connect(func(): _onTilePressed(tile.name))
# tile.set_name(str(numberX) + "-" + str(numberY))
# boardContainer.add_child(tile)
# numberX += 1
# isWhite = !isWhite
# numberY += 1
# numberX = 0
# setupPieces()
# updateTurnIndicator()
func initializeBoard() -> void: func initializeBoard() -> void:
board = [] board = []
for i in range(8): for i in range(boardXSize):
var row = [] var row = []
for j in range(8): for j in range(boardYSize):
row.append(null) row.append(null)
board.append(row) board.append(row)
createBoard() createBoard()
setupPieces() setupPieces()
if !boardContainer.has_user_signal("tile_pressed"):
boardContainer.add_user_signal("tile_pressed")
if !boardContainer.has_user_signal("send_location"):
boardContainer.add_user_signal("send_location")
func setupUI() -> void: func setupUI() -> void:
@ -143,111 +112,22 @@ func createTile(x: int, y: int, isWhite: bool) -> void:
var tile = Button.new() var tile = Button.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)
tile.pressed.connect(func(): handleTileSelection(tile.name))
tile.set_name(str(x) + "-" + str(y)) tile.set_name(str(x) + "-" + str(y))
# tile.pressed.connect(func(): handleTileSelection(tile.name))
tile.pressed.connect(func():
# tile_pressed.emit(tile.name)
boardContainer.emit_signal("tile_pressed", tile.name)
)
boardContainer.add_child(tile) boardContainer.add_child(tile)
func clearSelection() :
func handleTileSelection(location: String) -> void: resetHighlights()
# Deselect current piece selectedNode = ""
if selectedNode == location: return
resetHighlights()
selectedNode = ""
return
var node = get_node("Flow/" + location)
parseLocation(location)
# First selection of a piece
if selectedNode == "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn:
selectedNode = location
getMovableAreas()
# Castling
elif isCastlingMove(node, location):
handleCastling(node)
# En Passant
elif isEnPassantMove(node, location):
handleEnPassant(node)
# Reselect piece
elif isReselectMove(node):
selectedNode = location
getMovableAreas()
# Capture piece
elif isCaptureMove(node):
handleCapture(node)
# Regular move
elif isRegularMove(node):
handleRegularMove(node)
func isCastlingMove(node: Node, location: String) -> bool:
return selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn && node.get_child(0).name == "Rook"
func isEnPassantMove(node: Node, location: String) -> bool:
return selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != Turn && \
node.get_child(0).name == "Pawn" && specialArea.size() != 0 && specialArea[0] == node.name && \
node.get_child(0).get("En_Passant") == true
func isReselectMove(node: Node) -> bool:
return selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color == Turn
func isCaptureMove(node: Node) -> bool:
return selectedNode != "" && node.get_child_count() != 0 && node.get_child(0).Item_Color != Turn
func isRegularMove(node: Node) -> bool:
return selectedNode != "" && node.get_child_count() == 0
func handleCastling(node: Node) -> void:
print("handleCastling")
for i in areas:
if i == node.name:
var king = get_node("Flow/" + selectedNode).get_child(0)
var rook = node.get_child(0)
king.reparent(get_node("Flow/" + specialArea[1]))
rook.reparent(get_node("Flow/" + specialArea[0]))
king.position = Vector2(25, 25)
rook.position = Vector2(25, 25)
currentlyMovingPiece = king
resolveMoveEffects()
func handleEnPassant(node: Node) -> void:
print("handleEnPassant")
for i in specialArea:
if i == node.name:
var pawn = get_node("Flow/" + selectedNode).get_child(0)
node.get_child(0).free()
pawn.reparent(get_node("Flow/" + specialArea[1]))
pawn.position = Vector2(25, 25)
currentlyMovingPiece = pawn
resolveMoveEffects()
func handleCapture(node: Node) -> void:
print("handleCapture")
for i in areas:
if i == node.name:
var piece = get_node("Flow/" + selectedNode).get_child(0)
var capturedPiece = node.get_child(0)
updatePoints(capturedPiece)
if capturedPiece.name == "King":
print("Game Over!")
capturedPiece.free()
piece.reparent(node)
piece.position = Vector2(25, 25)
piece.position = Vector2(25, 25)
currentlyMovingPiece = piece
resolveMoveEffects()
func handleRegularMove(node: Node) -> void:
print("handleRegularMove")
for i in areas:
if i == node.name:
var piece = get_node("Flow/" + selectedNode).get_child(0)
piece.reparent(node)
piece.position = Vector2(25, 25)
currentlyMovingPiece = piece
resolveMoveEffects()
func updatePoints(capturedPiece: Node) -> void: func updatePoints(capturedPiece: Node) -> void:
if Turn == 0: if Turn == 0:
@ -282,9 +162,6 @@ func updateTurnIndicator():
else: # Black's turn else: # Black's turn
turnIndicator.color = Color(0, 0, 0, 1) # Black turnIndicator.color = Color(0, 0, 0, 1) # Black
func _onTilePressed(tileName: String) -> void:
boardContainer.emit_signal("send_location", tileName)
func setupPieces() -> void: func setupPieces() -> void:
# White pieces # White pieces
placePiece("0-0", "Rook", 1) placePiece("0-0", "Rook", 1)
@ -296,7 +173,7 @@ func setupPieces() -> void:
placePiece("6-0", "Knight", 1) placePiece("6-0", "Knight", 1)
placePiece("7-0", "Rook", 1) placePiece("7-0", "Rook", 1)
for x in range(8): for x in range(boardXSize):
placePiece(str(x) + "-1", "Pawn", 1) placePiece(str(x) + "-1", "Pawn", 1)
# Black pieces # Black pieces
@ -309,7 +186,7 @@ func setupPieces() -> void:
placePiece("6-7", "Knight", 0) placePiece("6-7", "Knight", 0)
placePiece("7-7", "Rook", 0) placePiece("7-7", "Rook", 0)
for x in range(8): for x in range(boardXSize):
placePiece(str(x) + "-6", "Pawn", 0) placePiece(str(x) + "-6", "Pawn", 0)
func placePiece(position: String, pieceName: String, color: int) -> void: func placePiece(position: String, pieceName: String, color: int) -> void:
@ -345,24 +222,54 @@ func summonPiece(pieceName: String, color: int) -> Node:
piece.position = Vector2(tileXSize / 2, tileYSize / 2) piece.position = Vector2(tileXSize / 2, tileYSize / 2)
return piece return piece
func clearBoard() -> void:
for child in boardContainer.get_children():
if child.get_child_count() > 0:
child.get_child(0).queue_free()
func prepareHand() -> void: func prepareHand() -> void:
if currentHand.size() < 5:
deckManager.drawCard()
pass pass
func drawCards() -> void: func drawCards() -> void:
pass while currentHand.size() < 5:
var card = deckManager.drawCard()
if card:
currentHand.append(card)
else:
break
func updateEffectDurations() -> void: func updateEffectDurations() -> void:
pass for effect in activeEffects:
effect.duration -= 1
if effect.duration <= 0:
activeEffects.erase(effect)
func applyTileEffects() -> void: func applyTileEffects() -> void:
pass for piece in get_tree().get_nodes_in_group("Pieces"):
var tile = piece.get_parent()
if tile.has_effect():
tile.apply_effect(piece)
func attachSelectedCards() -> void: func attachSelectedCards() -> void:
pass # Logic for attaching selected cards to pieces
var selectedCard = cardDisplay.get_selected_card()
if selectedCard and selectedNode:
var piece = get_node("Flow/" + selectedNode).get_child(0)
if piece and selectedCard.can_attach_to_piece(piece):
deckManager.playCard(selectedCard, piece, boardContainer, self)
func applyCardEffects() -> void: func applyCardEffects() -> void:
pass pass
func evaluatePosition() -> Dictionary:
var status = {
"checkmate": isCheckmate(),
"draw": isDraw(),
}
return status
func isValidMove(location: String) -> bool: func isValidMove(location: String) -> bool:
var node = get_node("Flow/" + location) var node = get_node("Flow/" + location)
if node.get_child_count() == 0 || node.get_child(0).Item_Color != Turn: if node.get_child_count() == 0 || node.get_child(0).Item_Color != Turn:
@ -371,6 +278,21 @@ func isValidMove(location: String) -> bool:
return true return true
return false return false
func resetBoard() -> void:
clearBoard();
setupPieces()
Turn = 0
currentPlayer = WHITE
p1Points = 0
p1String.text = str(p1Points)
p2Points = 0
p2String.text = str(p2Points)
areas.clear()
specialArea.clear()
updateTurnIndicator()
func getMovableAreas() -> void: func getMovableAreas() -> void:
resetHighlights() resetHighlights()
areas.clear() areas.clear()
@ -378,7 +300,7 @@ func getMovableAreas() -> void:
var piece = get_node("Flow/" + selectedNode).get_child(0) var piece = get_node("Flow/" + selectedNode).get_child(0)
# print("Flow/" + selectedNode) # print("Flow/" + selectedNode)
var flowNodes = get_tree().get_nodes_in_group("Flow") # var flowNodes = get_tree().get_nodes_in_group("Flow")
# print("Flow nodes:", flowNodes) # print("Flow nodes:", flowNodes)
var moves = piece.getValidMoves(boardContainer, selectedNode) var moves = piece.getValidMoves(boardContainer, selectedNode)
areas = moves.regular_moves areas = moves.regular_moves
@ -395,10 +317,10 @@ func highlightValidMoves() -> void:
button.add_theme_stylebox_override("normal", combinedStyle) button.add_theme_stylebox_override("normal", combinedStyle)
func executeMove(targetLocation: String) -> void: func executeMove(targetLocation: String) -> void:
print("executeMove") print("executeMove ", targetLocation)
var targetNode = get_node("Flow/" + targetLocation) var targetNode = get_node("Flow/" + targetLocation)
var piece = get_node("Flow/" + selectedNode).get_child(0) var piece = get_node("Flow/" + selectedNode).get_child(0)
print("piece", piece) # print("piece", piece)
if targetNode.get_child_count() != 0: if targetNode.get_child_count() != 0:
var capturedPiece = targetNode.get_child(0) var capturedPiece = targetNode.get_child(0)
@ -412,14 +334,13 @@ func executeMove(targetLocation: String) -> void:
piece.reparent(targetNode) piece.reparent(targetNode)
piece.position = Vector2(25, 25) piece.position = Vector2(25, 25)
print("piece2", piece) # print("piece2", piece)
hasMoved = true hasMoved = true
currentlyMovingPiece = piece currentlyMovingPiece = piece
resetHighlights() resetHighlights()
func resolveMoveEffects() -> void: func resolveMoveEffects() -> void:
print("resolveMoveEffects") print("resolveMoveEffects")
print("currentlyMovingPiece", currentlyMovingPiece)
var piece = currentlyMovingPiece var piece = currentlyMovingPiece
if piece.name == "Pawn": if piece.name == "Pawn":
if piece.Double_Start: if piece.Double_Start:
@ -441,6 +362,9 @@ func resolveMoveEffects() -> void:
func resetHighlights(): func resetHighlights():
for button in boardContainer.get_children(): for button in boardContainer.get_children():
print(button.name)
if !button.name.contains("-"):
continue
var coord = button.name.split("-") var coord = button.name.split("-")
var isWhiteSquare = (int(coord[0]) + int(coord[1])) % 2 == 0 var isWhiteSquare = (int(coord[0]) + int(coord[1])) % 2 == 0
if isWhiteSquare: if isWhiteSquare:
@ -450,10 +374,10 @@ func resetHighlights():
func isCheckmate() -> bool: func isCheckmate() -> bool:
return false return gamecheckMate
func isDraw() -> bool: func isDraw() -> bool:
return false return gamedraw
func endGame(reason: String) -> void: func endGame(reason: String) -> void:
pass pass

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.APPLY_CARD_EFFECTS)
game.applyCardEffects() game.applyCardEffects()
finished.emit(Constants.MOVEMENT) finished.emit(Constants.MOVEMENT)

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.TILE_EFFECTS)
game.applyTileEffects() game.applyTileEffects()
finished.emit(Constants.PRE_MOVE) finished.emit(Constants.PRE_MOVE)

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.ATTACH_CARDS)
game.attachSelectedCards() game.attachSelectedCards()
finished.emit(Constants.APPLY_CARD_EFFECTS) finished.emit(Constants.APPLY_CARD_EFFECTS)

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.BLACK_TURN)
game.currentPlayer = game.BLACK game.currentPlayer = game.BLACK
finished.emit(Constants.HAND_SETUP) finished.emit(Constants.HAND_SETUP)

View file

@ -1,6 +1,7 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, data := {}) -> void: func enter(_previous: String, data := {}) -> void:
print("ENTERING STATE ", Constants.CLEANUP)
game.cleanupPhase() game.cleanupPhase()
if "endCondition" in data: if "endCondition" in data:

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.DRAW_PHASE)
game.drawCards() game.drawCards()
finished.emit(Constants.PERSISTENT_EFFECTS) finished.emit(Constants.PERSISTENT_EFFECTS)

View file

@ -1,6 +1,7 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.EVALUATE_POSITION)
if game.isCheckmate(): if game.isCheckmate():
game.endGame("checkmate") game.endGame("checkmate")
finished.emit(Constants.CLEANUP, {"endCondition": "checkmate"}) finished.emit(Constants.CLEANUP, {"endCondition": "checkmate"})

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.HAND_SETUP)
game.prepareHand() game.prepareHand()
finished.emit(Constants.DRAW_PHASE) finished.emit(Constants.DRAW_PHASE)

View file

@ -1,17 +1,185 @@
# extends "res://Systems/StateMachine/ChessGameState.gd"
# func enter(_previous: String, _data := {}) -> void:
# print("ENTERING STATE ", Constants.MOVEMENT)
# game.boardContainer.connect("tile_pressed", handleMovement)
# game.resetHighlights()
# if game.selectedNode != "":
# game.getValidMoves()
# func exit() -> void:
# game.boardContainer.disconnect("tile_pressed", handleMovement)
# func handleMovement(location: String) -> void:
# print("HANDLING MOVEMENT")
# 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()
# else:
# if game.isValidMove(location):
# game.executeMove(location)
# finished.emit(Constants.POST_MOVE)
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func _ready() -> void:
print("Movement state ready")
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
game.resetHighlights() print("ENTERING STATE ", Constants.MOVEMENT)
if game.selectedNode != "": if !game.boardContainer.is_connected("tile_pressed", handleMovement):
game.getValidMoves() print("Connecting tile_pressed signal")
game.boardContainer.connect("tile_pressed", handleMovement)
game.resetHighlights()
if game.selectedNode != "":
game.getValidMoves()
func exit() -> void:
if game.boardContainer.is_connected("tile_pressed", handleMovement):
game.boardContainer.disconnect("tile_pressed", handleMovement)
func handleMovement(location: String) -> void: func handleMovement(location: String) -> void:
print("HANDLING MOVEMENT ", location)
var node = game.get_node("Flow/" + location) var node = game.get_node("Flow/" + location)
if game.selectedNode == "": if game.selectedNode == "":
if node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn: if node.get_child_count() != 0 && node.get_child(0).Item_Color == game.Turn:
game.selectedNode = location game.selectedNode = location
game.getMovableAreas() 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: else:
if game.isValidMove(location): if game.isValidMove(location):
game.executeMove(location) executeMove(location)
finished.emit(Constants.POST_MOVE) 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
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
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
resolveMoveEffects()
func handleRegularMove(node: Node) -> void:
print("handleRegularMove")
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
resolveMoveEffects()
func resolveMoveEffects() -> void:
print("resolveMoveEffects")
var piece = game.currentlyMovingPiece
if piece.name == "Pawn":
if piece.Double_Start:
piece.En_Passant = true
piece.Double_Start = false
elif piece.name == "King":
piece.Castling = false
elif piece.name == "Rook":
piece.Castling = false
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()

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.POST_MOVE)
game.resolveMoveEffects() game.resolveMoveEffects()
finished.emit(Constants.EVALUATE_POSITION) finished.emit(Constants.EVALUATE_POSITION)

View file

@ -1,6 +1,8 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.PRE_MOVE)
finished.emit(Constants.ATTACH_CARDS)
pass pass
func handleInput(event: InputEvent) -> void: func handleInput(event: InputEvent) -> void:

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.PERSISTENT_EFFECTS)
game.updateEffectDurations() game.updateEffectDurations()
finished.emit(Constants.TILE_EFFECTS) finished.emit(Constants.TILE_EFFECTS)

View file

@ -1,8 +1,8 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, data := {}) -> void: func enter(_previous: String, data := {}) -> void:
# Check win condition print("ENTERING STATE ", Constants.ROUND_END)
if "endCondition" in data: if "endCondition" in data:
match data["endCondition"]: match data["endCondition"]:
"checkmate": "checkmate":
handleCheckmate() handleCheckmate()
@ -10,8 +10,9 @@ func enter(_previous: String, data := {}) -> void:
handleDraw() handleDraw()
# Reset state for next round # Reset state for next round
game.resetBoard() game.clearSelection();
finished.emit(Constants.WHITE_TURN) game.resetBoard()
finished.emit(Constants.WHITE_TURN)
func handleCheckmate() -> void: func handleCheckmate() -> void:
var winner = "White" if game.turn == 1 else "Black" var winner = "White" if game.turn == 1 else "Black"
@ -19,3 +20,6 @@ func handleCheckmate() -> void:
func handleDraw() -> void: func handleDraw() -> void:
print("Game ended in draw") print("Game ended in draw")

View file

@ -1,5 +1,6 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.WHITE_TURN)
game.currentPlayer = game.WHITE game.currentPlayer = game.WHITE
finished.emit(Constants.HAND_SETUP) finished.emit(Constants.HAND_SETUP)

View file

@ -3,36 +3,37 @@ class_name StateMachine extends Node
@export var initialState: ChessGameState = null @export var initialState: ChessGameState = null
@onready var state: ChessGameState = (func getInitialState() -> State: @onready var state: ChessGameState = (func getInitialState() -> State:
return initialState if initialState != null else get_child(0) return initialState if initialState != null else get_child(0)
).call() ).call()
@onready var parent = get_parent() @onready var parent = get_parent()
var previouseState = null var previouseState = null
func ready() -> void: func _ready() -> void:
for stateNode: ChessGameState in find_children("*", "ChessGameState"): for stateNode: ChessGameState in find_children("*", "ChessGameState"):
stateNode.finished.connect(transitionToNextState) stateNode.finished.connect(transitionToNextState)
await owner.ready await owner.ready
state.enter("") state.enter("")
func unhandledInput(event: InputEvent) -> void: func unhandledInput(event: InputEvent) -> void:
state.handleInput(event) print("StateMachine received input:", event)
state.handleInput(event)
func process(delta: float) -> void: func process(delta: float) -> void: state.update(delta)
state.update(delta)
func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void: func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void:
if not has_node(targetStatePath): print("TRANSITIONING TO: ", targetStatePath)
printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.") if not has_node(targetStatePath):
return printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.")
previouseState = state.name return
state.exit() previouseState = state.name
state = get_node(targetStatePath) state.exit()
state.enter(previouseState, data) state = get_node(targetStatePath)
state.enter(previouseState, data)