Fixed turn transitioning

This commit is contained in:
2ManyProjects 2025-03-08 17:40:50 -06:00
parent 63ab8605d0
commit 3d86f9c42e
9 changed files with 136 additions and 39 deletions

View file

@ -32,7 +32,7 @@ func initializeStartingDeck():
deck.append(ExplosiveBootsCard.new()) deck.append(ExplosiveBootsCard.new())
deck.append(DoubleTimeCard.new()) deck.append(DoubleTimeCard.new())
# deck.append(DrunkDrivingCard.new()) # deck.append(DrunkDrivingCard.new())
# deck.append(SupernovaCard.new()) deck.append(SupernovaCard.new())
func initializeStartingBank(): func initializeStartingBank():
# sample # sample

View file

@ -29,7 +29,6 @@ var areas: PackedStringArray
var specialArea: PackedStringArray var specialArea: PackedStringArray
var gamecheckMate: bool = false var gamecheckMate: bool = false
var gamedraw: bool = false var gamedraw: bool = false
var hasMoved: bool = false
var currentlyMovingPiece = null var currentlyMovingPiece = null
var p1Points: int = 0 var p1Points: int = 0
var p2Points: int = 0 var p2Points: int = 0
@ -582,7 +581,6 @@ func executeMove(targetLocation: String) -> void:
sourceContainer.remove_piece(true) sourceContainer.remove_piece(true)
targetContainer.set_piece(piece) targetContainer.set_piece(piece)
hasMoved = true
currentlyMovingPiece = piece currentlyMovingPiece = piece
resetHighlights() resetHighlights()
if winConditionManager: if winConditionManager:
@ -608,7 +606,6 @@ func is_tile_reached(location: String, piece_name: String = "") -> bool:
# Turn = 1 if Turn == 0 else 0 # Turn = 1 if Turn == 0 else 0
# updateTurnIndicator() # updateTurnIndicator()
# resetHighlights() # resetHighlights()
# hasMoved = false
# currentlyMovingPiece = null # currentlyMovingPiece = null
# emit_signal("turn_changed") # emit_signal("turn_changed")
@ -619,15 +616,16 @@ func resolveMoveEffects() -> void:
togglePieceChessEffect() togglePieceChessEffect()
selectedNode = "" selectedNode = ""
resetHighlights() resetHighlights()
hasMoved = false
currentlyMovingPiece = null currentlyMovingPiece = null
check_captured_pieces_conditions()
emit_signal("turn_changed")
Turn = 1 if Turn == 0 else 0
updateTurnIndicator()
func endTurn() -> void: func endTurn() -> void:
print("turn_changed") print("turn_changed")
emit_signal("turn_changed")
Turn = 1 if Turn == 0 else 0
updateTurnIndicator()
if (isPlayerTurn()):
updateEffectDurations()
check_captured_pieces_conditions()
func togglePieceChessEffect() -> void: func togglePieceChessEffect() -> void:
@ -754,9 +752,9 @@ func updatePointsAndCapture(capturedPiece: Pawn, animate: bool = true) -> void:
# Player captured an enemy piece # Player captured an enemy piece
print("winConditionManager and capturedPiece.Item_Color == 1") print("winConditionManager and capturedPiece.Item_Color == 1")
if winConditionManager and capturedPiece.Item_Color == 1: # Enemy piece (black) if winConditionManager and capturedPiece.Item_Color == 1: # Enemy piece (black)
print("Player captured an enemy piece ") # winConditionManager.on_piece_captured(capturedPiece)
winConditionManager.on_piece_captured(capturedPiece) captured_pieces_this_turn.append(capturedPiece)
# captured_pieces_this_turn.append(capturedPiece.duplicate(true)) print("Player captured an enemy piece ", captured_pieces_this_turn)
else: # Enemy's turn else: # Enemy's turn
p2Points += capturedPiece.Points p2Points += capturedPiece.Points
@ -767,12 +765,12 @@ func updatePointsAndCapture(capturedPiece: Pawn, animate: bool = true) -> void:
# recover_cards_from_player_piece(capturedPiece) # recover_cards_from_player_piece(capturedPiece)
# Add to player pieces lost array for loss condition checking # Add to player pieces lost array for loss condition checking
# player_pieces_lost_this_turn.append(capturedPiece.duplicate(true))
if winConditionManager and capturedPiece.Item_Color == 0: # Player piece (white) if winConditionManager and capturedPiece.Item_Color == 0: # Player piece (white)
player_pieces_lost_this_turn.append(capturedPiece)
recover_cards_from_player_piece(capturedPiece) recover_cards_from_player_piece(capturedPiece)
print("ENemy captured an player piece ") print("ENemy captured an player piece ", player_pieces_lost_this_turn)
winConditionManager.on_piece_lost(capturedPiece) # winConditionManager.on_piece_lost(capturedPiece)
if capturedPiece.name == "King": if capturedPiece.name == "King":
@ -787,13 +785,11 @@ func check_captured_pieces_conditions():
if winConditionManager: if winConditionManager:
winConditionManager.on_piece_captured(piece) winConditionManager.on_piece_captured(piece)
print(captured_pieces_this_turn) print(captured_pieces_this_turn)
captured_pieces_this_turn = []
# Process captured player pieces (possible loss condition) # Process captured player pieces (possible loss condition)
for piece in player_pieces_lost_this_turn: for piece in player_pieces_lost_this_turn:
if winConditionManager: if winConditionManager:
winConditionManager.on_piece_lost(piece) winConditionManager.on_piece_lost(piece)
print(player_pieces_lost_this_turn) print(player_pieces_lost_this_turn)
player_pieces_lost_this_turn = []
func _on_win_condition_met(condition_data): func _on_win_condition_met(condition_data):
print("Win condition met: ", condition_data) print("Win condition met: ", condition_data)
@ -804,6 +800,9 @@ func _on_win_condition_met(condition_data):
"success": true, "success": true,
"condition": condition_data "condition": condition_data
}) })
if stateMachine:
stateMachine.transitionToNextState(Constants.CLEANUP, {"endCondition": "win_condition_met"})
# endRound() # endRound()
func _on_loss_condition_met(condition_data): func _on_loss_condition_met(condition_data):
@ -815,6 +814,8 @@ func _on_loss_condition_met(condition_data):
"success": false, "success": false,
"condition": condition_data "condition": condition_data
}) })
if stateMachine:
stateMachine.transitionToNextState(Constants.CLEANUP, {"endCondition": "win_condition_met"})
# endRound() # endRound()
func animatePieceCapture(capturedPiece: Pawn) -> void: func animatePieceCapture(capturedPiece: Pawn) -> void:

View file

@ -374,9 +374,10 @@ func generate_chess_data(node):
elif x == 1: elif x == 1:
fen += pawn_string + "/" fen += pawn_string + "/"
# elif x == height - 3: # elif x == height - 3:
# for y in unit_string.length() - 1: # fen += "u"
# fen += "*" # # for y in unit_string.length() - 1:
# fen += "1/" # # fen += "*"
# fen += "u/"
else: else:
fen += str(unit_string.length()) + "/" fen += str(unit_string.length()) + "/"

View file

@ -113,7 +113,7 @@ func _on_turn_changed():
func on_piece_captured(piece): func on_piece_captured(piece):
print("on_piece_captured ISEnemy ", piece) print("on_piece_captured ISEnemy ", piece)
var is_enemy_piece = piece.Item_Color == (1 if chess_game.Turn == 0 else 0) var is_enemy_piece = piece.Item_Color == (1 if chess_game.Turn == 1 else 0)
print("on_piece_captured ISEnemy ", is_enemy_piece) print("on_piece_captured ISEnemy ", is_enemy_piece)
if is_enemy_piece: if is_enemy_piece:
captured_pieces.append(piece) captured_pieces.append(piece)
@ -139,7 +139,7 @@ func on_piece_captured(piece):
check_conditions() check_conditions()
func on_piece_lost(piece): func on_piece_lost(piece):
var is_player_piece = piece.Item_Color == (0 if chess_game.Turn == 0 else 1) var is_player_piece = piece.Item_Color == (0 if chess_game.Turn == 1 else 1)
print("on_piece_lost isPlayer ", is_player_piece) print("on_piece_lost isPlayer ", is_player_piece)
if is_player_piece: if is_player_piece:

View file

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

View file

@ -1,8 +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) print("ENTERING STATE ", Constants.CLEANUP, data)
game.endTurn()
if "endCondition" in data: if "endCondition" in data:
finished.emit(Constants.ROUND_END) finished.emit(Constants.ROUND_END)

View file

@ -2,11 +2,108 @@ extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.EVALUATE_POSITION) print("ENTERING STATE ", Constants.EVALUATE_POSITION)
if game.isCheckmate():
game.endGame("checkmate") # Check if WinConditionManager exists
finished.emit(Constants.CLEANUP, {"endCondition": "checkmate"}) if !game.winConditionManager:
elif game.isDraw(): print("Warning: WinConditionManager not initialized")
game.endGame("draw") finished.emit(Constants.CLEANUP)
finished.emit(Constants.CLEANUP, {"endCondition": "draw"}) return
else: # Don't rely on the signal - directly check captured pieces here
finished.emit(Constants.CLEANUP) var win_condition_met = check_win_conditions()
var loss_condition_met = check_loss_conditions()
# Only proceed to CLEANUP if no win/loss conditions were met
if !win_condition_met and !loss_condition_met:
finished.emit(Constants.CLEANUP)
# Check all possible win conditions
func check_win_conditions() -> bool:
match game.winConditionManager.win_condition:
Utils.WinConditionType.CAPTURE_UNIT:
# Directly check if king/target unit was captured
var target_unit = game.winConditionManager.win_condition_data.get("unit", "King")
for piece in game.captured_pieces_this_turn:
if piece.name == target_unit and piece.Item_Color == (1 if game.Turn == 0 else 0):
# Found captured target piece - win condition met!
var condition_data = {
"type": Utils.WinConditionType.CAPTURE_UNIT,
"message": "Enemy " + target_unit + " captured!"
}
game.winConditionManager.emit_signal("win_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "target_captured"})
return true
game.captured_pieces_this_turn = []
Utils.WinConditionType.BOARD_CLEARED:
var remaining_enemy_pieces = game.winConditionManager.count_enemy_pieces()
if remaining_enemy_pieces <= 0:
var condition_data = {
"type": Utils.WinConditionType.BOARD_CLEARED,
"message": "Board cleared of enemy pieces!"
}
game.winConditionManager.emit_signal("win_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "board_cleared"})
return true
Utils.WinConditionType.TURN_NUMBER:
var target_turn = game.winConditionManager.win_condition_data.get("target_turn", 0)
if game.winConditionManager.turn_count >= target_turn:
var condition_data = {
"type": Utils.WinConditionType.TURN_NUMBER,
"message": "Turn limit reached!"
}
game.winConditionManager.emit_signal("win_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "turn_limit_reached"})
return true
Utils.WinConditionType.TILE_REACHED:
var target_tiles = game.winConditionManager.target_tiles
var required_unit = game.winConditionManager.win_condition_data.get("unit", "")
var all_targets_reached = true
for tile_loc in target_tiles:
if !game.is_tile_reached(tile_loc, required_unit):
all_targets_reached = false
break
if all_targets_reached and target_tiles.size() > 0:
var condition_data = {
"type": Utils.WinConditionType.TILE_REACHED,
"message": "All target tiles reached!"
}
game.winConditionManager.emit_signal("win_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "tile_reached"})
return true
return false
# Check all possible loss conditions
func check_loss_conditions() -> bool:
match game.winConditionManager.loss_condition:
Utils.LossConditionType.UNIT_LOST:
# Directly check if king/target unit was lost
var target_unit = game.winConditionManager.loss_condition_data.get("unit", "King")
for piece in game.player_pieces_lost_this_turn:
if piece.name == target_unit and piece.Item_Color == (0 if game.Turn == 0 else 1):
# Found lost target piece - loss condition met!
var condition_data = {
"type": Utils.LossConditionType.UNIT_LOST,
"message": "Your " + target_unit + " was captured!"
}
game.winConditionManager.emit_signal("loss_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "unit_lost"})
return true
game.player_pieces_lost_this_turn = []
Utils.WinConditionType.TURN_NUMBER:
var turn_limit = game.winConditionManager.loss_condition_data.get("turn_limit", 0)
if game.winConditionManager.turn_count >= turn_limit:
var condition_data = {
"type": Utils.WinConditionType.TURN_NUMBER,
"message": "Turn limit exceeded!"
}
game.winConditionManager.emit_signal("loss_condition_met", condition_data)
finished.emit(Constants.CLEANUP, {"endCondition": "turn_limit_exceeded"})
return true
return false

View file

@ -201,7 +201,6 @@ func handleMovement(location: String, generated: bool = false) -> void:
moves_remaining.erase(piece_id) moves_remaining.erase(piece_id)
finished.emit(Constants.POST_MOVE) finished.emit(Constants.POST_MOVE)
else: else:
game.hasMoved = false
game.currentlyMovingPiece = null game.currentlyMovingPiece = null
# game.applyTileEffects() # game.applyTileEffects()
# finished.emit(Constants.POST_MOVE) # finished.emit(Constants.POST_MOVE)
@ -291,14 +290,15 @@ func handleCapture(node: PieceContainer, generated: bool = false) -> void:
func handleCaptureMovement(node: PieceContainer) -> void: func handleCaptureMovement(node: PieceContainer) -> void:
var source_container = game.get_node("Flow/" + game.selectedNode) as PieceContainer var source_container = game.get_node("Flow/" + game.selectedNode) as PieceContainer
print("handleCaptureMovement")
var moving_piece = source_container.get_piece() var moving_piece = source_container.get_piece()
var captured_piece = node.get_piece() var captured_piece = node.get_piece()
if moving_piece && captured_piece: if moving_piece && captured_piece:
await game.animatePieceCapture(captured_piece) print("handleCapturingMovement")
game.updatePointsAndCapture(captured_piece, false) # await game.animatePieceCapture(captured_piece)
game.updatePointsAndCapture(captured_piece, true)
node.animate_movement(source_container, moving_piece); node.animate_movement(source_container, moving_piece);
var tile = game.tileManager.get_tile(node.name) var tile = game.tileManager.get_tile(node.name)
if tile: if tile:
@ -351,6 +351,5 @@ func executeMove(targetLocation: String) -> void:
sourceContainer.remove_piece(true) sourceContainer.remove_piece(true)
targetContainer.set_piece(piece) targetContainer.set_piece(piece)
game.hasMoved = true
game.currentlyMovingPiece = piece game.currentlyMovingPiece = piece
game.resetHighlights() game.resetHighlights()

View file

@ -3,8 +3,6 @@ extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.POST_MOVE) print("ENTERING STATE ", Constants.POST_MOVE)
if (game.isPlayerTurn()):
game.updateEffectDurations()
# if (game.isPlayerTurn()): # if (game.isPlayerTurn()):
# game.deckManager.updateCardDurations() # game.deckManager.updateCardDurations()
finished.emit(Constants.TILE_EFFECTS) finished.emit(Constants.TILE_EFFECTS)