Full StateMachine Implementation
This commit is contained in:
parent
08e27e9268
commit
4f00d01f82
17 changed files with 316 additions and 188 deletions
|
|
@ -2,6 +2,7 @@ extends Control
|
|||
class_name CardDisplay
|
||||
|
||||
var card_displays = []
|
||||
var selected_card = null
|
||||
const CARD_WIDTH = 100
|
||||
const CARD_HEIGHT = 150
|
||||
const CARD_MARGIN = 10
|
||||
|
|
@ -44,7 +45,24 @@ func add_card_display(card: Card):
|
|||
card_displays.append(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():
|
||||
for display in card_displays:
|
||||
display.queue_free()
|
||||
card_displays.clear()
|
||||
selected_card = null
|
||||
|
|
@ -2,7 +2,8 @@ class_name ChessGame extends Control
|
|||
|
||||
const WHITE = "white"
|
||||
const BLACK = "black"
|
||||
|
||||
signal tile_pressed(location: String)
|
||||
signal send_location(location: String)
|
||||
var currentPlayer: String = WHITE
|
||||
var board: Array
|
||||
var activeEffects: Array
|
||||
|
|
@ -12,6 +13,8 @@ var locationX: String = ""
|
|||
var locationY: String = ""
|
||||
var areas: PackedStringArray
|
||||
var specialArea: PackedStringArray
|
||||
var gamecheckMate: bool = false
|
||||
var gamedraw: bool = false
|
||||
var hasMoved: bool = false
|
||||
var currentlyMovingPiece = null
|
||||
var p1Points: int = 0
|
||||
|
|
@ -38,6 +41,8 @@ var highlightStyle = null
|
|||
|
||||
func _ready() -> void:
|
||||
initializeGame()
|
||||
stateMachine.transitionToNextState(Constants.WHITE_TURN)
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
stateMachine.unhandledInput(event)
|
||||
|
|
@ -50,58 +55,22 @@ func initializeGame() -> void:
|
|||
initializeBoard()
|
||||
setupUI()
|
||||
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:
|
||||
board = []
|
||||
for i in range(8):
|
||||
for i in range(boardXSize):
|
||||
var row = []
|
||||
for j in range(8):
|
||||
for j in range(boardYSize):
|
||||
row.append(null)
|
||||
board.append(row)
|
||||
createBoard()
|
||||
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:
|
||||
|
|
@ -143,111 +112,22 @@ func createTile(x: int, y: int, isWhite: bool) -> void:
|
|||
var tile = Button.new()
|
||||
tile.set_custom_minimum_size(Vector2(tileXSize, tileYSize))
|
||||
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.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)
|
||||
|
||||
|
||||
func handleTileSelection(location: String) -> void:
|
||||
# Deselect current piece
|
||||
if selectedNode == location:
|
||||
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 clearSelection() :
|
||||
resetHighlights()
|
||||
selectedNode = ""
|
||||
return
|
||||
|
||||
|
||||
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:
|
||||
if Turn == 0:
|
||||
|
|
@ -282,9 +162,6 @@ func updateTurnIndicator():
|
|||
else: # Black's turn
|
||||
turnIndicator.color = Color(0, 0, 0, 1) # Black
|
||||
|
||||
func _onTilePressed(tileName: String) -> void:
|
||||
boardContainer.emit_signal("send_location", tileName)
|
||||
|
||||
func setupPieces() -> void:
|
||||
# White pieces
|
||||
placePiece("0-0", "Rook", 1)
|
||||
|
|
@ -296,7 +173,7 @@ func setupPieces() -> void:
|
|||
placePiece("6-0", "Knight", 1)
|
||||
placePiece("7-0", "Rook", 1)
|
||||
|
||||
for x in range(8):
|
||||
for x in range(boardXSize):
|
||||
placePiece(str(x) + "-1", "Pawn", 1)
|
||||
|
||||
# Black pieces
|
||||
|
|
@ -309,7 +186,7 @@ func setupPieces() -> void:
|
|||
placePiece("6-7", "Knight", 0)
|
||||
placePiece("7-7", "Rook", 0)
|
||||
|
||||
for x in range(8):
|
||||
for x in range(boardXSize):
|
||||
placePiece(str(x) + "-6", "Pawn", 0)
|
||||
|
||||
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)
|
||||
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:
|
||||
if currentHand.size() < 5:
|
||||
deckManager.drawCard()
|
||||
pass
|
||||
|
||||
func drawCards() -> void:
|
||||
pass
|
||||
while currentHand.size() < 5:
|
||||
var card = deckManager.drawCard()
|
||||
if card:
|
||||
currentHand.append(card)
|
||||
else:
|
||||
break
|
||||
|
||||
func updateEffectDurations() -> void:
|
||||
pass
|
||||
|
||||
for effect in activeEffects:
|
||||
effect.duration -= 1
|
||||
if effect.duration <= 0:
|
||||
activeEffects.erase(effect)
|
||||
|
||||
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:
|
||||
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:
|
||||
pass
|
||||
|
||||
func evaluatePosition() -> Dictionary:
|
||||
var status = {
|
||||
"checkmate": isCheckmate(),
|
||||
"draw": isDraw(),
|
||||
}
|
||||
return status
|
||||
|
||||
func isValidMove(location: String) -> bool:
|
||||
var node = get_node("Flow/" + location)
|
||||
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 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:
|
||||
resetHighlights()
|
||||
areas.clear()
|
||||
|
|
@ -378,7 +300,7 @@ func getMovableAreas() -> void:
|
|||
|
||||
var piece = get_node("Flow/" + selectedNode).get_child(0)
|
||||
# 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)
|
||||
var moves = piece.getValidMoves(boardContainer, selectedNode)
|
||||
areas = moves.regular_moves
|
||||
|
|
@ -395,10 +317,10 @@ func highlightValidMoves() -> void:
|
|||
button.add_theme_stylebox_override("normal", combinedStyle)
|
||||
|
||||
func executeMove(targetLocation: String) -> void:
|
||||
print("executeMove")
|
||||
print("executeMove ", targetLocation)
|
||||
var targetNode = get_node("Flow/" + targetLocation)
|
||||
var piece = get_node("Flow/" + selectedNode).get_child(0)
|
||||
print("piece", piece)
|
||||
# print("piece", piece)
|
||||
|
||||
if targetNode.get_child_count() != 0:
|
||||
var capturedPiece = targetNode.get_child(0)
|
||||
|
|
@ -412,14 +334,13 @@ func executeMove(targetLocation: String) -> void:
|
|||
|
||||
piece.reparent(targetNode)
|
||||
piece.position = Vector2(25, 25)
|
||||
print("piece2", piece)
|
||||
# print("piece2", piece)
|
||||
hasMoved = true
|
||||
currentlyMovingPiece = piece
|
||||
resetHighlights()
|
||||
|
||||
func resolveMoveEffects() -> void:
|
||||
print("resolveMoveEffects")
|
||||
print("currentlyMovingPiece", currentlyMovingPiece)
|
||||
var piece = currentlyMovingPiece
|
||||
if piece.name == "Pawn":
|
||||
if piece.Double_Start:
|
||||
|
|
@ -441,6 +362,9 @@ func resolveMoveEffects() -> void:
|
|||
|
||||
func resetHighlights():
|
||||
for button in boardContainer.get_children():
|
||||
print(button.name)
|
||||
if !button.name.contains("-"):
|
||||
continue
|
||||
var coord = button.name.split("-")
|
||||
var isWhiteSquare = (int(coord[0]) + int(coord[1])) % 2 == 0
|
||||
if isWhiteSquare:
|
||||
|
|
@ -450,10 +374,10 @@ func resetHighlights():
|
|||
|
||||
|
||||
func isCheckmate() -> bool:
|
||||
return false
|
||||
return gamecheckMate
|
||||
|
||||
func isDraw() -> bool:
|
||||
return false
|
||||
return gamedraw
|
||||
|
||||
func endGame(reason: String) -> void:
|
||||
pass
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.APPLY_CARD_EFFECTS)
|
||||
game.applyCardEffects()
|
||||
finished.emit(Constants.MOVEMENT)
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.TILE_EFFECTS)
|
||||
game.applyTileEffects()
|
||||
finished.emit(Constants.PRE_MOVE)
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.ATTACH_CARDS)
|
||||
game.attachSelectedCards()
|
||||
finished.emit(Constants.APPLY_CARD_EFFECTS)
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.BLACK_TURN)
|
||||
game.currentPlayer = game.BLACK
|
||||
finished.emit(Constants.HAND_SETUP)
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.CLEANUP)
|
||||
game.cleanupPhase()
|
||||
|
||||
if "endCondition" in data:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.DRAW_PHASE)
|
||||
game.drawCards()
|
||||
finished.emit(Constants.PERSISTENT_EFFECTS)
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.EVALUATE_POSITION)
|
||||
if game.isCheckmate():
|
||||
game.endGame("checkmate")
|
||||
finished.emit(Constants.CLEANUP, {"endCondition": "checkmate"})
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.HAND_SETUP)
|
||||
game.prepareHand()
|
||||
finished.emit(Constants.DRAW_PHASE)
|
||||
|
|
@ -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"
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
print("Movement state ready")
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
game.resetHighlights()
|
||||
if game.selectedNode != "":
|
||||
game.getValidMoves()
|
||||
|
||||
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.getValidMoves()
|
||||
|
||||
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):
|
||||
game.executeMove(location)
|
||||
finished.emit(Constants.POST_MOVE)
|
||||
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
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.POST_MOVE)
|
||||
game.resolveMoveEffects()
|
||||
finished.emit(Constants.EVALUATE_POSITION)
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.PRE_MOVE)
|
||||
finished.emit(Constants.ATTACH_CARDS)
|
||||
pass
|
||||
|
||||
func handleInput(event: InputEvent) -> void:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.PERSISTENT_EFFECTS)
|
||||
game.updateEffectDurations()
|
||||
finished.emit(Constants.TILE_EFFECTS)
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, data := {}) -> void:
|
||||
# Check win condition
|
||||
if "endCondition" in data:
|
||||
print("ENTERING STATE ", Constants.ROUND_END)
|
||||
if "endCondition" in data:
|
||||
match data["endCondition"]:
|
||||
"checkmate":
|
||||
handleCheckmate()
|
||||
|
|
@ -10,12 +10,16 @@ func enter(_previous: String, data := {}) -> void:
|
|||
handleDraw()
|
||||
|
||||
# Reset state for next round
|
||||
game.resetBoard()
|
||||
finished.emit(Constants.WHITE_TURN)
|
||||
game.clearSelection();
|
||||
game.resetBoard()
|
||||
finished.emit(Constants.WHITE_TURN)
|
||||
|
||||
func handleCheckmate() -> void:
|
||||
var winner = "White" if game.turn == 1 else "Black"
|
||||
print("Checkmate! " + winner + " wins!")
|
||||
|
||||
func handleDraw() -> void:
|
||||
print("Game ended in draw")
|
||||
print("Game ended in draw")
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
extends "res://Systems/StateMachine/ChessGameState.gd"
|
||||
|
||||
func enter(_previous: String, _data := {}) -> void:
|
||||
print("ENTERING STATE ", Constants.WHITE_TURN)
|
||||
game.currentPlayer = game.WHITE
|
||||
finished.emit(Constants.HAND_SETUP)
|
||||
|
|
|
|||
|
|
@ -3,36 +3,37 @@ class_name StateMachine extends Node
|
|||
@export var initialState: ChessGameState = null
|
||||
|
||||
@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()
|
||||
|
||||
@onready var parent = get_parent()
|
||||
var previouseState = null
|
||||
|
||||
|
||||
func ready() -> void:
|
||||
for stateNode: ChessGameState in find_children("*", "ChessGameState"):
|
||||
stateNode.finished.connect(transitionToNextState)
|
||||
func _ready() -> void:
|
||||
for stateNode: ChessGameState in find_children("*", "ChessGameState"):
|
||||
stateNode.finished.connect(transitionToNextState)
|
||||
|
||||
await owner.ready
|
||||
state.enter("")
|
||||
await owner.ready
|
||||
state.enter("")
|
||||
|
||||
|
||||
func unhandledInput(event: InputEvent) -> void:
|
||||
state.handleInput(event)
|
||||
print("StateMachine received input:", event)
|
||||
state.handleInput(event)
|
||||
|
||||
|
||||
func process(delta: float) -> void:
|
||||
state.update(delta)
|
||||
|
||||
func process(delta: float) -> void: state.update(delta)
|
||||
|
||||
|
||||
|
||||
|
||||
func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void:
|
||||
if not has_node(targetStatePath):
|
||||
printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.")
|
||||
return
|
||||
previouseState = state.name
|
||||
state.exit()
|
||||
state = get_node(targetStatePath)
|
||||
state.enter(previouseState, data)
|
||||
print("TRANSITIONING TO: ", targetStatePath)
|
||||
if not has_node(targetStatePath):
|
||||
printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.")
|
||||
return
|
||||
previouseState = state.name
|
||||
state.exit()
|
||||
state = get_node(targetStatePath)
|
||||
state.enter(previouseState, data)
|
||||
|
|
|
|||
Loading…
Reference in a new issue