ChessBuilder/Systems/StateMachine/GameStates/AttachCards.gd

77 lines
2.8 KiB
GDScript

extends "res://Systems/StateMachine/ChessGameState.gd"
const ATTACHMENT_PHASE_DURATION = 5 # Duration in seconds
var timer: Timer
var tile_pressed_connection: Signal
func _ready() -> void:
super._ready()
timer = Timer.new()
timer.one_shot = true
timer.timeout.connect(_on_timer_timeout)
add_child(timer)
func enter(_previous: String, _data := {}) -> void:
print("ENTERING STATE ", Constants.ATTACH_CARDS)
if(game.currentPlayer == game.BLACK):
_on_timer_timeout()
return
if !game.boardContainer.is_connected("tile_pressed", handleTilePressed):
game.boardContainer.connect("tile_pressed", handleTilePressed)
timer.start(ATTACHMENT_PHASE_DURATION)
if game.deckManager.hand.is_empty():
complete_attachment_phase()
func exit() -> void:
if game.boardContainer.is_connected("tile_pressed", handleTilePressed):
game.boardContainer.disconnect("tile_pressed", handleTilePressed)
# game.cardDisplay.selectedCard = null
# game.selectedNode = ""
# game.resetHighlights()
func handleTilePressed(location: String) -> void:
# print("HANDLING Tile PRESSED ", location)
var container = game.get_node("Flow/" + location) as PieceContainer
var piece = container.get_piece()
if piece == null:
return
var piece_id = piece.get_instance_id()
var selectedCard = game.cardDisplay.getSelectedCard()
if game.deckManager.attached_cards.has(piece_id):
var card = game.deckManager.attached_cards[piece_id]
game.cardPreview.show_card_preview(card)
else:
game.cardPreview.hide_preview()
if selectedCard and piece:
var isCorrectColor = (piece.Item_Color == 0 and game.currentPlayer == game.WHITE) or \
(piece.Item_Color == 1 and game.currentPlayer == game.BLACK)
if isCorrectColor and selectedCard.can_attach_to_piece(piece) and \
!game.deckManager.attached_cards.has(piece.get_instance_id()):
if game.deckManager.playCard(selectedCard, piece, game.boardContainer, game):
# print("Successfully attached card ", selectedCard.cardName, " to ", piece.name, " at ", location)
piece.on_card_effect_changed()
if game.deckManager.hand.is_empty():
complete_attachment_phase()
else:
print("Failed to attach card")
func _on_timer_timeout() -> void:
var stateString: RichTextLabel = owner.get_node("StateLabel")
# prevent transition of state is already changed by other effects
if stateString.text == "AttachCards":
print("Card attachment phase timed out")
finished.emit(Constants.APPLY_CARD_EFFECTS)
func complete_attachment_phase() -> void:
if timer.time_left > 0:
timer.stop()
_on_timer_timeout()