fixed crash on invalid move

This commit is contained in:
2ManyProjects 2025-01-28 02:20:02 -06:00
parent b5b2b977ad
commit 54d6e33c8c
8 changed files with 68 additions and 78 deletions

View file

@ -226,18 +226,12 @@ 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:
while currentHand.size() < 5:
var card = deckManager.drawCard()
if card:
currentHand.append(card)
else:
break
# func prepareHand() -> void:
# if deckManager.hand.size() < 5:
# deckManager.drawCard()
# pass
func updateEffectDurations() -> void:
for effect in activeEffects:
@ -303,8 +297,11 @@ func getMovableAreas() -> void:
var piece = get_node("Flow/" + selectedNode).get_child(0)
# print("Flow/" + selectedNode)
# print("PEICE ", piece)
# var flowNodes = get_tree().get_nodes_in_group("Flow")
# print("Flow nodes:", flowNodes)
# if !piece:
# return
var moves = piece.getValidMoves(boardContainer, selectedNode)
areas = moves.regular_moves
specialArea = moves.special_moves
@ -343,7 +340,7 @@ func executeMove(targetLocation: String) -> void:
resetHighlights()
func resolveMoveEffects() -> void:
print("resolveMoveEffects")
print("resolveMoveEffects", currentlyMovingPiece)
var piece = currentlyMovingPiece
if piece.name == "Pawn":
if piece.Double_Start:

View file

@ -3,9 +3,10 @@ class_name ChessGameState extends Node
signal finished(nextStatePath: String, data: Dictionary)
const ChessGame = preload("res://Systems/Game/ChessGame.gd")
@export var game: ChessGame
@onready var deckManager: DeckManager
func _ready() -> void:
assert(game != null, "ChessGameState must have a ChessGame reference")
deckManager = game.deckManager
func enter(_previous: String, _data := {}) -> void:
pass

View file

@ -1,6 +1,21 @@
extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void:
# finished.emit(Constants.APPLY_CARD_EFFECTS)
print("ENTERING STATE ", Constants.ATTACH_CARDS)
game.attachSelectedCards()
finished.emit(Constants.APPLY_CARD_EFFECTS)
if !game.boardContainer.is_connected("card_pressed", handleCardPressed):
print("Connecting card_pressed signal")
game.boardContainer.connect("card_pressed", handleCardPressed)
func exit() -> void:
if game.boardContainer.is_connected("card_pressed", handleCardPressed):
game.boardContainer.disconnect("card_pressed", handleCardPressed)
func handleCardPressed(location: String) -> void:
print("HANDLING MOVEMENT ", location)
var node = game.get_node("Flow/" + location)

View file

@ -2,5 +2,7 @@ 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)
# deckManager.drawCards()
finished.emit(Constants.PERSISTENT_EFFECTS)

View file

@ -2,5 +2,15 @@ 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)
# prepareHand()
finished.emit(Constants.DRAW_PHASE)
# func prepareHand() -> void:
# if deckManager.hand.size() < 5:
# deckManager.drawCard()
# pass
# func drawCards() -> void:
# while deckManager.hand.size() < 5:
# deckManager.drawCard()

View file

@ -1,26 +1,3 @@
# 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"
@ -34,7 +11,7 @@ func enter(_previous: String, _data := {}) -> void:
game.boardContainer.connect("tile_pressed", handleMovement)
game.resetHighlights()
if game.selectedNode != "":
game.getValidMoves()
game.getMovableAreas()
func exit() -> void:
if game.boardContainer.is_connected("tile_pressed", handleMovement):
@ -103,7 +80,7 @@ func handleCastling(node: Node) -> void:
king.position = Vector2(25, 25)
rook.position = Vector2(25, 25)
game.currentlyMovingPiece = king
resolveMoveEffects()
game.resolveMoveEffects()
func handleEnPassant(node: Node) -> void:
print("handleEnPassant")
@ -114,7 +91,7 @@ func handleEnPassant(node: Node) -> void:
pawn.reparent(game.get_node("Flow/" + game.specialArea[1]))
pawn.position = Vector2(25, 25)
game.currentlyMovingPiece = pawn
resolveMoveEffects()
game.resolveMoveEffects()
func handleCapture(node: Node) -> void:
print("handleCapture")
@ -133,34 +110,21 @@ func handleCapture(node: Node) -> void:
piece.position = Vector2(25, 25)
piece.position = Vector2(25, 25)
game.currentlyMovingPiece = piece
resolveMoveEffects()
game.resolveMoveEffects()
func handleRegularMove(node: Node) -> void:
print("handleRegularMove")
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
resolveMoveEffects()
game.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)

View file

@ -2,5 +2,5 @@ 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)
# game.resolveMoveEffects()
finished.emit(Constants.EVALUATE_POSITION)

View file

@ -3,7 +3,7 @@ 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()
@ -11,16 +11,17 @@ var previouseState = null
func _ready() -> void:
for stateNode: ChessGameState in find_children("*", "ChessGameState"):
stateNode.finished.connect(transitionToNextState)
for stateNode: ChessGameState in find_children("*", "ChessGameState"):
stateNode.game = owner
stateNode.finished.connect(transitionToNextState)
await owner.ready
state.enter("")
await owner.ready
state.enter("")
func unhandledInput(event: InputEvent) -> void:
print("StateMachine received input:", event)
state.handleInput(event)
print("StateMachine received input:", event)
state.handleInput(event)
func process(delta: float) -> void: state.update(delta)
@ -29,11 +30,11 @@ func process(delta: float) -> void: state.update(delta)
func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void:
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)
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)