Update
This commit is contained in:
107
demos/11. timed options/timed_options.gd
Normal file
107
demos/11. timed options/timed_options.gd
Normal file
@@ -0,0 +1,107 @@
|
||||
extends VBoxContainer
|
||||
|
||||
|
||||
@export var dialogue_gdscript : GDScript = null
|
||||
var dialogue_engine : DialogueEngine = null
|
||||
|
||||
@onready var progress_bar : ProgressBar = $ProgressBar
|
||||
@onready var vbox : VBoxContainer = $VBox
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
dialogue_engine = dialogue_gdscript.new()
|
||||
dialogue_engine.dialogue_started.connect(__on_dialogue_started)
|
||||
dialogue_engine.dialogue_continued.connect(__on_dialogue_continued)
|
||||
dialogue_engine.dialogue_finished.connect(__on_dialogue_finished)
|
||||
dialogue_engine.dialogue_cancelled.connect(__on_dialogue_cancelled)
|
||||
|
||||
|
||||
func _input(p_input_event : InputEvent) -> void:
|
||||
if p_input_event.is_action_pressed(&"ui_accept"):
|
||||
dialogue_engine.advance()
|
||||
accept_event() # to avoid hidding an button due to the input event travelling through the children
|
||||
|
||||
|
||||
func __on_dialogue_started() -> void:
|
||||
print("Dialogue Started!")
|
||||
|
||||
|
||||
var enabled_buttons : Array[Button] = []
|
||||
|
||||
|
||||
func __on_dialogue_continued(p_dialogue_entry : DialogueEntry) -> void:
|
||||
var label : RichTextLabel = RichTextLabel.new()
|
||||
label.set_use_bbcode(true)
|
||||
label.set_fit_content(true)
|
||||
label.set_text(" > " + p_dialogue_entry.get_text())
|
||||
vbox.add_child(label)
|
||||
|
||||
if p_dialogue_entry.has_options():
|
||||
var dont_show_options : Array = p_dialogue_entry.get_metadata("dont_show_options", [])
|
||||
for option_id : int in range(0, p_dialogue_entry.get_option_count()):
|
||||
if option_id in dont_show_options:
|
||||
continue
|
||||
var option_text : String = p_dialogue_entry.get_option_text(option_id)
|
||||
var button : Button = Button.new()
|
||||
button.set_text(option_text)
|
||||
vbox.add_child(button)
|
||||
var tween: Tween = create_tween()
|
||||
if option_id == 0:
|
||||
button.grab_focus()
|
||||
tween.tween_property(button, "modulate", Color.TRANSPARENT, 3.0)
|
||||
tween.tween_callback(button.hide)
|
||||
else:
|
||||
# Only show other buttons after the tween finishes
|
||||
button.hide()
|
||||
tween.tween_callback(button.show).set_delay(5.0)
|
||||
|
||||
if option_id == 1:
|
||||
tween.tween_callback(button.grab_focus)
|
||||
tween.tween_callback(progress_bar.show)
|
||||
tween.tween_method(progress_bar.set_value, 1.0, 0.0, 2.0)
|
||||
|
||||
# The timer has just finished
|
||||
tween.tween_callback(progress_bar.hide)
|
||||
tween.tween_callback(advance_dialogue_no_answer)
|
||||
button.pressed.connect(__advance_dialogue_with_chosen_option.bind(option_id))
|
||||
enabled_buttons.push_back(button)
|
||||
set_process_input(false)
|
||||
|
||||
|
||||
func advance_dialogue_no_answer() -> void:
|
||||
for button : Button in enabled_buttons:
|
||||
button.set_disabled(true)
|
||||
|
||||
var entry : DialogueEntry = dialogue_engine.get_current_entry()
|
||||
var option_id : int = entry.get_metadata("auto_choose")
|
||||
entry.choose_option(option_id)
|
||||
dialogue_engine.advance()
|
||||
set_process_input(true)
|
||||
|
||||
|
||||
func __advance_dialogue_with_chosen_option(p_option_id : int) -> void:
|
||||
# Kill all tweens from processing further
|
||||
for tween: Tween in get_tree().get_processed_tweens():
|
||||
tween.kill()
|
||||
for button : Button in enabled_buttons:
|
||||
button.set_disabled(true)
|
||||
# Reset modulate of vanishing button
|
||||
button.modulate = Color.WHITE
|
||||
enabled_buttons.clear()
|
||||
progress_bar.hide()
|
||||
|
||||
var current_entry : DialogueEntry = dialogue_engine.get_current_entry()
|
||||
current_entry.choose_option(p_option_id)
|
||||
dialogue_engine.advance()
|
||||
|
||||
set_process_input(true)
|
||||
|
||||
|
||||
func __on_dialogue_finished() -> void:
|
||||
print("Dialogue Finished! Exiting...")
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
func __on_dialogue_cancelled() -> void:
|
||||
print("Dialogue Cancelled! Exiting...")
|
||||
get_tree().quit()
|
||||
36
demos/11. timed options/timed_options.tscn
Normal file
36
demos/11. timed options/timed_options.tscn
Normal file
@@ -0,0 +1,36 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://ctlvurl3kobjf"]
|
||||
|
||||
[ext_resource type="Script" path="res://demos/11. timed options/timed_options.gd" id="1_g0ote"]
|
||||
[ext_resource type="Script" path="res://demos/11. timed options/timed_options_dialogue.gd" id="2_gqlp8"]
|
||||
|
||||
[node name="TimedOptions" type="VBoxContainer"]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_g0ote")
|
||||
dialogue_gdscript = ExtResource("2_gqlp8")
|
||||
|
||||
[node name="DialogueEngineDemoQuickStartPanelContainer" type="PanelContainer" parent="."]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="DialogueEngineDemoQuickStartLabel" type="Label" parent="DialogueEngineDemoQuickStartPanelContainer"]
|
||||
layout_mode = 2
|
||||
text = "Press <Enter> or <Space> to progress the dialogue."
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="VBox" type="VBoxContainer" parent="."]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="LogRichTextLabel" type="RichTextLabel" parent="VBox"]
|
||||
layout_mode = 2
|
||||
text = "Log:"
|
||||
fit_content = true
|
||||
|
||||
[node name="ProgressBar" type="ProgressBar" parent="."]
|
||||
visible = false
|
||||
custom_minimum_size = Vector2(0, 16)
|
||||
layout_mode = 2
|
||||
max_value = 1.0
|
||||
show_percentage = false
|
||||
37
demos/11. timed options/timed_options_dialogue.gd
Normal file
37
demos/11. timed options/timed_options_dialogue.gd
Normal file
@@ -0,0 +1,37 @@
|
||||
extends DialogueEngine
|
||||
|
||||
|
||||
enum {
|
||||
DEFAULT_TOPIC = 0, # this is the branch used by default unless set_branch_id() is used
|
||||
WATCH_THE_STORM,
|
||||
GO_BACK_TO_SLEEP,
|
||||
KEEP_WORKING,
|
||||
}
|
||||
|
||||
|
||||
func _setup() -> void:
|
||||
var entry : DialogueEntry = add_text_entry("The storm rages right outside the window. I should...")
|
||||
var option_id_1 : int = entry.add_option("Wait for storm to finish.")
|
||||
var option_id_2 : int = entry.add_option("Go back to sleep.")
|
||||
var option_id_3 : int = entry.add_option("Get back to work.")
|
||||
var option_id_4 : int = entry.add_option("Hidden option -- this should not be shown on the UI")
|
||||
entry.set_metadata("dont_show_options", [option_id_4])
|
||||
entry.set_metadata("auto_choose", option_id_4)
|
||||
|
||||
var option_id_2_entry : DialogueEntry = add_text_entry("That's right, sleep is for the strong 💪.", GO_BACK_TO_SLEEP)
|
||||
entry.set_option_goto_id(option_id_2, option_id_2_entry.get_id())
|
||||
|
||||
var option_id_3_entry : DialogueEntry = add_text_entry("That's right, let's get back to work 🫡", KEEP_WORKING)
|
||||
entry.set_option_goto_id(option_id_3, option_id_3_entry.get_id())
|
||||
|
||||
var option_id_4_entry : DialogueEntry = add_text_entry("I think I'll enjoy watching the storm for a bit...", WATCH_THE_STORM)
|
||||
entry.set_option_goto_id(option_id_4, option_id_4_entry.get_id())
|
||||
|
||||
# Join branches into the default topic (i.e. branch id 0)
|
||||
var default_topic : DialogueEntry = add_text_entry("Some time passes...")
|
||||
entry.set_option_goto_id(option_id_1, default_topic.get_id())
|
||||
option_id_2_entry.set_goto_id(default_topic.get_id())
|
||||
option_id_3_entry.set_goto_id(default_topic.get_id())
|
||||
option_id_4_entry.set_goto_id(default_topic.get_id())
|
||||
|
||||
add_text_entry("<Press 'Space' or 'Enter' to quit>")
|
||||
Reference in New Issue
Block a user