diff --git a/Systems/Card.gd b/Systems/Card.gd index 31d7040..589b7a3 100644 --- a/Systems/Card.gd +++ b/Systems/Card.gd @@ -3,10 +3,10 @@ class_name Card enum Rank {RANK_0, RANK_1, RANK_2, RANK_3} enum EffectType { - MOVEMENT_MODIFIER, - BOARD_EFFECT, - PIECE_EFFECT, - SPECIAL_ACTION + MOVEMENT_MODIFIER, + BOARD_EFFECT, + PIECE_EFFECT, + SPECIAL_ACTION } var card_name: String @@ -18,9 +18,11 @@ var burned: bool = false var effect_type: EffectType var remaining_turns: int = 0 var unit_whitelist: Array[String] = [] # List of piece types this card can be attached to +var id: String = Utils.generate_guid() func _init(): remaining_turns = duration + # print(id) func can_attach_to_piece(piece: Pawn) -> bool: if unit_whitelist.is_empty(): @@ -33,7 +35,7 @@ func apply_effect(target_piece = null, board_flow = null, game_state = null): remaining_turns = duration attached_piece = target_piece - + match rank: Rank.RANK_0: burned = true Rank.RANK_1: burned = true @@ -47,12 +49,12 @@ func update_duration(): remaining_turns -= 1 if remaining_turns <= 0: remove_effect() - - + + func modify_moves() -> Dictionary: return {} - - + + func remove_effect(): if attached_piece: pass diff --git a/Systems/CardDisplay.gd b/Systems/CardDisplay.gd index a6fa5ea..076922c 100644 --- a/Systems/CardDisplay.gd +++ b/Systems/CardDisplay.gd @@ -3,27 +3,30 @@ class_name CardDisplay var card_displays = [] var selected_card = null -const CARD_WIDTH = 100 -const CARD_HEIGHT = 150 +const CARD_WIDTH = 150 +const CARD_HEIGHT = 250 const CARD_MARGIN = 10 -var container: HBoxContainer +@onready var container: HBoxContainer func _ready(): container = HBoxContainer.new() - container.name = "CardContainer" + container.name = "Hand" container.set_position(Vector2(10, 500)) add_child(container) func update_hand(hand: Array): clear_cards() + print("update_hand") for card in hand: + print(card.id) add_card_display(card) -func add_card_display(card: Card): +func add_card_display(card): var card_panel = PanelContainer.new() card_panel.custom_minimum_size = Vector2(CARD_WIDTH, CARD_HEIGHT) var vbox = VBoxContainer.new() + vbox.set_meta("card_id", card.id) card_panel.add_child(vbox) var name_label = Label.new() @@ -41,23 +44,49 @@ func add_card_display(card: Card): desc_label.autowrap_mode = TextServer.AUTOWRAP_WORD desc_label.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0) vbox.add_child(desc_label) + + var id_label = Label.new() + id_label.text = card.id + id_label.autowrap_mode = true + id_label.custom_minimum_size = Vector2(CARD_WIDTH - 10, 0) + vbox.add_child(id_label) + + + card_panel.gui_input.connect(func(event): _on_card_clicked(event, 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) + if selected_card == null || selected_card.id != card.id: + selected_card = card + container.emit_signal("card_pressed", card) + elif selected_card.id == card.id: + selected_card = null + highlight_selected_card(selected_card) + # selected_card = card + # highlight_selected_card(card) + # container.emit_signal("card_pressed", card) -func highlight_selected_card(selected: Card): +func highlight_selected_card(selected: Card) -> void: 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) + style.bg_color = Color(0.2, 0.2, 0.2, 1) # Default color + + var vbox = display.get_child(0) + if selected && vbox.get_meta("card_id") == selected.id: + style.bg_color = Color(0.4, 0.6, 0.4, 1) # Selected color + display.add_theme_stylebox_override("panel", style) +func get_card_by_uuid(uuid: String) -> Card: + for display in card_displays: + var vbox = display.get_child(0) + if vbox.get_meta("card_id") == uuid: + return selected_card + return null + func get_selected_card() -> Card: return selected_card diff --git a/Systems/Cards/Doubletime.gd b/Systems/Cards/Doubletime.gd index d348c4d..7c806df 100644 --- a/Systems/Cards/Doubletime.gd +++ b/Systems/Cards/Doubletime.gd @@ -1,22 +1,25 @@ -class DoubleTimeCard extends Card: - func _init(): - super._init() - card_name = "Double Time" - rank = Rank.RANK_2 - effect_type = EffectType.MOVEMENT_MODIFIER - description = "Piece can move twice in one turn" - duration = 2 # Lasts for 2 turns +class_name DoubleTimeCard extends Card + +func _init(): + super._init() + # id = Utils.generate_guid() + card_name = "Double Time" + rank = Rank.RANK_2 + effect_type = EffectType.MOVEMENT_MODIFIER + description = "Piece can move twice in one turn" + duration = 2 # Lasts for 2 turns + unit_whitelist = [] + +func modify_moves() -> Dictionary: + return { + "extra_moves": 1, + "move_multiplier": 1 + } + +func apply_effect(target_piece = null, board_flow = null, game_state = null): + if !super.apply_effect(target_piece, board_flow, game_state): + return false - func modify_moves() -> Dictionary: - return { - "extra_moves": 1, - "move_multiplier": 1 - } - - func apply_effect(target_piece = null, board_flow = null, game_state = null): - if !super.apply_effect(target_piece, board_flow, game_state): - return false - - # Logic to allow second move would be implemented in Game.gd - attached_piece = target_piece - return true + # Logic to allow second move would be implemented in Game.gd + attached_piece = target_piece + return true \ No newline at end of file diff --git a/Systems/Cards/Drunkdriving.gd b/Systems/Cards/Drunkdriving.gd index d762b3c..d9a91b3 100644 --- a/Systems/Cards/Drunkdriving.gd +++ b/Systems/Cards/Drunkdriving.gd @@ -1,19 +1,20 @@ -class DrunkDrivingCard extends Card: - func _init(): - super._init() - card_name = "Drunk Driving" - rank = Rank.RANK_1 - effect_type = EffectType.SPECIAL_ACTION - description = "Force Rook to move to opposite end" - unit_whitelist = ["Rook"] # Can only be attached to Rooks - - func apply_effect(target_piece = null, board_flow = null, game_state = null): - if !super.apply_effect(target_piece, board_flow, game_state): - return false - - if target_piece: - var current_pos = target_piece.get_parent().name.split("-") - var target_x = current_pos[0] - var target_y = "0" if target_piece.Item_Color == 0 else "7" - return [target_x + "-" + target_y] +class_name DrunkDrivingCard extends Card +func _init(): + super._init() + # id = Utils.generate_guid() + card_name = "Drunk Driving" + rank = Rank.RANK_1 + effect_type = EffectType.SPECIAL_ACTION + description = "Force Rook to move to opposite end" + unit_whitelist = ["Rook"] # Can only be attached to Rooks + +func apply_effect(target_piece = null, board_flow = null, game_state = null): + if !super.apply_effect(target_piece, board_flow, game_state): return false + + if target_piece: + var current_pos = target_piece.get_parent().name.split("-") + var target_x = current_pos[0] + var target_y = "0" if target_piece.Item_Color == 0 else "7" + return [target_x + "-" + target_y] + return false diff --git a/Systems/Cards/Supernova.gd b/Systems/Cards/Supernova.gd index daede16..e41f3d4 100644 --- a/Systems/Cards/Supernova.gd +++ b/Systems/Cards/Supernova.gd @@ -1,20 +1,21 @@ -class SupernovaCard extends Card: - func _init(): - super._init() - card_name = "Supernova" - rank = Rank.RANK_0 - effect_type = EffectType.PIECE_EFFECT - description = "Remove all abilities from enemy pieces" - unit_whitelist = ["King"] # Can only be used by Kings +class_name SupernovaCard extends Card +func _init(): + super._init() + # id = Utils.generate_guid() + card_name = "Supernova" + rank = Rank.RANK_0 + effect_type = EffectType.PIECE_EFFECT + description = "Remove all abilities from enemy pieces" + unit_whitelist = ["King"] # Can only be used by Kings + +func apply_effect(target_piece = null, board_flow = null, game_state = null): + if !super.apply_effect(target_piece, board_flow, game_state): + return false - func apply_effect(target_piece = null, board_flow = null, game_state = null): - if !super.apply_effect(target_piece, board_flow, game_state): - return false - - if board_flow: - for cell in board_flow.get_children(): - if cell.get_child_count() > 0: - var piece = cell.get_child(0) - if piece.Item_Color != target_piece.Item_Color: - pass - return true + if board_flow: + for cell in board_flow.get_children(): + if cell.get_child_count() > 0: + var piece = cell.get_child(0) + if piece.Item_Color != target_piece.Item_Color: + pass + return true diff --git a/Systems/DeckManager.gd b/Systems/DeckManager.gd index c4ae865..c223306 100644 --- a/Systems/DeckManager.gd +++ b/Systems/DeckManager.gd @@ -1,6 +1,9 @@ extends Node class_name DeckManager +const DoubleTimeCard = preload("res://Systems/Cards/Doubletime.gd") + + var deck: Array = [] var hand: Array = [] var discard: Array = [] @@ -19,9 +22,8 @@ func _init(): initializeStartingDeck() func initializeStartingDeck(): - # Add basic Rank 3 and 4 cards as per design doc for i in range(10): - deck.append(Card.new()) # Basic movement cards + deck.append(DoubleTimeCard.new()) shuffleDeck() drawStartingHand() @@ -49,10 +51,10 @@ signal hand_updated func playCard(card: Card, target_piece: Pawn, board_flow = null, game_state = null): if !hand.has(card): return false - emit_signal("hand_updated", hand) if card.apply_effect(target_piece, board_flow, game_state): hand.erase(card) + emit_signal("hand_updated", hand) match card.rank: Card.Rank.RANK_0: diff --git a/Systems/Game/ChessGame.gd b/Systems/Game/ChessGame.gd index 69a53e8..5cd8119 100644 --- a/Systems/Game/ChessGame.gd +++ b/Systems/Game/ChessGame.gd @@ -84,6 +84,13 @@ func initializeDeckSystem() -> void: cardDisplay = CardDisplay.new() add_child(deckManager) add_child(cardDisplay) + + if !deckManager.has_user_signal("card_pressed"): + deckManager.add_user_signal("card_pressed") + + if !deckManager.has_user_signal("hand_updated"): + deckManager.add_user_signal("hand_updated") + cardDisplay.update_hand(deckManager.hand) deckManager.connect("hand_updated", func(hand): cardDisplay.update_hand(hand)) diff --git a/Systems/StateMachine/GameStates/AttachCards.gd b/Systems/StateMachine/GameStates/AttachCards.gd index 9cb1345..f33bfdf 100644 --- a/Systems/StateMachine/GameStates/AttachCards.gd +++ b/Systems/StateMachine/GameStates/AttachCards.gd @@ -1,21 +1,63 @@ extends "res://Systems/StateMachine/ChessGameState.gd" +const ATTACHMENT_PHASE_DURATION = 30 # 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: - # finished.emit(Constants.APPLY_CARD_EFFECTS) print("ENTERING STATE ", Constants.ATTACH_CARDS) - if !game.boardContainer.is_connected("card_pressed", handleCardPressed): - print("Connecting card_pressed signal") - game.boardContainer.connect("card_pressed", handleCardPressed) - + 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("card_pressed", handleCardPressed): - game.boardContainer.disconnect("card_pressed", handleCardPressed) + if game.boardContainer.is_connected("tile_pressed", handleTilePressed): + game.boardContainer.disconnect("tile_pressed", handleTilePressed) + + game.cardDisplay.selected_card = null + game.selectedNode = "" + game.resetHighlights() + +func handleTilePressed(location: String) -> void: + print("HANDLING Tile PRESSED ", location) + var targetNode = game.get_node("Flow/" + location) + + if targetNode.get_child_count() == 0: + return + + var piece = targetNode.get_child(0) + var selectedCard = game.cardDisplay.get_selected_card() + + 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.card_name, " to ", piece.name) + if game.deckManager.hand.is_empty(): + complete_attachment_phase() + else: + print("Failed to attach card") + +func _on_timer_timeout() -> void: + 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() -func handleCardPressed(location: String) -> void: - print("HANDLING MOVEMENT ", location) - var node = game.get_node("Flow/" + location) - \ No newline at end of file diff --git a/Utils/Utils.gd b/Utils/Utils.gd new file mode 100644 index 0000000..b1ca935 --- /dev/null +++ b/Utils/Utils.gd @@ -0,0 +1,23 @@ +class_name Utils +extends Object + +static func generate_guid() -> String: + var guid = "" + var time = Time.get_ticks_msec() + var rng = RandomNumberGenerator.new() + rng.randomize() + + # Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx + guid += "%08x" % time + guid += "-" + guid += "%04x" % rng.randi() + guid += "-" + guid += "4" + guid += "%03x" % rng.randi() + guid += "-" + guid += "%x" % (8 + rng.randi() % 4) + guid += "%03x" % rng.randi() + guid += "-" + guid += "%012x" % rng.randi() + + return guid \ No newline at end of file diff --git a/board.tscn b/board.tscn index 2eb0ab7..d972218 100644 --- a/board.tscn +++ b/board.tscn @@ -72,7 +72,7 @@ offset_top = 65.0 offset_right = 1142.0 offset_bottom = 104.0 -[node name="HBoxContainer" type="HBoxContainer" parent="."] +[node name="Hand" type="HBoxContainer" parent="."] layout_mode = 1 anchors_preset = 7 anchor_left = 0.5