updates for cleaning functionality
This commit is contained in:
@@ -11,10 +11,15 @@ signal phase_changed(phase_name: String)
|
||||
signal card_played(card_data: Dictionary)
|
||||
signal damage_dealt(player_name: String, amount: int)
|
||||
signal message(text: String)
|
||||
signal action_undone(action_name: String)
|
||||
signal undo_available_changed(available: bool)
|
||||
|
||||
# Game state
|
||||
var game_state: GameState = null
|
||||
|
||||
# Undo system
|
||||
var undo_system: UndoSystem = null
|
||||
|
||||
# State flags
|
||||
var is_initialized: bool = false
|
||||
var is_game_active: bool = false
|
||||
@@ -55,6 +60,11 @@ func start_new_game() -> void:
|
||||
# Create new game state
|
||||
game_state = GameState.new()
|
||||
|
||||
# Create undo system
|
||||
undo_system = UndoSystem.new(game_state)
|
||||
undo_system.undo_available_changed.connect(_on_undo_available_changed)
|
||||
undo_system.action_undone.connect(_on_action_undone)
|
||||
|
||||
# Connect signals
|
||||
_connect_game_signals()
|
||||
|
||||
@@ -131,21 +141,67 @@ func try_play_card(card: CardInstance) -> bool:
|
||||
selected_card = card
|
||||
return false
|
||||
|
||||
# Check play restrictions before attempting to play
|
||||
var play_error = _check_play_restrictions(player, card)
|
||||
if play_error != "":
|
||||
message.emit(play_error)
|
||||
return false
|
||||
|
||||
# Determine target zone
|
||||
var to_zone = Enums.ZoneType.FIELD_FORWARDS if card.is_forward() else Enums.ZoneType.FIELD_BACKUPS
|
||||
|
||||
# Try to play
|
||||
if game_state.play_card(player_index, card):
|
||||
# Record for undo
|
||||
if undo_system:
|
||||
undo_system.record_play_card(player_index, card, to_zone, {})
|
||||
|
||||
message.emit("Played " + card.get_display_name())
|
||||
return true
|
||||
else:
|
||||
message.emit("Cannot play that card!")
|
||||
# This shouldn't happen if _check_play_restrictions works correctly
|
||||
# but provide context if it does
|
||||
message.emit("Failed to play " + card.get_display_name() + " (internal error)")
|
||||
return false
|
||||
|
||||
## Check play restrictions and return error message, or empty string if playable
|
||||
func _check_play_restrictions(player: Player, card: CardInstance) -> String:
|
||||
# Check if card is in hand
|
||||
if not player.hand.has_card(card):
|
||||
return "Card is no longer in your hand!"
|
||||
|
||||
# Check backup limit
|
||||
if card.is_backup():
|
||||
if player.field_backups.get_count() >= Player.MAX_BACKUPS:
|
||||
return "Cannot play: Maximum 5 Backups allowed!"
|
||||
|
||||
# Check unique name restriction (non-generic cards)
|
||||
if not card.card_data.is_generic:
|
||||
if card.is_forward() and player.field_forwards.has_card_with_name(card.card_data.name):
|
||||
return "Cannot play: You already have " + card.card_data.name + " on the field!"
|
||||
if card.is_backup() and player.field_backups.has_card_with_name(card.card_data.name):
|
||||
return "Cannot play: You already have " + card.card_data.name + " on the field!"
|
||||
|
||||
# Check Light/Dark restriction
|
||||
if card.is_light_or_dark():
|
||||
if player.field_forwards.has_light_or_dark() or player.field_backups.has_light_or_dark():
|
||||
return "Cannot play: You can only have one Light/Dark card on the field!"
|
||||
|
||||
return ""
|
||||
|
||||
## Discard a card to generate CP
|
||||
func discard_card_for_cp(card: CardInstance) -> bool:
|
||||
if not game_state or not is_game_active:
|
||||
return false
|
||||
|
||||
var player_index = card.owner_index
|
||||
var element = card.get_element()
|
||||
|
||||
if game_state.discard_for_cp(player_index, card):
|
||||
# Record for undo
|
||||
if undo_system:
|
||||
undo_system.record_discard_for_cp(player_index, card, element)
|
||||
|
||||
message.emit("Discarded " + card.get_display_name() + " for 2 CP")
|
||||
_check_pending_action()
|
||||
return true
|
||||
@@ -158,7 +214,13 @@ func dull_backup_for_cp(card: CardInstance) -> bool:
|
||||
return false
|
||||
|
||||
var player_index = card.controller_index
|
||||
var element = card.get_element()
|
||||
|
||||
if game_state.dull_backup_for_cp(player_index, card):
|
||||
# Record for undo
|
||||
if undo_system:
|
||||
undo_system.record_dull_backup_for_cp(player_index, card, element)
|
||||
|
||||
message.emit("Dulled " + card.get_display_name() + " for 1 CP")
|
||||
_check_pending_action()
|
||||
return true
|
||||
@@ -169,6 +231,13 @@ func dull_backup_for_cp(card: CardInstance) -> bool:
|
||||
func _check_pending_action() -> void:
|
||||
if input_mode == InputMode.SELECT_CP_SOURCE and selected_card:
|
||||
var player = game_state.get_player(selected_card.controller_index)
|
||||
|
||||
# Check if the card is still in hand (it may have been discarded for CP)
|
||||
if not player.hand.has_card(selected_card):
|
||||
message.emit("The card you wanted to play was discarded!")
|
||||
clear_selection()
|
||||
return
|
||||
|
||||
if player.cp_pool.can_afford_card(selected_card.card_data):
|
||||
# Can now afford - try to play
|
||||
try_play_card(selected_card)
|
||||
@@ -233,8 +302,48 @@ func clear_selection() -> void:
|
||||
selected_card = null
|
||||
pending_action = func(): pass
|
||||
|
||||
## Undo the last action
|
||||
func undo_last_action() -> bool:
|
||||
if not undo_system:
|
||||
return false
|
||||
|
||||
return undo_system.undo()
|
||||
|
||||
## Check if undo is available
|
||||
func can_undo() -> bool:
|
||||
if not undo_system:
|
||||
return false
|
||||
return undo_system.can_undo()
|
||||
|
||||
## Get description of last undoable action
|
||||
func get_undo_description() -> String:
|
||||
if not undo_system:
|
||||
return ""
|
||||
return undo_system.get_last_action_description()
|
||||
|
||||
## Restore the input mode based on the current phase
|
||||
func restore_input_mode_for_phase() -> void:
|
||||
if not game_state:
|
||||
return
|
||||
|
||||
var phase = game_state.turn_manager.current_phase
|
||||
match phase:
|
||||
Enums.TurnPhase.MAIN_1, Enums.TurnPhase.MAIN_2:
|
||||
input_mode = InputMode.SELECT_CARD_TO_PLAY
|
||||
Enums.TurnPhase.ATTACK:
|
||||
input_mode = InputMode.SELECT_ATTACKER
|
||||
_:
|
||||
input_mode = InputMode.NONE
|
||||
|
||||
## Signal handlers
|
||||
|
||||
func _on_undo_available_changed(available: bool) -> void:
|
||||
undo_available_changed.emit(available)
|
||||
|
||||
func _on_action_undone(action_name: String) -> void:
|
||||
message.emit("Undid: " + action_name)
|
||||
action_undone.emit(action_name)
|
||||
|
||||
func _on_game_ended(winner_index: int) -> void:
|
||||
is_game_active = false
|
||||
var winner_name = game_state.get_player(winner_index).player_name
|
||||
@@ -276,6 +385,10 @@ func _on_phase_changed(phase: Enums.TurnPhase) -> void:
|
||||
var phase_name = Enums.phase_to_string(phase)
|
||||
phase_changed.emit(phase_name)
|
||||
|
||||
# Clear undo history on phase change
|
||||
if undo_system:
|
||||
undo_system.clear_history()
|
||||
|
||||
# Set appropriate input mode
|
||||
match phase:
|
||||
Enums.TurnPhase.MAIN_1, Enums.TurnPhase.MAIN_2:
|
||||
|
||||
Reference in New Issue
Block a user