Files
experiment-adventure-ai/cutscene/actions/DialogueAction.gd
2025-08-01 08:34:51 -07:00

76 lines
2.3 KiB
GDScript
Executable File

class_name DialogueAction
extends "res://cutscene/Action.gd"
# Properties
var character: Node2D # The speaking character (optional)
var text: String # Dialogue text
var duration: float = 0.0 # Duration to display (0 = manual advance)
func _init(speaking_character: Node2D, dialogue_text: String, display_duration: float = 0.0) -> void:
character = speaking_character
text = dialogue_text
duration = display_duration
name = "DialogueAction"
func start() -> void:
# Display the dialogue text
_display_dialogue()
self._set_running()
# If duration is 0, this is a manual advance action
if duration <= 0:
# For demo purposes, we'll complete immediately
# In a real game, this would wait for player input
self._set_completed()
else:
# Start a timer for automatic completion
var timer = Timer.new()
timer.wait_time = duration
timer.one_shot = true
timer.connect("timeout", _on_timer_timeout)
# Add timer as a child to ensure it processes
if Engine.get_main_loop().current_scene:
Engine.get_main_loop().current_scene.add_child(timer)
#else:
## Fallback if we can't access the main scene
#var root = get_tree().root if get_tree() else null
#if root and root.get_child_count() > 0:
#root.get_child(0).add_child(timer)
#else:
## Last resort - connect directly and manage manually
#timer.connect("timeout", _on_timer_timeout_direct)
#timer.start(duration)
#return
timer.connect("timeout", _cleanup_timer.bind(timer))
timer.start()
func _display_dialogue() -> void:
# In a real implementation, this would interface with a dialogue system
# For now, we'll simulate by printing to console
var character_name = character.name if character and character.name else "Unknown"
print("%s: %s" % [character_name, text])
func _on_timer_timeout() -> void:
self._set_completed()
func _on_timer_timeout_direct() -> void:
# Direct timeout handler for when we can't add the timer to the scene tree
self._set_completed()
func _cleanup_timer(timer: Timer) -> void:
# Clean up the timer
if timer and timer.get_parent():
timer.get_parent().remove_child(timer)
if timer:
timer.queue_free()
self._set_completed()
func is_completed() -> bool:
return state == State.COMPLETED
func stop() -> void:
# Clean up any timers when stopping
super.stop()