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

View file

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

View file

@ -1,6 +1,21 @@
extends "res://Systems/StateMachine/ChessGameState.gd" extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
# finished.emit(Constants.APPLY_CARD_EFFECTS)
print("ENTERING STATE ", Constants.ATTACH_CARDS) print("ENTERING STATE ", Constants.ATTACH_CARDS)
game.attachSelectedCards() if !game.boardContainer.is_connected("card_pressed", handleCardPressed):
finished.emit(Constants.APPLY_CARD_EFFECTS) 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: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.DRAW_PHASE) print("ENTERING STATE ", Constants.DRAW_PHASE)
game.drawCards() # deckManager.drawCards()
finished.emit(Constants.PERSISTENT_EFFECTS) finished.emit(Constants.PERSISTENT_EFFECTS)

View file

@ -2,5 +2,15 @@ extends "res://Systems/StateMachine/ChessGameState.gd"
func enter(_previous: String, _data := {}) -> void: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.HAND_SETUP) print("ENTERING STATE ", Constants.HAND_SETUP)
game.prepareHand() # prepareHand()
finished.emit(Constants.DRAW_PHASE) 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" extends "res://Systems/StateMachine/ChessGameState.gd"
@ -34,7 +11,7 @@ func enter(_previous: String, _data := {}) -> void:
game.boardContainer.connect("tile_pressed", handleMovement) game.boardContainer.connect("tile_pressed", handleMovement)
game.resetHighlights() game.resetHighlights()
if game.selectedNode != "": if game.selectedNode != "":
game.getValidMoves() game.getMovableAreas()
func exit() -> void: func exit() -> void:
if game.boardContainer.is_connected("tile_pressed", handleMovement): if game.boardContainer.is_connected("tile_pressed", handleMovement):
@ -103,7 +80,7 @@ func handleCastling(node: Node) -> void:
king.position = Vector2(25, 25) king.position = Vector2(25, 25)
rook.position = Vector2(25, 25) rook.position = Vector2(25, 25)
game.currentlyMovingPiece = king game.currentlyMovingPiece = king
resolveMoveEffects() game.resolveMoveEffects()
func handleEnPassant(node: Node) -> void: func handleEnPassant(node: Node) -> void:
print("handleEnPassant") print("handleEnPassant")
@ -114,7 +91,7 @@ func handleEnPassant(node: Node) -> void:
pawn.reparent(game.get_node("Flow/" + game.specialArea[1])) pawn.reparent(game.get_node("Flow/" + game.specialArea[1]))
pawn.position = Vector2(25, 25) pawn.position = Vector2(25, 25)
game.currentlyMovingPiece = pawn game.currentlyMovingPiece = pawn
resolveMoveEffects() game.resolveMoveEffects()
func handleCapture(node: Node) -> void: func handleCapture(node: Node) -> void:
print("handleCapture") print("handleCapture")
@ -133,34 +110,21 @@ func handleCapture(node: Node) -> void:
piece.position = Vector2(25, 25) piece.position = Vector2(25, 25)
piece.position = Vector2(25, 25) piece.position = Vector2(25, 25)
game.currentlyMovingPiece = piece game.currentlyMovingPiece = piece
resolveMoveEffects() game.resolveMoveEffects()
func handleRegularMove(node: Node) -> void: func handleRegularMove(node: Node) -> void:
print("handleRegularMove") print("handleRegularMove", node)
for i in game.areas: for i in game.areas:
if i == node.name: if i == node.name:
var piece = game.get_node("Flow/" + game.selectedNode).get_child(0) var piece = game.get_node("Flow/" + game.selectedNode).get_child(0)
piece.reparent(node) piece.reparent(node)
piece.position = Vector2(25, 25) piece.position = Vector2(25, 25)
game.currentlyMovingPiece = piece 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: func executeMove(targetLocation: String) -> void:
print("executeMove ", targetLocation) print("executeMove ", targetLocation)
var targetNode = game.get_node("Flow/" + game.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: func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.POST_MOVE) print("ENTERING STATE ", Constants.POST_MOVE)
game.resolveMoveEffects() # game.resolveMoveEffects()
finished.emit(Constants.EVALUATE_POSITION) finished.emit(Constants.EVALUATE_POSITION)

View file

@ -3,7 +3,7 @@ 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()
@ -11,16 +11,17 @@ 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.game = owner
stateNode.finished.connect(transitionToNextState)
await owner.ready await owner.ready
state.enter("") state.enter("")
func unhandledInput(event: InputEvent) -> void: func unhandledInput(event: InputEvent) -> void:
print("StateMachine received input:", event) print("StateMachine received input:", event)
state.handleInput(event) state.handleInput(event)
func process(delta: float) -> void: state.update(delta) 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: func transitionToNextState(targetStatePath: String, data: Dictionary = {}) -> void:
print("TRANSITIONING TO: ", targetStatePath) print("TRANSITIONING TO: ", targetStatePath)
if not has_node(targetStatePath): if not has_node(targetStatePath):
printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.") printerr(owner.name + ": Trying to transition to state " + targetStatePath + " but it does not exist.")
return return
previouseState = state.name previouseState = state.name
state.exit() state.exit()
state = get_node(targetStatePath) state = get_node(targetStatePath)
state.enter(previouseState, data) state.enter(previouseState, data)